我必须在图像中找到正方形,然后为检测到的正方形创建单独的图像。到目前为止,我能够检测到正方形并根据四个点得到它的轮廓。
问题: 当我使用 ROI 创建图像时,我在没有正方形的地方也得到了背景。我想删除那个区域,只保留与正方形相关的区域。
最佳答案
你要戴口罩!
创建黑白单 channel 图像 (CV_U8C1)。白色部分是原始图像中的所需区域(您感兴趣的区域,ROI)。
向量“ROI_Vertices”包含 ROI 的顶点。在它周围放置一个多边形 (ROI_Poly),然后用白色填充它。
之后使用CopyTo从图像中减去您的投资返回率。
// 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两周前,当我遇到同样的问题时,为我提供了我需要的所有信息!
关于opencv - 使用 OpenCV 中的轮廓点从源图像创建图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22235015/