Below is a program that detects object based on DEFINED HSV-COLOR BOUNDARIES. The problem is that when the illumination in the room changes, it affects negatively on the object's color to be detected and leads to mis-detection. So any ideas of how to make the color boundaries change in an adaptiv way so that even when substantial change in illumination occurs it can still detect the target color ??
import imutils
import cv2 as cv
import numpy as np
cap=cv.VideoCapture(0)
out = cv.VideoWriter('output12.mp4', -1, 1, (2448,2048))
kernel1 = np.ones((11,11),np.uint8)
while True:
ret,img=cap.read()
if ret==False:
print('Error reading frame')
break
hsv=cv.cvtColor(img,cv.COLOR_BGR2HSV)
#color-boundaries
lower_red = np.array([125,122,136])
upper_red = np.array([255,255,255])
mask1 = cv.inRange(hsv, lower_red, upper_red)
opening = cv.morphologyEx(mask1, cv.MORPH_OPEN, kernel1)
closing1 = cv.morphologyEx(opening, cv.MORPH_CLOSE, kernel1)
contours = cv.findContours(closing1.copy(), cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
if len(contours) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
c = max(contours, key=cv.contourArea)
print('max_contour_area',c)
(x, y, w, h)=cv.boundingRect(c)
#raw BBox
cv.putText(img,"{}".format('Object detected'), (10,40), cv.FONT_HERSHEY_SIMPLEX,1,(0,255,255,20))
cv.rectangle(img, (x,y), (x+w, y+h), (0,255,0),2)
cv.putText(img,"x={}".format(x),(x,y-10),cv.FONT_HERSHEY_SIMPLEX,0.4,(0,255,0,50))
cv.putText(img,"y={}".format(y),(x+50,y-10),cv.FONT_HERSHEY_SIMPLEX,0.4,(0,255,0,50))
cv.putText(img,"w={}".format(w),(x+100,y-10),cv.FONT_HERSHEY_SIMPLEX,0.4,(0,255,0,50))
cv.putText(img,"h={}".format(h),(x+150,y-10),cv.FONT_HERSHEY_SIMPLEX,0.4,(0,255,0,50))
cv.imshow('img',img)
cv.imshow('mask1',mask1)
cv.imshow('closing5',closing1)
k=cv.cv2.waitKey(40)
if k==27:
break
cap.release()
cv.destroyAllWindows()
Thanks in advance