sweng123
sweng123

Reputation: 41

OpenCV how to detect a specific color in a frame (inRange function)

I am able to use the code below to find anything blue within a frame:

How To Detect Red Color In OpenCV Python?

However I want to modify the code to look for a very specific color within a video that I have. I read in a frame from the video file, and convert the frame to HSV, and then print the HSV values at a particular element (that contains the color I want to detect):

print("hsv[x][y]: {}".format(hsv[x][y]))

"hsv" is what I get after I convert the frame from BGR to HSV using cvtColor(). The print command above gives me: hsv[x][y]: [108 27 207]

I then define my lower and upper HSV values, and pass that to the inRange() function:

lowerHSV = np.array([107,26,206])

upperHSV = np.array([109,28,208])

maskHSV = cv2.inRange(hsv, lowerHSV, upperHSV)

I display maskHSV, but it doesn't seem to identify the item that contains that color. I try to expand the lowerHSV and upperHSV bounds, but that doesn't seem to work either. I try something something similar using BGR but that doesn't appear to work.

The thing I'm trying to identify can best be described as a lemon-lime sports drink bottle...

Any suggestions would be appreciated.

=====================================================

The complete python code I am running is shown below, along with some relevant images...

import cv2
import numpy as np 
import time

video_capture = cv2.VideoCapture("conveyorBeltShort.wmv") 

xloc = 460

yloc = 60

dCounter = 0

while(1):

    dCounter += 1

    grabbed, frame = video_capture.read()

    if grabbed == False:
        break

    time.sleep(2)

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lowerBGR = np.array([200,190,180])
    upperBGR = np.array([210,200,190])

    # HSV [108 27 209] observation
    lowerHSV = np.array([105,24,206])
    upperHSV = np.array([111,30,212])

    maskBGR = cv2.inRange(frame, lowerBGR, upperBGR)
    maskHSV = cv2.inRange(hsv, lowerHSV, upperHSV)

    cv2.putText(hsv, "HSV:" + str(hsv[xloc][yloc]),(100,280), 
        cv2.FONT_HERSHEY_SIMPLEX, 3,(0,0,0),8,cv2.LINE_AA)
    cv2.putText(frame, "BGR:" + str(frame[xloc][yloc]),(100,480), 
        cv2.FONT_HERSHEY_SIMPLEX, 3,(0,0,0),8,cv2.LINE_AA)
    cv2.rectangle(frame, (0, 0), (xloc-1, yloc-1), (255, 0, 0), 2)
    cv2.rectangle(hsv, (0, 0), (xloc-1, yloc-1), (255, 0, 0), 2)

    cv2.imwrite("maskHSV-%d.jpg" % dCounter, maskHSV)
    cv2.imwrite("maskBGR-%d.jpg" % dCounter, maskBGR)
    cv2.imwrite("hsv-%d.jpg" % dCounter, hsv) 
    cv2.imwrite("frame-%d.jpg" % dCounter, frame)

    cv2.imshow('frame',frame)
    cv2.imshow('hsv',hsv)
    cv2.imshow('maskHSV',maskHSV)    
    cv2.imshow('maskBGR',maskBGR)

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

cv2.destroyAllWindows()
video_capture.release()

=====================================================

first image is "frame-6.jpg"

second image is "hsv-6.jpg"

third image is "maskHSV-6.jpg"

fourth image is "maskBGR-6.jpg"

maskHSV-6.jpg and maskBGR-6.jpg do not appear to show the lemon-lime bottle on the conveyor belt. I believe I have the lower and upper HSV/BGR limits set correctly...

frame-6.jpg

hsv-6.jpg

maskHSV-6.jpg

maskBGR-6.jpg

Upvotes: 2

Views: 2461

Answers (1)

Matthieu Bippus
Matthieu Bippus

Reputation: 46

I know only C++ for OpenCV, but according to this post you should use img[y][x] or img[x,y] to access pixel value.

Your rgb value are not correct.It should be something like [96,160,165].

Upvotes: 0

Related Questions