首页 文章

使用OpenCV中的Contour点从源图像创建图像?

提问于
浏览
1

我必须在图像中找到正方形,然后创建检测到的正方形的单独图像 . 到目前为止,我能够检测到正方形,并根据四个点得到它的轮廓 .

Problem: 当我使用ROI创建图像时,我也得到了不存在正方形的背景 . 我想删除该区域,并希望仅保留与square相关的区域 .

1 回答

  • 2

    你想用面具!

    创建黑白单通道图像(CV_U8C1) . 白色部分是原始图像中所需的区域(您感兴趣的区域,ROI) .

    矢量“ROI_Vertices”包含ROI的顶点 . 在它周围安装一个多边形(ROI_Poly),然后用白色填充它 .

    然后使用CopyTo从图像中减去ROI .

    // 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);
    

    感谢this guy为我提供了两周前我需要的所有信息,当时我遇到了同样的问题!

相关问题