Ask Your Question
0

Drawing a rectangle around the red color region

asked 2020-04-28 11:50:29 -0600

orujino gravatar image

updated 2020-04-28 14:13:38 -0600

Hi. I am trying to draw a rectangle around the biggest red color region in the image. image description

import cv2
import numpy as np

img = cv2.imread("ther.jpg")
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)


mask1 = cv2.inRange(img_hsv, (0,90,50), (5,255,255))
mask2 = cv2.inRange(img_hsv, (175,90,50), (180,255,255))


mask = cv2.bitwise_or(mask1, mask2 )



(_,contours,_) = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
    area = cv2.contourArea(c)
    print(area)
    if(area>800):
        x,y,w,h = cv2.boundingRect(c)
        frame = cv2.rectangle(img,(x,y),(x+w,x+h),(0,0,255),5)
        cv2.imshow(" ",frame)

When I run this code, it doesn't work. Can anyone help me with this? I'm running out of ideas. Many thanks in advance.

edit retag flag offensive close merge delete

Comments

Didn't you add cv2.waitKey() or not?

supra56 gravatar imagesupra56 ( 2020-04-28 14:50:49 -0600 )edit
1

"it doesn't work" doesn't explain anything about the problem. You need to tell in detail how it doesn't work: what you expect and what happens instead.

mvuori gravatar imagemvuori ( 2020-04-28 15:10:35 -0600 )edit

Which red do you wanted to detect? I merely detected red hand, or left or right foot.

supra56 gravatar imagesupra56 ( 2020-04-28 23:28:12 -0600 )edit

Actually, you can't detected face, Merely, hand, left foot and right foot can merely be detected.

supra56 gravatar imagesupra56 ( 2020-04-29 04:09:47 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-29 07:54:39 -0600

supra56 gravatar image

updated 2020-05-02 04:31:58 -0600

I don't know which one you're selected. I am posting both images. in mask image, you can see all 4 red colours.

#!/usr/bin/python37
#OpenCV 4.3.0, Raspberry pi 3B+/4B, Buster v10.
#Date: 29th April, 2020

import numpy as np
import cv2  

frame = cv2.imread('thermail_image.jpg')
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of red color in HSV
lower_red = np.array([0, 10, 120])
upper_red = np.array([15, 255, 255])

mask = cv2.inRange (hsv, lower_red, upper_red)
_, contours, _ = cv2.findContours(mask.copy(),
                           cv2.RETR_TREE,
                           cv2.CHAIN_APPROX_SIMPLE)[-2]

if len(contours) > 0:
    red_area = max(contours, key=cv2.contourArea)
    x, y, w, h = cv2.boundingRect(red_area)
    cv2.rectangle(frame,(x, y),(x+w, y+h),(0, 0, 255), 2)


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

cv2.waitKey(0)

Output: thermail image.

image description

Output mask image:

image description

I'm uncertainly if this help to find person.

import numpy as np
import cv2

image = cv2.imread("heat.jpg")
#hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# red color boundaries [B, G, R]
lower = [1, 0, 20]
upper = [60, 40, 200]

lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")

mask = cv2.inRange(image, lower, upper)
output = cv2.bitwise_and(image, image, mask=mask)
ret,thresh = cv2.threshold(mask, 20, 255, 9)
contours,hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

cv2.imshow("Result", np.hstack([image, output]))
cv2.waitKey(0)

Output: image description

edit flag offensive delete link more

Comments

Code above is run on opencv 3. I just added unpacked cv2.findContours().

supra56 gravatar imagesupra56 ( 2020-04-29 08:18:41 -0600 )edit
1

Thank you so much for your help. Actually, I want to detect the red regions that bigger than a limit in images. Like if area of the red spot is bigger than a limit then draw the rectangle around it. Your code runs pretty fine for the image above. However when I test it for the images that there are 2 or 3 people, it only detects the biggest regions.

orujino gravatar imageorujino ( 2020-04-30 18:43:21 -0600 )edit

Are you doing something like coronavirus? You want to detected head only which had a biggest red region?. I have seen one similar from New Zealand.

supra56 gravatar imagesupra56 ( 2020-04-30 21:52:24 -0600 )edit

No. I want to design a search and rescue robot which will move through rubbles and debris and detect the alive humans by using thermal camera.

orujino gravatar imageorujino ( 2020-05-01 14:26:40 -0600 )edit

I added an extra code, so you can see reds. Actually, I'm trying to get biggest contours. Unfortunately, your image is not accurate. I needed fully red contours.

supra56 gravatar imagesupra56 ( 2020-05-03 06:23:06 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-28 11:50:29 -0600

Seen: 11,095 times

Last updated: May 02 '20