[英]How can I find the middle points of x, y of a line segment in a binary image?
I have some damaged line segments in a binary image and I need to fix them (make them straight and at their original thick). 我在二进制图像中有一些损坏的线段,我需要修复它们(使它们笔直且原始厚度)。 In order to do that I have to find the middle points of the segment , so when I check the neighborhood to find the thickness of the lines I'll be able to find where the pixel stops being 1 and becomes 0. 为了做到这一点,我必须找到段的中间点 ,所以当我检查邻域以找到线的粗细时,我将能够找到像素停止为1并变为0的位置。
Assuming your damaged line segments are straight, you can use regionprops in MATLAB to find the center of each bounding box. 假设您的损坏线段是直的,您可以在MATLAB中使用regionprops来查找每个边界框的中心。 Because if a segment is straight, its is always the diagonal line of the bounding box, thus the center of the box is also the center of the semgent. 因为如果一个段是直的,它总是边界框的对角线,因此盒子的中心也是半圆的中心。
Let's call the points A and B to reduce ambiguity, A(Xa, Ya) and B(Xb, Yb) 让我们调用点A和B来减少歧义,A(Xa,Ya)和B(Xb,Yb)
Let C be the middle point. 设C为中间点。
C(Xc, Yc)
Xc = (Xa + Xb) / 2
Yc = (Ya + Yb) / 2
We have four interesting numbers, two for the X coordinates and two for the Y coordinates. 我们有四个有趣的数字,两个用于X坐标,两个用于Y坐标。
Xmin = floor(Xc)
Xmax = ceil(Xc)
Ymin = floor(Yc)
Ymax = ceil(Yc)
The X coordinate of your middle point is either Xmin or Xmax, the Y coordinate of your middle point is either Ymin or Ymax. 中点的X坐标是Xmin或Xmax,中点的Y坐标是Ymin或Ymax。
So we have four potential points: (Xmin, Ymin), (Xmin, Ymax), (Xmax, Ymin), (Xmax, Ymax)
. 因此我们有四个潜在点: (Xmin, Ymin), (Xmin, Ymax), (Xmax, Ymin), (Xmax, Ymax)
。
So, finally, we must decide which point is nearest to C. 所以,最后,我们必须决定哪个点离C最近。
Distance from P(Xp, Yp) to C(Xc, Yc) is: 从P(Xp,Yp)到C(Xc,Yc)的距离是:
sqrt(sqr(Xp - Xc) + sqr(Yp - Yc))
Calculate the four distance from the four points to C, choose the minimum and that will be the best possible middle point. 计算从四个点到C的四个距离,选择最小值,这将是最好的中间点。
Suppose 假设
A = [xa ya];
B = [xb yb];
then 然后
C = round( mean([A;B]) );
Matlab's round
rounds numbers towards their nearest integer, so this minimizes the (city-block) distance from the analytical center ( mean([A;B])
) to the nearest pixel. Matlab将数字round
到它们最接近的整数,因此最小化了从分析中心( mean([A;B])
)到最近像素的(城市块)距离。
If you want to keep sub-pixel precision (which is actually advisable for most calculations until an explicit map from a result to pixel indices is required), just drop the round
and use only the mean
part. 如果你想保持子像素精度(这对于大多数计算来说实际上是可取的,直到需要从结果到像素索引的显式映射),只需放弃该round
并仅使用mean
部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.