APPLICATION OF HOMOGRAPHY CONVERSION TO IMAGE IN OpenCV

Asked 1 years ago, Updated 1 years ago, 107 views

I would like to apply the homography matrix to the input image by cv::warperspectiveTransform().
A memory access violation occurs.
I tried many things referring to OpenCV warpperspective, but it didn't work.
Could you give me some advice?

 cv::Mat src_image=cv::imread("image.png");// Input image

cv::Mat src_corner=cv::Mat(4,1,CV_32FC2); // Four corners of the input image
cv::Mat dst_corner=cv::Mat(4,1,CV_32FC2); // Four corners of the output image 
for(inti=0;i<2;++i){
        for(int j=0;j<2;++j){
                src_corner.at<cv::Vec2f>(i*2+j,0)[0] = src_image.cols*j;
                src_corner.at<cv::Vec2f>(i*2+j,0)[1] = src_image.rows*i;
        }
}

// Homography matrix homography has been calculated appropriately in advance

// Homography conversion of four corners of input image
cv:: perspectiveTransform(src_corner, dst_corner, homography);

// HOMOGRAPHY TRANSFORMATION CORRECTS POINT HAVING NEGATIVE COORDIN
cv::Math;
cv::Rect br;
{
        br=cv::boundingRect(dst_corner);
        cv::Mat offset_mat=cv::Mat::eye(3,3,CV_32FC1);
        offset_mat.at<float>(0,2) -=br.x;
        offset_mat.at<float>(1,2) -=br.y;
        h = offset_mat*homography;
}

// homography conversion
cv::Mat dst_image;
cv::warpPerspective(src_image, dst_image, h, br.rect);

c++ c++11 opencv

2022-09-29 22:37

1 Answers

Now that I've solved myself, I'll save it for those who want to see it later.

The problem was that the data type of the input image was CV_8UC1, and it worked fine when I changed it to CV_32FC1.

// Data type conversion: CV_8UC1-> CV_32FC1
cv::Mat src_uchar=cv::imread("image.png"); // Input image(CV_8UC1)
cv::Mat src_image;
src_uchar.convertTo(src_image, CV_32FC1); // type conversion (->CV_32FC1)

In addition, it was necessary to reserve memory for the image dst_image after the homography conversion. I still have doubts about the size, but it's working.

// Homography Conversion
cv::Mat dst_image=cv::Mat(br.br().y,br.br().x,CV_32FC1);// Free Memory
cv::warpPerspective(src_image, dst_image, h, br.rect);


2022-09-29 22:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.