Magic wand tool-Introduction to regiongrow Algorithm

Source: Internet
Author: User
The magic wand tool in PS is very easy to use and is a very common tool in image processing. It is now an important member of my c ++ toolbox, I will gradually introduce my toolbox to you in the future. Core of magic wand toolsAlgorithmIt is the regiongrow region growth method. Its concept is very simple. First, select a seed point on the image to be processed, and then take this point as the starting point to form a region from around. At first, the growth area only has the seed point, and then constantly merges the surrounding points into the growth area. The condition is that the difference between the value of this point and the value of the boundary point of the growth area is less than the threshold. When the growth area cannot continue to expand, the algorithm stops.  

  Algorithm Description: The idea of the regional growth law is well understood,CodeImplementation is difficult for beginners. For the pixels that meet the condition, the function will push them one by one into the end of the queue, and then extract them one by one from the header of the queue to form a growth area. M is a node register used to record whether each pixel has been processed. Start and end are used to record the beginning and end of the queue. When start = end, all pixels are processed and the function ends.   Parameter description: SRC: the input single-channel image. DST: the output single-channel image, which is the same as the input size and must be opened in advance. Seedx, seedy: seedy Coordinate Threshold: Tolerance Flag: 0/1 indicates that the search method is 8/4 neighbor.
 Struct  Node {  Int  X;  Int  Y; Node * Next ;}; Void Mytreasurebox: regiongrow ( Const Iplimage * SRC, iplimage * DST, Int Seedx, Int Seedy, Int Threshold, Bool  Flag ){  If (! SRC | Src-> nchannels! = 1 ) Return  ;  Int Width = Src-> Width; Int Height = Src-> Height;  Int Srcwidthstep = Src-> Widthstep; uchar * IMG = (uchar *) Src-> Imagedata;  //  Growth Zone  Cvzero (DST );  //  Indicates whether each pixel has been computed. Iplimage * m = cvcreateimage (cvsize (width, height ), 8 , 1  ); Int Mwidthstep = m-> Widthstep; cvzero (m); m -> Imagedata [seedy * mwidthstep + seedx] = 1 ; //  The position of the seed point is 1, and the other position is 0.  Cvscalar cur = Cv_rgb ( 255 , 255 , 255  ); Cvset2d (DST, seedy, seedx, cur );  //  Both ends of the queue      Int Start =0  ;  Int End = 1  ; Node * Queue = New  Node; queue -> X = Seedx; queue -> Y = Seedy; queue -> Next = NULL; Node * First = Queue; Node * Last = Queue;  While (End-Start>0  ){  Int X = first-> X;  Int Y = first-> Y; uchar Pixel = (Uchar) IMG [y * srcwidthstep + X];  For ( Int YY =- 1 ; YY <= 1 ; YY ++ ){  For (Int Xx =- 1 ; XX <= 1 ; XX ++ ){  If  (FLAG)  If (ABS (yy )&& ABS (XX ))  Continue  ;  Int Cx = x + XX;  Int Cy = Y +YY;  If (CX> = 0 & CX <width & Cy> = 0 & Cy < Height ){  If (ABS (IMG [CY * srcwidthstep + cx]-pixel) <= threshold & M-> imagedata [CY * mwidthstep + cx]! = 1  ) {Node * Node = New  Node -> X =CX; Node -> Y = Cy; Node -> Next = Null; End ++ ; Last -> Next = Node; last = Node; m -> Imagedata [CY * mwidthstep + cx] = 1  ; Cvset2d (DST, Cy, CX, cur) ;}}} Node * Temp = First; first = First-> Next; Delete temp; Start ++ ;} Cvreleaseimage ( & M );} 

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.