Ask Your Question
0

Mean position of white pixels

asked 2019-04-04 13:45:56 -0600

Is there a built-in function in OpenCV to find the mean position of white pixels along each column? The goal is to draw a horizontal line as the boundary. Here is an approximate example: image description

Many thanks.

edit retag flag offensive close merge delete

Comments

use countNonZero with a.col(i) as parameter

LBerger gravatar imageLBerger ( 2019-04-04 14:11:53 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-04-04 20:17:03 -0600

Tetragramm gravatar image

You want to use cv::moments using a.col(i), where i is the column number.

The output is a structure that contains the moments. The y location of the mass center (the mean) is m01/m00.

edit flag offensive delete link more

Comments

Thanks a lot, Tetragramm. I managed to get the centroid of the image, but am having difficulty to find the center of a single column. When I referenced the column index 0, i.e. M = cv2.moments(edges.col(0)), I get this error:

   'numpy.ndarray' object has no attribute 'col'

I have pasted my moments code that can find the centroid of an image below. Thanks again!

    M = cv2.moments(edges)

    # calculate x,y coordinate of center        
    if M["m00"] != 0:
     cX = int(M["m10"] / M["m00"])
     cY = int(M["m01"] / M["m00"])
    else:
     cX, cY = 0, 0
model_leaf gravatar imagemodel_leaf ( 2019-04-05 10:37:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-04-04 13:45:56 -0600

Seen: 1,236 times

Last updated: Apr 04 '19