OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Fri, 10 Feb 2017 04:20:57 -0600Finding nearest non-zero pixelhttp://answers.opencv.org/question/125174/finding-nearest-non-zero-pixel/I've got a binary image `noObjectMask` (`CV_8UC1`) and a given point `objectCenter` (`cv::Point`). If the `objectCenter` is a zer-value pixel, I need to find the nearest non-zero pixel starting from the given point.
The number of non-zero points in the whole image can be large (even up to 50%), so calculating distances for each point returned from `cv::findNonZero` seems to be non-optimal. As the highest probability is that the pixel will be in the close neighborhood, I currently use:
# my prototype script in Python, but the final version will be implemented in C++
if noObjectMask[objectCenter[1],objectCenter[0]] == 0:
# if the objectCenter is zero-value pixel, subtract sequentially its neighborhood ROIs
# increasing its size (r), until the ROI contains at least one non-zero pixel
for r in range(noObjectMask.shape[1]/2):
rectL = objectCenter[1]-r-1
rectR = objectCenter[1]+r
rectT = objectCenter[0]-r-1
rectB = objectCenter[0]+r
# Pythonic way of subtracting ROI: noObjectMask(cv::Rect(...))
rect = noObjectMask[rectL:rectR, rectT:rectB]
if cv2.countNonZero(rect)>0: break
nonZeroNeighbours = cv2.findNonZero(rect)
# calculating the distances between objectCenter and each of nonZeroNeighbours
# and choosing the closest one
This works okay, as in my images the non-zero pixels are typically in the closest neighborhood (`r`<=10px), but the processing time increases dramatically with the distance of the closest pixel. Each repetition of `countNonZero` repeats counting of the previous pixels. This could be improved by incrementing the radius `r` by more than one, but this still looks a bit clumsy to me.
How to improve the procedure? And ideas? -Thanks!Tue, 07 Feb 2017 07:56:46 -0600http://answers.opencv.org/question/125174/finding-nearest-non-zero-pixel/Comment by mstankie for <p>I've got a binary image <code>noObjectMask</code> (<code>CV_8UC1</code>) and a given point <code>objectCenter</code> (<code>cv::Point</code>). If the <code>objectCenter</code> is a zer-value pixel, I need to find the nearest non-zero pixel starting from the given point.</p>
<p>The number of non-zero points in the whole image can be large (even up to 50%), so calculating distances for each point returned from <code>cv::findNonZero</code> seems to be non-optimal. As the highest probability is that the pixel will be in the close neighborhood, I currently use:</p>
<pre><code># my prototype script in Python, but the final version will be implemented in C++
if noObjectMask[objectCenter[1],objectCenter[0]] == 0:
# if the objectCenter is zero-value pixel, subtract sequentially its neighborhood ROIs
# increasing its size (r), until the ROI contains at least one non-zero pixel
for r in range(noObjectMask.shape[1]/2):
rectL = objectCenter[1]-r-1
rectR = objectCenter[1]+r
rectT = objectCenter[0]-r-1
rectB = objectCenter[0]+r
# Pythonic way of subtracting ROI: noObjectMask(cv::Rect(...))
rect = noObjectMask[rectL:rectR, rectT:rectB]
if cv2.countNonZero(rect)>0: break
nonZeroNeighbours = cv2.findNonZero(rect)
# calculating the distances between objectCenter and each of nonZeroNeighbours
# and choosing the closest one
</code></pre>
<p>This works okay, as in my images the non-zero pixels are typically in the closest neighborhood (<code>r</code><=10px), but the processing time increases dramatically with the distance of the closest pixel. Each repetition of <code>countNonZero</code> repeats counting of the previous pixels. This could be improved by incrementing the radius <code>r</code> by more than one, but this still looks a bit clumsy to me.</p>
<p>How to improve the procedure? And ideas? -Thanks!</p>
http://answers.opencv.org/question/125174/finding-nearest-non-zero-pixel/?comment=126683#post-id-126683Okay, this will show me how far I am from the closest non-zero point, and the local gradient will let me estimate the direction of closest point, right? Seems a good idea...Fri, 10 Feb 2017 04:20:57 -0600http://answers.opencv.org/question/125174/finding-nearest-non-zero-pixel/?comment=126683#post-id-126683Comment by sturkmen for <p>I've got a binary image <code>noObjectMask</code> (<code>CV_8UC1</code>) and a given point <code>objectCenter</code> (<code>cv::Point</code>). If the <code>objectCenter</code> is a zer-value pixel, I need to find the nearest non-zero pixel starting from the given point.</p>
<p>The number of non-zero points in the whole image can be large (even up to 50%), so calculating distances for each point returned from <code>cv::findNonZero</code> seems to be non-optimal. As the highest probability is that the pixel will be in the close neighborhood, I currently use:</p>
<pre><code># my prototype script in Python, but the final version will be implemented in C++
if noObjectMask[objectCenter[1],objectCenter[0]] == 0:
# if the objectCenter is zero-value pixel, subtract sequentially its neighborhood ROIs
# increasing its size (r), until the ROI contains at least one non-zero pixel
for r in range(noObjectMask.shape[1]/2):
rectL = objectCenter[1]-r-1
rectR = objectCenter[1]+r
rectT = objectCenter[0]-r-1
rectB = objectCenter[0]+r
# Pythonic way of subtracting ROI: noObjectMask(cv::Rect(...))
rect = noObjectMask[rectL:rectR, rectT:rectB]
if cv2.countNonZero(rect)>0: break
nonZeroNeighbours = cv2.findNonZero(rect)
# calculating the distances between objectCenter and each of nonZeroNeighbours
# and choosing the closest one
</code></pre>
<p>This works okay, as in my images the non-zero pixels are typically in the closest neighborhood (<code>r</code><=10px), but the processing time increases dramatically with the distance of the closest pixel. Each repetition of <code>countNonZero</code> repeats counting of the previous pixels. This could be improved by incrementing the radius <code>r</code> by more than one, but this still looks a bit clumsy to me.</p>
<p>How to improve the procedure? And ideas? -Thanks!</p>
http://answers.opencv.org/question/125174/finding-nearest-non-zero-pixel/?comment=125193#post-id-125193what about [cv::distanceTransform](http://docs.opencv.org/master/d7/d1b/group__imgproc__misc.html#ga8a0b7fdfcb7a13dde018988ba3a43042)Tue, 07 Feb 2017 11:19:43 -0600http://answers.opencv.org/question/125174/finding-nearest-non-zero-pixel/?comment=125193#post-id-125193