C + + implementation of watershed segmentation algorithm (watershed segmentation)

Source: Internet
Author: User

Operating Environment: ubuntu16.04+qt+opencv2.4.13

Reference Link: http://blog.csdn.net/u010741471/article/details/45193521

Watershedsegmenter.h

#ifndef Watershedsegmenter#defineWatershedsegmenter#include<opencv2/core/core.hpp>#include<opencv2/imgproc/imgproc.hpp>classWatershedsegmenter {Private:      //used to denote a marker (figure)Cv::mat markers;  Public:       //set up marker map      voidSetmarkers (Constcv::mat&markerimage) {        //the input parameter of the watershed () must be a 32-bit signed token, so the conversion is done firstMarkerimage.convertto (markers,cv_32s); }      //Executive watershed ()Cv::mat Process (ConstCv::mat &image) {        //Apply Watershedcv::watershed (image,markers); returnmarkers; }      //returning results as an imageCv::mat getsegmentation () {Cv::mat tmp; //saturation is performed from 32S to 8U (0-255), so all pixels above 255 are copied as 255Markers.convertto (tmp,cv_8u);//        returntmp; }      //return the watershed as an image (I understand the split line)Cv::mat getwatersheds () {Cv::mat tmp; //after you set the tag image, that is, the setmarkers (), the pixels of the edge are assigned a value of-1, others are represented by a positive integer//The following conversion allows the edge pixels to become -1*255+255=0, that is, black, the rest of the overflow, the assignment is 255, that is, white. Markers.convertto (tmp,cv_8u,255,255); returntmp; }};#endif //Watershedsegmenter

Main.cpp

#include <iostream>#include<opencv2/core/core.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/highgui/highgui.hpp>#include"Watershedsegmenter.h"using namespacestd;using namespaceCV;intMain () {//Read input Image artworkMat image= Imread ("/home/osksh/skin_c/dulani_anuradha4.jpg"); if(!image.data)return 0; //Display the imageNamedwindow ("Original Image"); Imshow ("Original Image", image);    Mat BINARY,FG,BG;    Cvtcolor (Image,binary,cv_rgba2gray); //Display the binary imageNamedwindow ("Binary Image"); Imshow ("Binary Image", binary); //the foreground is obtained from a two value image. Corrosion. Remove noise and small objectsErode (Binary,fg,cv::mat (), CV::P oint (-1,-1),6); //Display the foreground imageNamedwindow ("Foreground Image"); Imshow ("Foreground Image", FG); //expanded binary graph to get background (only grass, no woods)Dilate (Binary,bg,cv::mat (), CV::P oint (-1,-1),6); Threshold (BG,BG, Max, -, CV::THRESH_BINARY_INV); //the last parameter represents Ifsrc>1,dst=0,else dst=128. This makes the background all Gray (+)//Display the background imageNamedwindow ("Background Image"); Imshow ("Background Image", BG); //Show Markers ImageMat Markers (Binary.size (), Cv_8u,cv::scalar (0)); Markers= FG+BG;//use overloaded operator +Namedwindow ("Markers"); Imshow ("Markers", markers); //Create Watershed Segmentation ObjectWatershedsegmenter Segmenter; //Set Markers and processsegmenter.setmarkers (markers);    Segmenter.process (image); //Display Segmentation ResultNamedwindow ("segmentation"); Imshow ("segmentation", Segmenter.getsegmentation ()); //Display WatershedsNamedwindow ("watersheds"); Imshow ("watersheds", Segmenter.getwatersheds ()); Waitkey (0); return 0;}

C + + implementation of watershed segmentation algorithm (watershed segmentation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.