简体   繁体   中英

Get pixel location of binary image with intensity 255 in python opencv

I want to get the pixel coordinates of the blue dots in an image. To get it, I first converted it to gray scale and use threshold function.

    import numpy as np
    import cv2

    img = cv2.imread("dot.jpg")
    img_g = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret1,th1 = cv2.threshold(img_g,127,255,cv2.THRESH_BINARY_INV)

What to do next if I want to get the pixel location with intensity 255? Please tell if there is some simpler method to do the same.

I don't think this is going to work as you would expect. Usually, in order to get a stable tracking over a shape with a specific color, you do that in RGB/HSV/HSL plane, you could start with HSV which is more robust in terms of lighting.

1-Convert to HSV using cv2.cvtColor()

2-Use cv2.inRagne(blue_lower, blue_upper) to "filter" all un-wanted colors.

Now you have a good-looking binary image with only blue color in it (assuming you have a static background or more filters should be added).

3-Now if you want to detect dots (which is usually more than one pixel) you could try cv2.findContours

4- You can get x,y pixel of contours using many methods(depends on the shape of what you want to detect) like this cv2.boundingRect()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM