imextendedmin(D,n)

时间:2019-02-21
本文章向大家介绍imextendedmin(D,n),主要包括imextendedmin(D,n)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 

版本0:

int main(int, char** argv)
{
    // Load the image
    Mat src = imread("/home/jumper/Ore_try/white_belt_position/watershed/src.jpg");
    // Check if everything was fine
    if (!src.data)
        return -1;

    // Create a kernel that we will use for accuting/sharpening our image
    Mat kernel = (Mat_<float>(3,3) <<
            1,  1, 1,
            1, -8, 1,
            1,  1, 1); // an approximation of second derivative, a quite strong kernel
    // 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;
    Mat sharp = src; // copy source image to another temporary one
    filter2D(sharp, imgLaplacian, CV_32F, kernel);
    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);
    imwrite( "/home/jumper/Ore_try/white_belt_position/watershed/imgLaplacian.jpg", imgLaplacian );
    imwrite( "/home/jumper/Ore_try/white_belt_position/watershed/imgResult.jpg", imgResult );

    src = imgResult; // copy back
    // Create binary image from source image
    Mat bw;
    cvtColor(src, bw, CV_BGR2GRAY);
    threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
    imwrite("/home/jumper/Ore_try/white_belt_position/watershed/bw.jpg", bw);
    // Perform the distance transform algorithm
    Mat dist;
    distanceTransform(bw, dist, CV_DIST_L2, 3);
    // Normalize the distance image for range = {0.0, 1.0}
    // so we can visualize and threshold it
    Mat distshow;
    cv::convertScaleAbs(dist,distshow,50,0);
    imwrite("/home/jumper/Ore_try/white_belt_position/watershed/dist.jpg", distshow);
    normalize(dist, dist, 0, 1., NORM_MINMAX);
    //Threshold to obtain the peaks
    //This will be the markers for the foreground objects
    threshold(dist, dist, .35, 1., CV_THRESH_BINARY);

    // Dilate a bit the dist image
    Mat kernel1 = Mat::ones(3, 3, CV_8UC1);
    dilate(dist, dist, kernel1);
    Mat distshowpeak;
	cv::convertScaleAbs(dist,distshowpeak,30,0);
	imwrite("/home/jumper/Ore_try/white_belt_position/watershed/Peaks.jpg", distshowpeak);
    // 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;
    findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    // Create the marker image for the watershed algorithm
    Mat markers = Mat::zeros(dist.size(), CV_32SC1);
    // Draw the foreground markers
    for (size_t i = 0; i < contours.size(); i++)
        drawContours(markers, contours, static_cast<int>(i), Scalar::all(static_cast<int>(i)+1), -1);
    Mat markersshow;
	cv::convertScaleAbs(markers,markersshow,30,0);
    imwrite("/home/jumper/Ore_try/white_belt_position/watershed/Markers.jpg", markersshow);
    // Draw the background marker
    circle(markers, Point(5,5), 3, CV_RGB(255,255,255), -1);
    Mat markersshow2;
	cv::convertScaleAbs(markers,markersshow2,30,0);
	imwrite("/home/jumper/Ore_try/white_belt_position/watershed/Markers2.jpg", markersshow2);
    watershed(src, markers);

    for(int r=0;r!=markers.rows;r++)
	{
		for(int c=0;c!=markers.cols;c++)
		{
			if((markers.ptr<int>(r)[c])<0)
			{
				bw.ptr<uchar>(r)[c]=0;
			}
		}
	}
    imwrite("/home/jumper/Ore_try/white_belt_position/watershed/result.jpg", bw);

//    bitwise_not(mark, mark);
//    imwrite("/home/jumper/Ore_try/white_belt_position/watershed/Markers_v2.jpg", mark);
//    // image looks like at that point
//    // Generate random colors
//    vector<Vec3b> colors;
//    for (size_t i = 0; i < contours.size(); i++)
//    {
//        int b = theRNG().uniform(0, 255);
//        int g = theRNG().uniform(0, 255);
//        int r = theRNG().uniform(0, 255);
//        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()))
//                dst.at<Vec3b>(i,j) = colors[index-1];
//            else
//                dst.at<Vec3b>(i,j) = Vec3b(0,0,0);
//        }
//    }
//    // Visualize the final image
//    imwrite("/home/jumper/Ore_try/white_belt_position/watershed/FinalResult.jpg", dst);

    return 0;
}