OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 18 Jun 2015 12:22:50 -0500Finding Leftmost and Rightmost Edge of Binary Imagehttp://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/I am trying to find the leftmost and rightmost edge of a binary image like the one attached. I am currently doing this by using a for loop starting from the center of the white portion and working towards a direction until I get to my first black pixel. This works great except it is very slow. Is there any other ways to do this, but in a faster way. Thanks all who reply.
![Binary Image](/upfiles/14341292027477476.png)
#### Start Image Processing #########
hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
lower_red = np.array([0,100,100])
upper_red = np.array([10,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
mat=cv.GetMat(cv.fromarray(mask))
moments=cv.Moments(mat)
yc_red= moments.m01/moments.m00
xc_red=moments.m10/moments.m00
width, height = cv.GetSize(mat)
max_right_red_x = 0
for a in range(int(round(xc_red)), width, 3):
for b in range (0, height, 3):
if(mat[b,a] == 0):
continue
elif(a > max_right_red_x):
max_right_red_x = a
max_right_red_y = b
max_left_red_x = width
for a2 in range(int(round(xc_red)), 0, -3):
for b2 in range (0, height, 3):
if(mat[b2,a2] == 0):
continue
elif(a2 < max_left_red_x):
max_left_red_x = a2
max_left_red_y = b2
### End of Image Processing ######Fri, 12 Jun 2015 12:16:37 -0500http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/Comment by theodore for <p>I am trying to find the leftmost and rightmost edge of a binary image like the one attached. I am currently doing this by using a for loop starting from the center of the white portion and working towards a direction until I get to my first black pixel. This works great except it is very slow. Is there any other ways to do this, but in a faster way. Thanks all who reply.</p>
<p><img alt="Binary Image" src="/upfiles/14341292027477476.png"/> </p>
<pre><code>#### Start Image Processing #########
hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
lower_red = np.array([0,100,100])
upper_red = np.array([10,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
mat=cv.GetMat(cv.fromarray(mask))
moments=cv.Moments(mat)
yc_red= moments.m01/moments.m00
xc_red=moments.m10/moments.m00
width, height = cv.GetSize(mat)
max_right_red_x = 0
for a in range(int(round(xc_red)), width, 3):
for b in range (0, height, 3):
if(mat[b,a] == 0):
continue
elif(a > max_right_red_x):
max_right_red_x = a
max_right_red_y = b
max_left_red_x = width
for a2 in range(int(round(xc_red)), 0, -3):
for b2 in range (0, height, 3):
if(mat[b2,a2] == 0):
continue
elif(a2 < max_left_red_x):
max_left_red_x = a2
max_left_red_y = b2
### End of Image Processing ######
</code></pre>
http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?comment=64035#post-id-64035I do not know if you can find the [LineIterator()](http://docs.opencv.org/modules/core/doc/drawing_functions.html#lineiterator) function in python, but that I think would fit your problem. You can have a look also in this [asnswer](http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?answer=56254#post-id-56254) from @berak in order to see how you can use it.Fri, 12 Jun 2015 13:28:00 -0500http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?comment=64035#post-id-64035Answer by Guyygarty for <p>I am trying to find the leftmost and rightmost edge of a binary image like the one attached. I am currently doing this by using a for loop starting from the center of the white portion and working towards a direction until I get to my first black pixel. This works great except it is very slow. Is there any other ways to do this, but in a faster way. Thanks all who reply.</p>
<p><img alt="Binary Image" src="/upfiles/14341292027477476.png"/> </p>
<pre><code>#### Start Image Processing #########
hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
lower_red = np.array([0,100,100])
upper_red = np.array([10,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
mat=cv.GetMat(cv.fromarray(mask))
moments=cv.Moments(mat)
yc_red= moments.m01/moments.m00
xc_red=moments.m10/moments.m00
width, height = cv.GetSize(mat)
max_right_red_x = 0
for a in range(int(round(xc_red)), width, 3):
for b in range (0, height, 3):
if(mat[b,a] == 0):
continue
elif(a > max_right_red_x):
max_right_red_x = a
max_right_red_y = b
max_left_red_x = width
for a2 in range(int(round(xc_red)), 0, -3):
for b2 in range (0, height, 3):
if(mat[b2,a2] == 0):
continue
elif(a2 < max_left_red_x):
max_left_red_x = a2
max_left_red_y = b2
### End of Image Processing ######
</code></pre>
http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?answer=64040#post-id-64040An alternative solution to `findcontours` is to project the image on the one axis using [reduce](http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#reduce).
This will give you a vector which has zeros only in the columns(or rows) for which all pixels in the original image are also zeros.
guy
Fri, 12 Jun 2015 16:17:45 -0500http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?answer=64040#post-id-64040Comment by theodore for <p>An alternative solution to <code>findcontours</code> is to project the image on the one axis using <a href="http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#reduce">reduce</a>.
This will give you a vector which has zeros only in the columns(or rows) for which all pixels in the original image are also zeros.
guy</p>
http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?comment=64041#post-id-64041nice thought ;-)Fri, 12 Jun 2015 18:50:57 -0500http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?comment=64041#post-id-64041Answer by unxnut for <p>I am trying to find the leftmost and rightmost edge of a binary image like the one attached. I am currently doing this by using a for loop starting from the center of the white portion and working towards a direction until I get to my first black pixel. This works great except it is very slow. Is there any other ways to do this, but in a faster way. Thanks all who reply.</p>
<p><img alt="Binary Image" src="/upfiles/14341292027477476.png"/> </p>
<pre><code>#### Start Image Processing #########
hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
lower_red = np.array([0,100,100])
upper_red = np.array([10,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
mat=cv.GetMat(cv.fromarray(mask))
moments=cv.Moments(mat)
yc_red= moments.m01/moments.m00
xc_red=moments.m10/moments.m00
width, height = cv.GetSize(mat)
max_right_red_x = 0
for a in range(int(round(xc_red)), width, 3):
for b in range (0, height, 3):
if(mat[b,a] == 0):
continue
elif(a > max_right_red_x):
max_right_red_x = a
max_right_red_y = b
max_left_red_x = width
for a2 in range(int(round(xc_red)), 0, -3):
for b2 in range (0, height, 3):
if(mat[b2,a2] == 0):
continue
elif(a2 < max_left_red_x):
max_left_red_x = a2
max_left_red_y = b2
### End of Image Processing ######
</code></pre>
http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?answer=64033#post-id-64033You can use `findContours` to get the contours of your image. For each contour, you can look at the bounding box to find the top left and bottom right pixel locations. Iterating over the contours should give you the leftmost and rightmost edge locations in the image. Fri, 12 Jun 2015 13:11:04 -0500http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?answer=64033#post-id-64033Answer by jonlwowski012 for <p>I am trying to find the leftmost and rightmost edge of a binary image like the one attached. I am currently doing this by using a for loop starting from the center of the white portion and working towards a direction until I get to my first black pixel. This works great except it is very slow. Is there any other ways to do this, but in a faster way. Thanks all who reply.</p>
<p><img alt="Binary Image" src="/upfiles/14341292027477476.png"/> </p>
<pre><code>#### Start Image Processing #########
hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
lower_red = np.array([0,100,100])
upper_red = np.array([10,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
mat=cv.GetMat(cv.fromarray(mask))
moments=cv.Moments(mat)
yc_red= moments.m01/moments.m00
xc_red=moments.m10/moments.m00
width, height = cv.GetSize(mat)
max_right_red_x = 0
for a in range(int(round(xc_red)), width, 3):
for b in range (0, height, 3):
if(mat[b,a] == 0):
continue
elif(a > max_right_red_x):
max_right_red_x = a
max_right_red_y = b
max_left_red_x = width
for a2 in range(int(round(xc_red)), 0, -3):
for b2 in range (0, height, 3):
if(mat[b2,a2] == 0):
continue
elif(a2 < max_left_red_x):
max_left_red_x = a2
max_left_red_y = b2
### End of Image Processing ######
</code></pre>
http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?answer=64432#post-id-64432I found out how to do it. I just followed this website's tutorials.
http://opencvpython.blogspot.com/2012/06/contours-3-extraction.html Thu, 18 Jun 2015 12:22:50 -0500http://answers.opencv.org/question/64026/finding-leftmost-and-rightmost-edge-of-binary-image/?answer=64432#post-id-64432