[英]How to count zero pixels between consecutive non-zero ones in OpenCV?
From a grayscale image I obtained its non-zero pixel coordinates using this line of code in OpenCV: 从灰度图像中,我使用OpenCV中的以下代码行获得其非零像素坐标:
cv::Mat nonZeroCoordinates;
cv::findNonZero(image, nonZeroCoordinates);
Now, I want to count the zero pixels between two consecutive non-zero elements in image
. 现在,我要计算image
两个连续的非零元素之间的零像素。 But should I move through columns or rows first? 但是我应该先浏览列还是行? For instance, if I have non-zero pixels at point (30, 20) and (30, 23) in a 40x40 mat, and moving through columns give me 2 zero pixels between them, and moving through rows gives me (39-30) + 40 + 40 + 29 = 118 pixels. 例如,如果我在40x40的Mat中的点(30,20)和(30,23)处有非零像素,则在列之间移动会给我2个零像素,在行之间移动会给我(39-30 )+ 40 + 40 + 29 = 118像素。 Through which way should I count these pixels correctly? 我应该通过哪种方式正确计算这些像素? Is there any better way then just nested loop for this purpose? 有没有比嵌套循环更好的方法了? Thank you. 谢谢。
I think you should fit a line over the endpoints and count the zero pixels under it. 我认为您应该在端点上放置一条线并计算其下的零像素。 OpenCV has a LineIterator class which you can make a 8-connected or 4-connected line from (30, 20) to (30, 23) and check the values. OpenCV具有LineIterator类,您可以创建从( 30,20 )到( 30,23)的8连接或4连接的线并检查值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.