0

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

Khaled
  • 555
  • 1
  • 6
  • 26
  • 2
    If you can’t control the illumination, you should at least measure it. If you know how the illumination changes, you know how your colors will change. – Cris Luengo Jun 14 '20 at 22:40
  • Thanks for your comment. In Fact, the change in room light causes change in the HSV lower boundaries values. I've plottet the Value of the HSV in a Histogram and spottet the change. Still the question is: how can i make this change of the lower boundaries ,which is caused due to different lightning conditions,adaptive so that the desired color (e.g green) can still be detected. I can trivially change the lower boundaries of HSV to the new values manually, but this is inefficient as i want the program to do that @CrisLuengo – Khaled Jun 15 '20 at 13:25
  • Yes, I understand that illumination changes cause color changes. You need to measure the illumination and correct for it, so that your colors remain constant. This is the type of card you need to use to correct for the illumination: https://www.amazon.com/Reference-Temperature-Calibration-Photography-Calibrate/dp/B012HMB1N8 This is essential for any task that involves using color if you don't control the illumination. – Cris Luengo Jun 15 '20 at 15:04
  • Thanks for the comment, I've actually seen your link. However, is there a titorial on how to use this card in order to correct the illumination?.@CrisLuengo – Khaled Jul 06 '20 at 15:21
  • Sorry, I didn't pay a whole lot of attention to the product I linked, I was just looking for an image of a color target. A good one like [this](https://www.amazon.com/Datacolor-SCK200-SpyderCHECKR-24/dp/B00LPS46TW/ref=pd_sbs_421_2/131-3579364-8423839?_encoding=UTF8&pd_rd_i=B00LPS46TW&pd_rd_r=685aef47-1d0f-4839-92c8-a609572f7b4d&pd_rd_w=KfCkU&pd_rd_wg=Mq5q9&pf_rd_p=bdc67ba8-ab69-42ee-b8d8-8f5336b36a83&pf_rd_r=FWZX9TPF3EZ987470148&psc=1&refRID=FWZX9TPF3EZ987470148) actually comes with software that corrects the colors in your images. – Cris Luengo Jul 06 '20 at 15:41
  • Well, that's actually a bit expensive. However,Thanks for the reply. As i mentioned above, I'm working with HSV as it's illuminance-invariance is alot better compared to RGB-space. DO you think working with normalized rgb would give better results as HSV?.@CrisLuengo – Khaled Jul 06 '20 at 16:38
  • It does not matter which color space you use. HSV will not help you in any way with this problem. – Cris Luengo Jul 06 '20 at 16:55
  • If you don't want to spend the money to correct for illumination changes, then you must control the illumination so it doesn't change. Pick one of the two. The cheapest solution is to correct only for the white balance. This will get you 80% there, I think. It might be enough, depending on your application. – Cris Luengo Jul 06 '20 at 16:59
  • White balancing is accomplished by placing a white object (e.g. a sheet of paper) in your scene, measuring its color, then dividing all images by the found RGB value. You need to repeat the calibration every time the light changes. Or you can place a white target in the scene and correct each image independently. – Cris Luengo Jul 06 '20 at 16:59
  • Understood. This means for a life video, as light changes in the course of time, the manual white balancing technique must be done again, right? and can we call the white balancing technique is similar to normalizing the image ?.@CrisLuengo – Khaled Jul 06 '20 at 18:09
  • "normalizing" has many different meanings. In machine learning it usually means setting the mean to 0 and the standard deviation to 1, or something similar. White balancing is definitely something totally different. – Cris Luengo Jul 06 '20 at 18:48
  • Thank you so much. I would really appreciate it ,if you could give my question a vote so that i can ask more questions as i've reached the limits. Therefore, i 'm trying to improve the already asked question and you're now the only one responding to me:). @CrisLuengo – Khaled Jul 06 '20 at 19:22

0 Answers0