Create image from original image using contour points in OpenCV?
I need to find squares in an image and then create a separate image of the detected square. So far I can detect a square and get its outline in terms of four points.
Problem: When I create an image using ROI, I get the background also where the square is missing. I want to delete this area and only want to keep the area associated with the square.
source to share
You want to use a mask!
Create a black and white single-band image (CV_U8C1). The white part is the desired area (your ROI, ROI) from the original image.
Vector "ROI_Vertices" contains ROI vertices. Place a polygon around it (ROI_Poly), then fill it with white.
Then 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 giving me all the information I needed two weeks ago when I had the same problem!
source to share