Ask Your Question

Finding Leftmost and Rightmost Edge of Binary Image

asked 2015-06-12 12:16:37 -0500

jonlwowski012 gravatar 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

#### 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)
yc_red= moments.m01/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):
        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):
        elif(a2 < max_left_red_x):
           max_left_red_x = a2
           max_left_red_y = b2

### End of Image Processing ######
edit retag flag offensive close merge delete


I do not know if you can find the LineIterator() function in python, but that I think would fit your problem. You can have a look also in this asnswer from @berak in order to see how you can use it.

theodore gravatar imagetheodore ( 2015-06-12 13:28:00 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2015-06-12 16:17:45 -0500

Guyygarty gravatar image

An alternative solution to findcontours is to project the image on the one axis using 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

edit flag offensive delete link more


nice thought ;-)

theodore gravatar imagetheodore ( 2015-06-12 18:50:57 -0500 )edit

answered 2015-06-12 13:11:04 -0500

unxnut gravatar image

You 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.

edit flag offensive delete link more

answered 2015-06-18 12:22:50 -0500

jonlwowski012 gravatar image

I found out how to do it. I just followed this website's tutorials.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools



Asked: 2015-06-12 12:16:37 -0500

Seen: 1,764 times

Last updated: Jun 18 '15