OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Wed, 13 Mar 2019 20:18:25 -0500Looking for ways to speed up pixel scanninghttp://answers.opencv.org/question/210250/looking-for-ways-to-speed-up-pixel-scanning/So I am trying to implement a pixel scanner, where I go through every pixel and check if it is white (the image has already been threshold-ed at this point) and if it is white get the pixel's respective x and y coordinate which I use to calculate distances. So far I have been doing it the double for-loop way, and it is adding significant slowdowns compared to the rest of my pipeline. I would like to speed this up. I have looked into using `cv::findNonZero()`, it gives me a vector of cv::Point, when I try and access the vector at some interation like `vec[ i ]` the output is *[ rowValue, colValue ]* which is expected but I don't know how to access the "rowValue" or "colValue" independently that I need in order to calculate distances. I have heard about using `cv::LUT()` but I'm having some trouble understanding how to use it.
Thanks is advanced and let me know if more info is needed.
Code below:
cv::Mat W; // approx 1000 x 1000
for (int i = 0; i < W.cols; i++)
{
for (int j = 0; j < W.rows; j++)
{
cv::Scalar pixelColor = counter.at<uchar>(j, i);
if (pixelColor.val[0] == 255)
{
// pixelLocations are defined as a ROS message, A B C D are constants
// used to calculate distances in meters
pixelLocation.x = (A * i) + B; // distance in x direction relative to robot
pixelLocation.y = (C * j) + D; // distance in front of robot
}
}
}drcurryWed, 13 Mar 2019 20:18:25 -0500http://answers.opencv.org/question/210250/Finding 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!mstankieTue, 07 Feb 2017 07:56:46 -0600http://answers.opencv.org/question/125174/What is the best way to find bounding box for binary mask?http://answers.opencv.org/question/4183/what-is-the-best-way-to-find-bounding-box-for-binary-mask/I have a binary mask of an object and want to get its bounding rectangle.
Function cv::boundingRect wants a vector of cv::Point, while I have a matrix.
I've written my own function, which reduces the binary mask with CV_REDUCE_MAX first to a column then to a row and finds leftmost and rightmost and topmost and bottommost non-zero elements.
The mask is strictly binary (0 and 1).
Is there any better way?
wl2776Wed, 14 Nov 2012 08:29:34 -0600http://answers.opencv.org/question/4183/Split contours into many small rectangleshttp://answers.opencv.org/question/25912/split-contours-into-many-small-rectangles/
As the graph show, the most easiest solution is return a bounding rect surrounded the contour, split the bounding rect to small rectangles, but it is much harder for irregular shape.What kind of algorithm would you suggest to split the contour?
![image description](/upfiles/13881416279081609.png)stereoMatchingFri, 27 Dec 2013 04:52:49 -0600http://answers.opencv.org/question/25912/OpenCV (Java): how to access coordinates returned by findNonZero()?http://answers.opencv.org/question/18551/opencv-java-how-to-access-coordinates-returned-by-findnonzero/In my android app, I am using findNonZero() to find and access white points in a binary image.
I get the image (called `binMat` with size: 595*800) as the input to the method and then I have:
...
Mat wLocMat = Mat.zeros(binMat.size(), binMat.channels());
Core.findNonZero(binMat, wLocMat);
The problem is: I don't know how to access the white points' coordinates stored inside the wLocMat!
When I log the wLocMat.toString(), I get this:
[ 476000*1*CV_32SC2, isCont=true, isSubmat=false, nativeObj=0x74abd920, dataAddr=0x75bac010 ]
So, how exactly can I access the coordinates stored in wLocMat?!
Mahm00dSat, 10 Aug 2013 00:50:18 -0500http://answers.opencv.org/question/18551/