opencv--Watershed Algorithm

Source: Internet
Author: User

Watershed algorithm is a mathematical morphology segmentation method based on topological theory, whose basic idea is to think of images as topological landforms on geodetic, the gray value of each pixel in the image is the elevation of the point, each local minimum and its affected area is called the basin, and the boundary of the basin is a watershed.

The general watershed algorithm causes excessive segmentation of faint edges, noise in the image, and subtle grayscale changes in the surface of the object.

The following is a sample program for the watershed algorithm.

WatershedSegmenter.h

#if!defined WATERSHS#defineWatershs#include<opencv2/core/core.hpp>#include<opencv2/imgproc/imgproc.hpp>classWatershedsegmenter {Private: Mat markers;  Public:       //set up marker map      voidSetmarkers (Constmat&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 ()Mat Process (ConstMat &image) {        //Apply Watershedwatershed (image,markers); returnmarkers; }      //returning results as an imageMat getsegmentation () {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 watershed in image formMat getwatersheds () {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

Main.cpp

#include <iostream>#include<opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/calib3d/calib3d.hpp>#include"WatershedSegmenter.h"using namespacestd;using namespaceCV;intMain () {//set the video read-in, the number in parentheses is the choice of the camera, usually comes with 0Videocapture Capture (0); if(!capture.isopened ()) {cout<<"can not open the video"<<Endl; return-1;    } Mat frame;    Mat Binimage; BOOLStop =false;  while(!stop) {        //read in the video frame, convert the color space, and split the channelCapture >>frame;        Cvtcolor (frame, binimage, Cv_bgr2gray); Threshold (Binimage, Binimage, -,255, thresh_binary); //expanded Imagedilate (Binimage, Binimage, Mat ()); /*Watershed Algorithm*/        //*************************************************************Mat FG; //corrosion Image 6 timesErode (Binimage, FG, Mat (), Point (-1, -1),6); //Identify image pixels without objectsMat BG; //expanded image 6 timesDilate (binimage, BG, Mat (), Point (-1, -1),6); Imshow ("BG", BG); //perform a fixed threshold operationThreshold (BG, BG,1, -, THRESH_BINARY_INV); //Show Markers ImageMat Markers (Binimage.size (), cv_8u, Scalar (0)); Markers= FG +BG; Imshow ("Markers Image", markers);        Watershedsegmenter Segmenter;        Segmenter.setmarkers (markers);        Segmenter.process (frame); Imshow ("segmentation", Segmenter.getsegmentation ()); Imshow ("watersheds", Segmenter.getwatersheds ()); } waitkey (0); return 0;}

opencv--Watershed Algorithm

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.