Detection of red rectangle and environment lighting
Hi, I'm in the process of writing some code which detects the largest red rectangle in an image. I'm using the code below, but the detection is very prone to changes in the environment lighting, if it works at all.
Does anyone have any ideas about how to make the detection more robust and less prone to light changes? Should I use adaptiveThreshold for this or something else?
After running findContours below I find the largest rectangle by checking for area size and number of vertices.
// convert the frame to HSV
Imgproc.cvtColor(frame, hsvImage, Imgproc.COLOR_BGR2HSV);
// Limit color range to reds in the image
Mat redMask1 = new Mat();
Mat redMask2 = new Mat();
Mat redMaskf = new Mat();
Core.inRange(hsvImage, new Scalar(0, 70, 50), new Scalar(10, 255, 255), redMask1);
Core.inRange(hsvImage, new Scalar(170, 70, 50), new Scalar(180, 255, 255), redMask2);
Core.bitwise_or(redMask1, redMask2, redMaskf);
Imgproc.medianBlur(redMaskf, blurredImage, 9);
Imgproc.Canny(blurredImage, edges, 300, 600, 5, true);
Imgproc.findContours(edges, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
I'm using this image as a reference for testing
Thanks!
Btw. I can post the full method if it helps
could you post some sample images
Sure, I added a image to my post. Thanks
I suggest after
Core.inRange
blur the image and then usethreshold
instead ofbitwise_or
andCanny
, then usefindContours
. I don't know any theoretical explanation of why it is better just saying it based on my own experience. When I had to find an object with a specific color with Canny it was impossible but with threshold, it worked pretty well.