V-S Watershed Transformation Algorithm

Source: Internet
Author: User

V-S Watershed Transformation Algorithm

// V-S version of the Watershed algorithm bool Watershed: WatershedTransformOfVS (IplImage * src, int & basinCount) {# define MASK-2/* initial value of a threshold level marked as the gradient layer currently being processed */# define INIT-1/* initial value of imgOut */# define WSHED 0/* value of the pixels belonging to the watersheds */# define INIT_DIST 0/* initial value of imgDist *//*. -- input: imgIn, decimal image; -- output: image of the labeled watersheds ;. I Nitializations: -- Value INIT is assigned to each pixel of imgOut: -- current_label = 0 -- current_dist: integer variable -- imgDist: work image (of distances), initialized to 0; */if (src = NULL) {return false;} const int width = src-> width; const int height = src-> height; const int widthStep = src-> widthStep; const int channels = src-> nChannels; uchar * imgIn = (uchar *) src-> imageData; if (channels! = 1) // src must be a single-channel image {return false ;} // V-S Watershed Transformation Algorithm // initialization process // BASIN_NUM * imgOut = new BASIN_NUM [height * widthStep]; // output image marked with dam above for (int I = 0; I 
  
VPointsGradient; // an ordered set of all vertices with gradient values for (int col = 0; col 
   
VPointsCurGrad; // set of all vertices in the current gradient level int iPoint = vPointsGradient. size ()-1; // subscript Of The traversal point nCurGradient = vPointsGradient [iPoint]. gradient; // minimum gradient value for (; iPoint> = 0; iPoint --) // traverse all gradient points from small to large {if (nCurGradient = vPointsGradient [iPoint]. gradient) // obtain the gradient point set of each layer {vPointsCurGrad. push_back (vPointsGradient [iPoint]); continue;} // >>> process pixels on the layer with a gradient level of nGradient /// expand each basin // POINT_GRADIENT p; // POINT_GRADIENT p _; // queue of the field of the current vertex p
   
    
Required oque; for (int iPoint = 0; iPoint <vPointsCurGrad. size (); iPoint ++) // traverse each point of the current gradient level {p = POINT_GRADIENT (vPointsCurGrad [iPoint]. x, vPointsCurGrad [iPoint]. y, vPointsCurGrad [iPoint]. gradient); // The current vertex imgOut [p. x + p. y * widthStep] = MASK; bool flag = false; // If a basin or watershed exists in the p domain, the flag is truefor (int iDirect = 0; iDirect <8; iDirect ++) {// p _ domain point p _ of the current vertex p _. x = p. x + Direct [iDirect] [0]; p _. y = p. y + Direct [I Direct] [1]; if (p _. x <0 | p _. x> = width | p _. y <0 | p _. y> = height) {continue;} p _. gradient = (int) imgIn [p _. x + p _. y * widthStep]; if (imgOut [p _. x + p _. y * widthStep]> 0 | imgOut [p _. x + p _. y * widthStep] = WSHED) {flag = true; break ;}} if (flag) {/* p is the point closest to the basin or watershed */imgDist [p. x + p. y * widthStep] = 1; stored oque. push (p) ;}}/* In this case, the gradient value of 1 from the edge of each basin is n + 1 points, where n is the gradient value of the basin edge */POINT_GRADIENT FictPoint = POINT_GRADIENT (0, 0,256); // fictitious_pixel virtual pixel, mark point currentDist = 1 at the end of a queue traversal; writable oque. push (fictPoint); while (1) // repeat indefinitely {p = foreign oque. front (); mirror oque. pop (); if (p. gradient = fictPoint. gradient) // if p = fictPoint {/* bfs finished a circle */if (else oque. empty () break; else {kerberoque. push (fictPoint); currentDist + = 1; // the point in the next circle of bfs. The distance between these points increases by one unit. front (); mirror oque. pop () ;}/// >>> For (int iDirect = 0; iDirect <8; iDirect ++) {// p _ domain point of the current vertex p _. x = p. x + Direct [iDirect] [0]; p _. y = p. y + Direct [iDirect] [1]; if (p _. x <0 | p _. x> = width | p _. y <0 | p _. y> = height) {continue;} p _. gradient = (int) imgIn [p _. x + p _. y * widthStep]; if (imgDist [p _. x + p _. y * widthStep] <currentDist & (imgOut [p _. x + p _. y * widthStep]> 0 | imgOut [p _. x + p _. y * widthStep] = WSHED) {/* If p _ is Point marked as basin or watershed */if (imgOut [p _. x + p _. y * widthStep]> 0) // if p _ is already a basin {if (imgOut [p. x + p. y * widthStep] = MASK | imgOut [p. x + p. y * widthStep] = WSHED) {/* p is an unlabeled point or watershed */imgOut [p. x + p. y * widthStep] = imgOut [p _. x + p _. y * widthStep];} else if (imgOut [p. x + p. y * widthStep]! = ImgOut [p _. x + p _. y * widthStep]) {/* p and p _ belong to different basins */imgOut [p. x + p. y * widthStep] = WSHED; // dam construction} else // if p _ is a point on the watershed {if (imgOut [p. x + p. y * widthStep] = MASK) {/* If p is not marked */imgOut [p. x + p. y * widthStep] = WSHED; // watershed of the self-built basin} else if (imgOut [p _. x + p _. y * widthStep] = MASK & imgDist [p _. x + p _. y * widthStep] = 0) {imgDist [p _. x + p _. y * widthStep] = currentDist + 1; stored oque. push (p _) ;}/// <} //// Find a new pole cell // for (int iPoint = 0; iPoint <vPointsCurGrad. size (); iPoint ++) // traverse each point of the current gradient {p = POINT_GRADIENT (vPointsCurGrad [iPoint]. x, vPointsCurGrad [iPoint]. y, vPointsCurGrad [iPoint]. gradient); // The current vertex imgDist [p. x + p. y * widthStep] = 0;/* The correlation distance of p points is reset to 0 */if (imgOut [p. x + p. y * widthStep] = MASK) {currentNum + = 1; // Add a basin called fifoQue. push (p); imgOut [p. x + p. y * widthStep] = currentNum; // POINT of the expanded pole Cell _ GRADIENT pt; // while (! Invalid oque. empty () {pt = Invalid oque. front (); mirror oque. pop (); POINT_GRADIENT pt _; // domain point of pt at the time point for (int iDirect = 0; iDirect <8; iDirect ++) {pt _. x = pt. x + Direct [iDirect] [0]; pt _. y = pt. y + Direct [iDirect] [1]; if (pt _. x <0 | pt _. x> = width | pt _. y <0 | pt _. y> = height) {continue;} pt _. gradient = (int) imgIn [pt _. x + pt _. y * widthStep]; if (pt _. gradient! = Pt. gradient) {continue;} if (imgOut [pt _. x + pt _. y * widthStep] = MASK) {kerberoque. push (pt _); imgOut [pt _. x + pt _. y * widthStep] = currentNum ;}}}/// <nCurGradient = vPointsGradient [iPoint]. gradient; vPointsCurGrad. clear (); vPointsCurGrad. push_back (vPointsGradient [iPoint]);} basinCount = currentNum; for (int col = 0; col 
  
 

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.