Create an image from source image using Contour points in OpenCV?

前端 未结 1 1532
时光取名叫无心
时光取名叫无心 2021-01-16 02:44

I have to find squares in an image and then create a separate image of the detected square. So far, i am able to detect the square and get its contour in terms of four point

相关标签:
1条回答
  • 2021-01-16 03:12

    You want to use a mask!

    Create a black and white single-channel image (CV_U8C1). The white part is the desired area (your region of interest, ROI) from the original image.

    The vector "ROI_Vertices" contains the vertices of the ROI. Fit a polygon around it (ROI_Poly), then fill it with white.

    Afterwards use CopyTo to subtract your ROI from the image.

    // ROI by creating mask for your trapecoid
    // Create black image with the same size as the original    
    Mat mask = cvCreateMat(480, 640, CV_8UC1); 
    for(int i=0; i<mask.cols; i++)
        for(int j=0; j<mask.rows; j++)
            mask.at<uchar>(Point(i,j)) = 0;
    
    // Create Polygon from vertices
    vector<Point> ROI_Poly;
    approxPolyDP(ROI_Vertices, ROI_Poly, 1.0, true);
    
    // Fill polygon white
    fillConvexPoly(mask, &ROI_Poly[0], ROI_Poly.size(), 255, 8, 0);                 
    
    // Create new image for result storage
    Mat resImage = cvCreateMat(480, 640, CV_8UC3);
    
    // Cut out ROI and store it in resImage 
    image->copyTo(resImage, mask);    
    

    Thanks to this guy for providing me with all the information i needed two weeks ago, when i had the same problem!

    0 讨论(0)
提交回复
热议问题