Saeed Masoomi
Saeed Masoomi

Reputation: 1834

Find biggest contour for shadow detection

I have this problem in image processing and I couldn't find an algorithm to perform well under this condition.It's so simple to understand but I don't know how to implement it in ‍‍OpenCV or in Matlab, so any algorithm or function in one of them (MATLAB or opencv) is helpful.

1 . lets suppose that an image and background of scene is like the below

enter image description here

2 . We apply an edge detector to image and my current image will be like the below picture.

enter image description here

now my Problem is that how we can find the biggest contour or area like the below in edge image?

enter image description here


If you want original picture the original picture is enter image description here

and in matlab you can get edge image by below codes.

clc
clear

img = imread('1.png');                 % read Image
gray = rgb2gray(img);                  % Convert RGB to gray-scale
edgeImage = edge(gray,'canny',0.09);   % apply canny to gray-scale image
imshow(edgeImage)                      % Display result in figure(MATLAB)

In OpenCV you can use below code

#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
   Mat img = imread("1.png");

   Mat gray;
   cvtColor(img,           //BGR form image
            gray,          //Mat for gray(destination)
            CV_BGR2GRAY);  //type of transform(in here BGR->GRay)

   Mat edgeImage;
   Canny(gray,       //Input Array
         edgeImage,  //Output Array
         40,         // Lower threshold
         120);        //Upper threshold

   namedWindow("Edge-Image");       //create a window for display image
   imshow("Edge-Image",edgeImage);  //Display edgeImage in window that in before line create
   waitKey(0);                      //stick display window and wait for any key

   return 0;

}

Upvotes: 1

Views: 463

Answers (2)

LowPolyCorgi
LowPolyCorgi

Reputation: 5171

Here is a solution in Matlab using imdilate to close the contours and regionprops to get the closed objects area:

% Your code to start
img = imread('Image.png');             % read Image
gray = rgb2gray(img);                  % Convert RGB to gray-scale
edgeImage = edge(gray,'canny',0.09);   % apply canny to gray-scale image

% First dilate to close contours
BW = imdilate(edgeImage, strel('disk',4,8));

% Then find the regions
R = regionprops(~BW, {'Area', 'PixelIdxList'});

% Find the second biggest region (the biggest is the background)
[~, I] = sort([R(:).Area], 'descend');
Mask = zeros(size(img));
Mask(R(I(2)).PixelIdxList) = 1;

% Display
clf
imshow(Mask)

An the result is:

The result: Mask

Best,

Upvotes: 1

KjMag
KjMag

Reputation: 2770

First close the contour with morphological closing since you can't find it now as it is not really a distinct contour, but a part of the larger one.

After closing, just use the findContours() function and use its output to get the area of each contour and eventually find the maximum one by using the contourArea() function.

Upvotes: 1

Related Questions