How to set ROI in OpenCV?

后端 未结 2 1628
不思量自难忘°
不思量自难忘° 2020-12-01 14:40

I have two images, the first one smaller than the other. I need to copy the second image on the first image. To do so, I need to set the ROI on the first one, copy the secon

相关标签:
2条回答
  • 2020-12-01 15:13

    This is the code I used. I think the comments explain it.

    /* ROI by creating mask for the parallelogram */
    Mat mask = cvCreateMat(480, 640, CV_8UC1);
    // Create black image with the same size as the original
    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> approxedRectangle;
    approxPolyDP(rectangleVertices, approxedRectangle, 1.0, true);
    
    // Fill polygon white
    fillConvexPoly(mask, &approxedRectangle[0], approxedRectangle.size(), 255, 8, 0);                 
    
    // Create new image for result storage
    Mat imageDest = cvCreateMat(480, 640, CV_8UC3);
    
    // Cut out ROI and store it in imageDest
    image->copyTo(imageDest, mask);
    

    I also wrote about this and put some pictures here.

    0 讨论(0)
  • 2020-12-01 15:22

    I think you have something wrong. If the first one is smaller than the other one and you want to copy the second image in the first one, you don't need an ROI. You can just resize the second image in copy it into the first one.

    However if you want to copy the first one in the second one, I think this code should work:

    cv::Rect roi = cv::Rect((img2.cols - img1.cols)/2,(img2.rows - img1.rows)/2,img1.cols,img1.rows);
    
    cv::Mat roiImg;
    roiImg = img2(roi);
    
    img1.copyTo(roiImg);
    
    0 讨论(0)
提交回复
热议问题