Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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);

Thanks!

Btw. I can post the full method if it helps

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

image description

Thanks!

Btw. I can post the full method if it helps