0

I have detected a rectangle, which could be out of shape or at an angle, inside one image and now I have rotated and skewed a second image to insert into that detected rectangle in the first image. What is the best way for me to do this? Do I need to use opengl for this?

OneTwo
  • 2,291
  • 6
  • 33
  • 55
  • 1
    Posting some sample image will helpful for the viewers or the same image you posted here http://stackoverflow.com/questions/22645735/what-box-to-use-when-cropping-a-rotated-image-in-opencv – Haris Mar 26 '14 at 15:08

1 Answers1

1

If the image to be inserted is a true rectangle and is well cropped, you can do this with OpenCV only using a combination of getPerspectiveTransform and warpPerspective.

// First estimate the perspective transform to be applied
cv::Mat srcCorners; // Rectangle corners in your second image (image to be inserted)
cv::Mat dstCorners; // Rectangle corners in your first image (image with detected rectangle)
cv::Mat H = cv::getPerspectiveTransform(srcCorners,dstCorners);
// Copy original image and insert new image
cv::Mat composite;
secondImage.copyTo(composite);
cv::warpPerspective(firstImage,composite,H,composite.size(),cv::INTER_CUBIC,cv::BORDER_TRANSPARENT);

If the image to be inserted is not a true rectangle, such as a credit card, you need to use alpha channels, which might require recoding the warpPerspective (not that hard).

BConic
  • 8,750
  • 2
  • 29
  • 55