计算图像中2点之间的白色像素 [英] Counting white pixels between 2 points in an image

查看:168
本文介绍了计算图像中2点之间的白色像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这条粗白线的起点和终点,分别是[234, 2][134, 397].现在,我想直接计算位于这两个点之间的白色像素的数量.我的目的是计算不在粗线上的白色像素的数量.我想计算位于1像素宽度线上的白色像素.我是Python的新手.我整整一天都在寻找解决方案,但没有找到任何方法.我怎么能找到那个?

I found starting and ending points of this thick white line and these are [234, 2] and [134, 397]. Now I want to calculate number of white pixels that are lying between these 2 points on a straight way. My purpose is to calculate the number of white pixels not on the thick line. I want to count white pixels that are lying on the line of width of 1 pixel. I'm very new to Python. I spent whole day in finding my solution but failed to find any way. How can I find that?

我寻找大概的起点和终点的努力是:

My effort to find the approximate starting and ending points is:

for y in range(height):
    for x in range(width):
         ....
          ....
    print(p1," , ",p2)

现在我在下一步面临麻烦.即计算这两个点之间的白色像素数.

Now I'm facing trouble in next step. i.e. counting the number of white pixels between these 2 points.

推荐答案

根据布雷森汉姆行绘图算法,完全有

max(abs(p1.x - p2.x), abs(p1.y - p2.y)) - 1

p1p2之间的

个像素(不计算p1p2像素).

pixels in between points p1 and p2 (not counting the pixels p1 or p2).

这条线是1像素粗的线.

This line is a 1-pixel thick line.

请注意,像素数与点之间的欧几里得距离不对应,而与L-无限距离(也称为最大范数,棋盘距离或

Note that the number of pixels does not correspond to the Euclidean distance between the points, but to the L-infinity distance, also called max-norm, chessboard distance or Chebyshev distance.

编辑:更新后的问题完全不同.以上就是上限.

The updated question is totally different. The above will be the upper bound.

要计算两点之间线的实际白色像素数,您需要绘制该线.上面的Wikipedia链接提供了有关 算法绘制线条的详细信息. 在MATLAB中这是一个非常简单的实现,应该易于转换为Python .尽管您无需实际画线,但可以读取像素值并计算白色的像素值.

To count the actual number of white pixels on the line between the two points, you need to draw that line. The Wikipedia link above gives details on the algorithm to draw lines. Here is a very simple implementation in MATLAB, should be easy to translate to Python. Though instead of actually drawing the line, you would read the pixel values, and count the ones that are white.

@Ben Jones建议使用Bresenham算法的Python实现.不必那么复杂.这是一个简单的Python脚本,可找到两点之间直线上的所有像素(从上面链接的MATLAB版本翻译而来):

@Ben Jones suggested a Python implementation of Bresenham's algorithm. It needn't be that complicated. Here is a simple Python script that finds all pixels on the straight line between the two points (translated from the MATLAB version linked above):

p1 = np.array([0,0])
p2 = np.array([7,4]) % Two example points.

p = p1
d = p2-p1
N = np.max(np.abs(d))
s = d/N
print(np.rint(p).astype('int'))
for ii in range(0,N):
   p = p+s;
   print(np.rint(p).astype('int'))

这将产生输出:

[0 0]
[1 1]
[2 1]
[3 2]
[4 2]
[5 3]
[6 3]
[7 4]

现在剩下的就是读取这些坐标处的像素值,并确定它们是否为白色.

Now all that's left is reading the pixel values at these coordinates, and determining whether they're white or not.

这篇关于计算图像中2点之间的白色像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆