简体   繁体   English

如何在二进制图像中找到线段的x,y的中间点?

[英]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.

相关问题 如何从与另一个给定线段相交的一组点中找到最短的线(2个端点就可以了) - how can I find a shortest line (2 end points will be fine) from a group of points which is intersect with another given line segment 如何找到包含所有点或与y = x轴对称的点的最小矩形? - How can I find the least rect, which contains all points or the point symmetrical to the y=x axis? 如何在二进制图像中找到特定点? - How can I find specific points in binary images? 找出线段的已知X的Y? - Finding the Y of a known X of a line segment? 如何从一堆点中找到包含三角形的最小点P(x,y)? - How do i find the smallest point P(x,y) containing triangle from a bunch of points? 如何从线角获得X,Y坐标点? - How to get points X, Y coordinates from line angle? 如何从多个点找到最远的 x, y 坐标? - How to find the farthest x, y coordinates from many points? 如何找到此代码段的复杂性? - How can I find the complexity of this code segment? 如何沿长度为 x 的线段生成 n 个等距点的数组? - How to generate array of n equidistant points along a line segment of length x? 线段中的等距点 - Equidistant points in a line segment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM