1 | initial version |
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.
Output mask image:
2 | No.2 Revision |
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 _, 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.
Output mask image:
3 | No.3 Revision |
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.
Output mask image:
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: