I'm working on a project to process marble slab images like the one below by using OpenCV 3.3.
Several more samples with different marble textures and sizes I'm working on can be found on https://1drv.ms/f/s!AjoScZ1lKToFheM6wmamv45R7zHwaQ
The requirements are:
- Separate the marble slab from the background and remove the background (fill with white color) so only the slab is shown.
- Calculate the area of the slab (the distance from the camera to the marble slab and the parameters of the lens are known)
The strategy I am using is: 1) find contour of the marble slab, 2) remove parts not within the contour, 3) get the area size of the contour, 4) calculate its physical area.
The contour of the slab is shown in the picture below in red (this one was done by hand).
I tried several ways to find contours of the slab in the image but failed to achieve a satisfying result because of the complex background and the rich texture of the marble.
Processing logic I'm using is: convert the image to gray and blur it and use Canny to find edges, then use findContours to find contours, the following is the code:
Mat img = imread('test2.jpg', 1);
Mat gray, edge;
cvtColor(img, gray, COLOR_BGR2GRAY);
blur(gray, gray, Size(3, 3));
Canny(gray, edge, 50, 200, 3);
vector<vector<Point> > contours;
vector<Vec4i> lines;
findContours(edge, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
cout << "Number of contours detected: " << contours.size() << endl;
vector<Vec4i> hierarchy;
for (int i = 0; i < contours.size(); i++)
{
drawContours(img, contours, i, Scalar(0, 0, 255), 1, 8, hierarchy, 0, Point());
}
imshow("Output", img);
I tried to tweak the blur and Canny parameters for dozens of combinations and still failed. I also tried to use HoughLinesP to find the edge of the slab with several groups of different parameters but alsofailed to do so.
I'm a newbie to computer vision, the questions I have now are:
- Am I going towards a wrong way or strategy to find the slab contour? Could there be any better algorithms or combinations? Or do I need to focus on tweaking parameters of Canny/findContours/HoughLinesP algorithms?
- Is this kind of image really difficult to process due to the complex background?
I'm open to any suggestions that may help me finish my goal. Thank you in advance.