Ask Your Question
0

drawing a rectangle around a color as shown?

asked 2018-10-10 05:08:01 -0600

rohan__007 gravatar image

updated 2019-12-04 10:00:45 -0600

supra56 gravatar image

I want to detect the color, which I have done with the following code:

import cv2 as cv
import numpy as np

cap = cv.VideoCapture(0)

while(1):

    _, frame = cap.read()
    # Convert BGR to HSV
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    # define range of blue color in HSV
    lower_blue = np.array([100,55,55])
    upper_blue = np.array([105,255,255])
    # Threshold the HSV image to get only blue colors
    mask = cv.inRange (hsv, lower_blue, upper_blue)
    # Bitwise-AND mask and original image
    res = cv.bitwise_and(frame,frame, mask= mask)
    cv.imshow('frame',frame)
    cv.imshow('mask',mask)
    cv.imshow('res',res)
    k = cv.waitKey(5) & 0xFF
    if k == 27:
        break
cv.destroyAllWindows()

but want to know how to draw a rectangle around it?

edit retag flag offensive close merge delete

Comments

somewhat better, than your last question. however, please spare us duplicate ones to the same topic, thank. you !

berak gravatar imageberak ( 2018-10-10 05:13:52 -0600 )edit

and what you really need, is a tour through the tutorials

berak gravatar imageberak ( 2018-10-10 05:15:08 -0600 )edit

Esp this one

"...Contours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity..."

Sound good to me. Should work.

holger gravatar imageholger ( 2018-10-10 05:31:25 -0600 )edit

I just noticed you already extracted the bbox(ignore comment above) - maybe all you asking for is how to draw rectangles at all? For drawing functions look here

holger gravatar imageholger ( 2018-10-10 05:38:47 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-03-16 09:12:06 -0600

supra56 gravatar image

I am using raspberry pi using linux. Btw, I used hsv colour, Fortunately, on your side, you may used your hsv colour.

import numpy as np
import cv2  

cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    # define range of blue color in HSV
    lower_blue = np.array([100,50,50])
    upper_blue = np.array([130,255,255])
    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange (hsv, lower_blue, upper_blue)
    bluecnts = cv2.findContours(mask.copy(),
                              cv2.RETR_EXTERNAL,
                              cv2.CHAIN_APPROX_SIMPLE)[-2]

    if len(bluecnts)>0:
        blue_area = max(bluecnts, key=cv2.contourArea)
        (xg,yg,wg,hg) = cv2.boundingRect(blue_area)
        cv2.rectangle(frame,(xg,yg),(xg+wg, yg+hg),(0,255,0),2)

    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)

    k = cv2.waitKey(5) 
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

Output: blue_rect

edit flag offensive delete link more

Comments

Hi may I know that what is the meaning of the line "key=cv2.contourArea" in the max() function. Thank you for reading this post.

MingCheng gravatar imageMingCheng ( 2019-12-12 02:25:39 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-10-10 05:08:01 -0600

Seen: 19,488 times

Last updated: Dec 04 '19