Region growth method implemented by QT

Source: Internet
Author: User

Int Threshold = door; // threshold

Int Height = image. Height (); // Image Height
Int width = image. Width (); // width
    QImage image2 = image;
    for(int i = 0;i<width;i++)
        for(int j = 0;j
Image2.setpixel (I, j, qrgb (, 0); // initialize to 0

// A two-dimensional array represents the eight-neighbor coordinate of the center pixel. The first column is the offset of the X direction, and the second column is the offset of the Y direction.
    int direction[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
// Define the stack. Assume that the number of pixels into the stack is about 1/3 of the total number of pixels in the image.
    int  max=height*width;
    QPoint stack[max];
// Stack top pointer
    int top;
// The currently processing vertex and pop-up Vertex
    QPoint currentPoint,popPoint;
// Loop variable, traversing the one-dimensional subscript of the array
    int k;
// Mark the variable
    int label;
    int x,y;
    x=curpo.x();
    y=curpo.y();
// Temporary Variable
    int temp1,temp2;
// Record the gray value of the seed Pixel
    temp1 = QRgb2Rgb(image.pixel(x,y));
// Mark the given seed point as 255 and add it to the stack
    image2.setPixel(x,y,qRgb(255,255,255));
Top = 0; // stack grows up
    stack[top].setX(x);
    stack[top].setY(y);
// Stack
    while(top>-1&&top<max)
    {
// Pop up the top element of the stack, which has been grown
       popPoint.setX(stack[top].x());
       popPoint.setY(stack[top].y());
       top--;
// Check whether any growing pixel exists around the pop-up Pixel
       for(k=0;k<8;k++)
       {
          currentPoint.setX(popPoint.x()+direction[k][0]);
          currentPoint.setY(popPoint.y()+direction[k][1]);
// Skip if the observed vertex is not in the image
          if((currentPoint.x()<0)||(currentPoint.x()>width-1)||
             (currentPoint.y()<0)||(currentPoint.y()>height-1))
             continue;
// This vertex ID
          label = QRgb2Rgb(image2.pixel(currentPoint.x(),currentPoint.y()));
// There are not yet grown points around the pop-up point
             if(label==0)
          {
             temp2 = QRgb2Rgb(image.pixel(currentPoint.x(),currentPoint.y()));
// If the difference between the pixel gray value and the seed point gray value is less than the specified threshold
// It is considered to be similar and processed in the stack.
             if(abs(temp1-temp2)<threshold)
             {
// Set the growth mark to 255
                 image2.setPixel(currentPoint.x(),currentPoint.y(),qRgb(255,255,255));
                top++;
                stack[top].setX(currentPoint.x());
                stack[top].setY(currentPoint.y());
             }
          }
       }
    }
    image3=image2;
    image2=image2.scaled(ui->label_3->width(),ui->label_3->height(),Qt::IgnoreAspectRatio);
    ui->label_3->setPixmap(QPixmap::fromImage(image2));

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.