0

Suppose that I have the color image of a person wearing PPE attached. I want to create a bounding box around yellow color in this images as seen in expected output. I tried the code below but can't really understand how to play around the threshold to achieve this.

The lower and upper bound for yellow color is as follows/

yellow_lower = np.array([20, 100, 100])
yellow_upper = np.array([30, 255, 255])

How can I use above color range and create bounding box around these colors?

import cv2
import numpy as np

# read image
img = cv2.imread('PPE.jpeg')

# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# threshold
thresh = cv2.threshold(gray, 20, 255, cv2.THRESH_BINARY)[1]\

# get contour bounding boxes and draw on copy of input
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = contours[0] if len(contours) == 2 else contours[1]
result = img.copy()
for c in contours:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(result, (x, y), (x+w-1, y+h-1), (0, 0, 255), 1)

# view result
cv2.imshow("threshold", thresh)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

PPE Image

Expected Output

enter image description here

Lopez
  • 372
  • 1
  • 15
  • You already have the range, now use it within `cv2.inRange` function as shown `mask_img = cv2.inRange(img, yellow_lower, yellow_upper)` – Jeru Luke Apr 07 '22 at 06:27

0 Answers0