2015-01-21 11:49:31 +08:00
|
|
|
/**
|
|
|
|
* @brief Sample code showing how to segment overlapping objects using Laplacian filtering, in addition to Watershed and Distance Transformation
|
|
|
|
* @author OpenCV Team
|
|
|
|
*/
|
|
|
|
|
2018-02-09 18:24:18 +08:00
|
|
|
#include <opencv2/core.hpp>
|
|
|
|
#include <opencv2/imgproc.hpp>
|
|
|
|
#include <opencv2/highgui.hpp>
|
2015-01-21 11:49:31 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
int main(int argc, char *argv[])
|
2015-01-21 11:49:31 +08:00
|
|
|
{
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [load_image]
|
2015-01-21 11:49:31 +08:00
|
|
|
// Load the image
|
2018-06-28 00:48:32 +08:00
|
|
|
CommandLineParser parser( argc, argv, "{@input | ../data/cards.png | input image}" );
|
|
|
|
Mat src = imread( parser.get<String>( "@input" ) );
|
|
|
|
if( src.empty() )
|
|
|
|
{
|
|
|
|
cout << "Could not open or find the image!\n" << endl;
|
|
|
|
cout << "Usage: " << argv[0] << " <Input image>" << endl;
|
2015-01-21 11:49:31 +08:00
|
|
|
return -1;
|
2018-06-28 00:48:32 +08:00
|
|
|
}
|
2015-01-21 11:49:31 +08:00
|
|
|
|
|
|
|
// Show source image
|
|
|
|
imshow("Source Image", src);
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [load_image]
|
2015-01-21 11:49:31 +08:00
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [black_bg]
|
2015-01-21 11:49:31 +08:00
|
|
|
// Change the background from white to black, since that will help later to extract
|
|
|
|
// better results during the use of Distance Transform
|
2018-06-28 00:48:32 +08:00
|
|
|
for ( int i = 0; i < src.rows; i++ ) {
|
|
|
|
for ( int j = 0; j < src.cols; j++ ) {
|
|
|
|
if ( src.at<Vec3b>(i, j) == Vec3b(255,255,255) )
|
|
|
|
{
|
|
|
|
src.at<Vec3b>(i, j)[0] = 0;
|
|
|
|
src.at<Vec3b>(i, j)[1] = 0;
|
|
|
|
src.at<Vec3b>(i, j)[2] = 0;
|
|
|
|
}
|
2015-01-21 11:49:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show output image
|
|
|
|
imshow("Black Background Image", src);
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [black_bg]
|
2015-01-21 11:49:31 +08:00
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [sharp]
|
|
|
|
// Create a kernel that we will use to sharpen our image
|
2015-01-21 11:49:31 +08:00
|
|
|
Mat kernel = (Mat_<float>(3,3) <<
|
2018-06-28 00:48:32 +08:00
|
|
|
1, 1, 1,
|
|
|
|
1, -8, 1,
|
|
|
|
1, 1, 1); // an approximation of second derivative, a quite strong kernel
|
2015-01-21 11:49:31 +08:00
|
|
|
|
|
|
|
// do the laplacian filtering as it is
|
|
|
|
// well, we need to convert everything in something more deeper then CV_8U
|
|
|
|
// because the kernel has some negative values,
|
|
|
|
// and we can expect in general to have a Laplacian image with negative values
|
|
|
|
// BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
|
|
|
|
// so the possible negative number will be truncated
|
|
|
|
Mat imgLaplacian;
|
2018-06-28 00:48:32 +08:00
|
|
|
filter2D(src, imgLaplacian, CV_32F, kernel);
|
|
|
|
Mat sharp;
|
2015-01-21 11:49:31 +08:00
|
|
|
src.convertTo(sharp, CV_32F);
|
|
|
|
Mat imgResult = sharp - imgLaplacian;
|
|
|
|
|
|
|
|
// convert back to 8bits gray scale
|
|
|
|
imgResult.convertTo(imgResult, CV_8UC3);
|
|
|
|
imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
|
|
|
|
|
|
|
|
// imshow( "Laplace Filtered Image", imgLaplacian );
|
|
|
|
imshow( "New Sharped Image", imgResult );
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [sharp]
|
2015-01-21 11:49:31 +08:00
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [bin]
|
2015-01-21 11:49:31 +08:00
|
|
|
// Create binary image from source image
|
|
|
|
Mat bw;
|
2018-06-28 00:48:32 +08:00
|
|
|
cvtColor(imgResult, bw, COLOR_BGR2GRAY);
|
2017-12-31 19:13:13 +08:00
|
|
|
threshold(bw, bw, 40, 255, THRESH_BINARY | THRESH_OTSU);
|
2015-01-21 11:49:31 +08:00
|
|
|
imshow("Binary Image", bw);
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [bin]
|
2015-01-21 11:49:31 +08:00
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [dist]
|
2015-01-21 11:49:31 +08:00
|
|
|
// Perform the distance transform algorithm
|
|
|
|
Mat dist;
|
2017-12-31 19:13:13 +08:00
|
|
|
distanceTransform(bw, dist, DIST_L2, 3);
|
2015-01-21 11:49:31 +08:00
|
|
|
|
|
|
|
// Normalize the distance image for range = {0.0, 1.0}
|
|
|
|
// so we can visualize and threshold it
|
2018-06-28 00:48:32 +08:00
|
|
|
normalize(dist, dist, 0, 1.0, NORM_MINMAX);
|
2015-01-21 11:49:31 +08:00
|
|
|
imshow("Distance Transform Image", dist);
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [dist]
|
2015-01-21 11:49:31 +08:00
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [peaks]
|
2015-01-21 11:49:31 +08:00
|
|
|
// Threshold to obtain the peaks
|
|
|
|
// This will be the markers for the foreground objects
|
2018-06-28 00:48:32 +08:00
|
|
|
threshold(dist, dist, 0.4, 1.0, THRESH_BINARY);
|
2015-01-21 11:49:31 +08:00
|
|
|
|
|
|
|
// Dilate a bit the dist image
|
2018-06-28 00:48:32 +08:00
|
|
|
Mat kernel1 = Mat::ones(3, 3, CV_8U);
|
2015-01-21 11:49:31 +08:00
|
|
|
dilate(dist, dist, kernel1);
|
|
|
|
imshow("Peaks", dist);
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [peaks]
|
2015-01-21 11:49:31 +08:00
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [seeds]
|
2015-01-21 11:49:31 +08:00
|
|
|
// Create the CV_8U version of the distance image
|
|
|
|
// It is needed for findContours()
|
|
|
|
Mat dist_8u;
|
|
|
|
dist.convertTo(dist_8u, CV_8U);
|
|
|
|
|
|
|
|
// Find total markers
|
|
|
|
vector<vector<Point> > contours;
|
2017-12-31 19:13:13 +08:00
|
|
|
findContours(dist_8u, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
|
2015-01-21 11:49:31 +08:00
|
|
|
|
|
|
|
// Create the marker image for the watershed algorithm
|
2018-06-28 00:48:32 +08:00
|
|
|
Mat markers = Mat::zeros(dist.size(), CV_32S);
|
2015-01-21 11:49:31 +08:00
|
|
|
|
|
|
|
// Draw the foreground markers
|
|
|
|
for (size_t i = 0; i < contours.size(); i++)
|
2018-06-28 00:48:32 +08:00
|
|
|
{
|
|
|
|
drawContours(markers, contours, static_cast<int>(i), Scalar(static_cast<int>(i)+1), -1);
|
|
|
|
}
|
2015-01-21 11:49:31 +08:00
|
|
|
|
|
|
|
// Draw the background marker
|
2018-06-28 00:48:32 +08:00
|
|
|
circle(markers, Point(5,5), 3, Scalar(255), -1);
|
2015-01-21 11:49:31 +08:00
|
|
|
imshow("Markers", markers*10000);
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [seeds]
|
2015-01-21 11:49:31 +08:00
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [watershed]
|
2015-01-21 11:49:31 +08:00
|
|
|
// Perform the watershed algorithm
|
2018-06-28 00:48:32 +08:00
|
|
|
watershed(imgResult, markers);
|
2015-01-21 11:49:31 +08:00
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
Mat mark;
|
|
|
|
markers.convertTo(mark, CV_8U);
|
2015-01-21 11:49:31 +08:00
|
|
|
bitwise_not(mark, mark);
|
2018-06-28 00:48:32 +08:00
|
|
|
// imshow("Markers_v2", mark); // uncomment this if you want to see how the mark
|
|
|
|
// image looks like at that point
|
2015-01-21 11:49:31 +08:00
|
|
|
|
|
|
|
// Generate random colors
|
|
|
|
vector<Vec3b> colors;
|
|
|
|
for (size_t i = 0; i < contours.size(); i++)
|
|
|
|
{
|
2018-06-28 00:48:32 +08:00
|
|
|
int b = theRNG().uniform(0, 256);
|
|
|
|
int g = theRNG().uniform(0, 256);
|
|
|
|
int r = theRNG().uniform(0, 256);
|
2015-01-21 11:49:31 +08:00
|
|
|
|
|
|
|
colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the result image
|
|
|
|
Mat dst = Mat::zeros(markers.size(), CV_8UC3);
|
|
|
|
|
|
|
|
// Fill labeled objects with random colors
|
|
|
|
for (int i = 0; i < markers.rows; i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < markers.cols; j++)
|
|
|
|
{
|
|
|
|
int index = markers.at<int>(i,j);
|
|
|
|
if (index > 0 && index <= static_cast<int>(contours.size()))
|
2018-06-28 00:48:32 +08:00
|
|
|
{
|
2015-01-21 11:49:31 +08:00
|
|
|
dst.at<Vec3b>(i,j) = colors[index-1];
|
2018-06-28 00:48:32 +08:00
|
|
|
}
|
2015-01-21 11:49:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visualize the final image
|
|
|
|
imshow("Final Result", dst);
|
2018-06-28 00:48:32 +08:00
|
|
|
//! [watershed]
|
2015-01-21 11:49:31 +08:00
|
|
|
|
2018-06-28 00:48:32 +08:00
|
|
|
waitKey();
|
2015-01-21 11:49:31 +08:00
|
|
|
return 0;
|
2017-12-31 19:13:13 +08:00
|
|
|
}
|