OpenCV HSV value changes depending on location on the screen/camera

I'm pretty new to all programming and have been trying to get a grasp on Python (2.7.6), OpenCV, and ROS to be able to use a neural network in a robot.

I am now stuck in an OpenCV (2.4.8) problem with getting the correct HSV-values, in short, it suddenly changes from red (around 5 for Hue) or blue (about 110 for Hue) to green (about 40/50) while I'm only moving the camera/presenting the image on the other side of the screen.

class CV:
    def __init__(self):

        # Initialize object
        self.cap = cv2.VideoCapture(0) # '0' should stay or won't work for me
        self.cap.set(cv2.cv.CV_CAP_PROP_FPS, 0.1)

    def recognize(self):
        while True:
            ret, frame = self.cap.read()
            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

            # Thresholding and contour finding
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            gray_blur = cv2.GaussianBlur(gray, (15, 15), 0)
            thresh = cv2.adaptiveThreshold(gray_blur, 255,
                                           cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                           cv2.THRESH_BINARY_INV, 11, 1)
            contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                                   cv2.CHAIN_APPROX_SIMPLE)

            # Largest contour
            areas = [cv2.contourArea(c) for c in contours]
            max_index = np.argmax(areas)
            cnt = contours[max_index]

            # Find center
            (x, y), radius = cv2.minEnclosingCircle(cnt)
            center = int(x), int(y)
            center_int = list(center)

            # Color recognition
            print hsv[center_int[0], center_int[1]]

            # Show capture
            cv2.imshow('Video', frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        self.cap.release()
        cv2.destroyAllWindows()

    if __name__ == '__main__':

        object = CV()
        object.recognize()

So I am presenting a red or blue circle either at the left or the right of the screen. I am trying to get the HSV value for color recognition from video input. The image is presented on another screen which is facing my webcam.

In my code you can see (or at least what I hope you should see) that I find the largest contour, then the smallest enclosing circle and I get the HSV value from the center (x,y) coordinate. If I present my circle on the left I can find red well, as goes for blue, but for both colors, if I present my circle on the right of the screen the HSV value is totally off in the green range as mentioned above.

If I keep the circle in one place and slowly move my camera from right to left (so the circle in the frame I get from the cam moves from left to right) the HSV value suddenly changes from blue/red to green Hue (with vastly different Saturation and Value values also, but less interested in those) from one pixel to the next.

I think it has something to do with where in the screen I am actually getting my color from but I just cannot find the right debugging tricks to get an answer myself.. I hope one of you can help me out!

EDIT I have printed a circle at the center coordinates of the shape to track where the center coordinates of the minimum enclosing circle are and that seems to be in place. However, I changed the background to blue (from white) and I now find that when the shape is at the right of the screen the HSV value is blue so it seems that the HSV value does not come from the center coordinates, but I have no idea why..


After two days of struggling with this problem I finally found where I went wrong:

The coordinates returned by this:

(x, y), radius = cv2.minEnclosingCircle(cnt)

Are not x and y coordinates but a row and column index. So when I moved my camera or the circle from left to right the HSV values for the center point which I coded as:

print hsv[center_int[0], center_int[1]]

Moved up and down the frame, returning colors from above and below the circle.

I renamed the coordinates like this:

(r, c), radius = cv2.minEnclosingCircle(cnt)
center = int(r), int(c)
center_int = list(center)

and the region of interest is now:

print hsv[center_int[1], center_int[0]]

And all worked out.

链接地址: http://www.djcxy.com/p/35412.html

上一篇: OpenCV Webcam feed在PictureBox visual studio 2015中不显示

下一篇: OpenCV HSV值根据屏幕/相机上的位置而变化