简体   繁体   English

如何在OpenCV中计算连续的非零像素之间的零像素?

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

相关问题 OpenCV - 二进制图像中所有非零像素的位置 - OpenCV - locations of all non-zero pixels in binary image 如何有效地计算每个多边形区域内的所有非零像素? - How to count all non-zero pixels within each polygon area efficiently? OpenCV:查找二进制Mat图像的所有非零坐标 - OpenCV: Find all non-zero coordinates of a binary Mat image OpenCV稀疏矩阵:如何获取行/列中非零元素的索引 - OpenCV sparse matrix: how to get the indices of non-zero elements in a row/column 根据另一个 Mat 的非零像素选择 cv::Mat 的一部分? - Select part of a cv::Mat based on non-zero pixels of another Mat? 如何强制编译器为未初始化的变量设置非零值? - How to force compiler to set a non-zero value to uninitialized variables? 如何清除霓虹灯中的第一条非零通道? - How to clear all but the first non-zero lane in neon? SIMD / SSE:如何检查所有矢量元素都不为零 - SIMD/SSE: How to check that all vector elements are non-zero C ++:如何在非零大小的地图中初始化矢量 - C++: how to initialize vector in map with non-zero size OpenCV:用最近的非零邻居平均值填充CVMat中的缺失元素吗? - OpenCV: Fill in missing elements in CVMat with avg of nearest non-zero neighbors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM