'Opencv4 src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4
I am having some problems with cv::getPerspectiveTransform in opencv4 Here is the code
typedef std::vector<std::pair<int, int>> vectorpair;
cv::Mat Perspective::fourPointTransformation(cv::Mat &img, vectorpair pts) {
//obtain a consistent order of the point
vectorpair points = orderPoints(std::move(pts));
std::pair<int, int> tl = points[0];
std::pair<int, int> tr = points[1];
std::pair<int, int> br = points[2];
std::pair<int, int> bl = points[3];
// compute the width of the new image, which will be the
// maximum distance between bottom-right and bottom-left
// x-coordiates or the top-right and top-left x-coordinates
int widthA = sqrt((br.first - bl.first) ^ 2 + (br.second - bl.second) ^ 2);
int widthB = sqrt((tr.first - tl.first) ^ 2 + (tr.second - tl.second) ^ 2);
int maxWidth = std::max(widthA, widthB);
// compute the height of the new image, which will be the
// maximum distance between the top-right and bottom-right
// y-coordinates or the top-left and bottom-left y-coordinates
int heightA = sqrt(((tr.first - br.first) ^ 2) + ((tr.second - br.second) ^ 2));
int heightB = sqrt(((tl.first - bl.first) ^ 2) + ((tl.second - bl.second) ^ 2));
int maxHeight = std::max(heightA, heightB);
vectorpair data = {{0, 0},
{maxWidth - 1, 0},
{maxWidth - 1, maxHeight - 1},
{0, maxHeight - 1}};
cv::Mat distance(4, 2, CV_32FC2, &data);
cv::Mat M(4,2, CV_32FC1);
M = cv::getPerspectiveTransform(points, distance);
cv::Mat warped;
cv::warpPerspective(img, warped, M, cv::Size_<int>(maxWidth, maxHeight));
return warped;
}
the main file
//
// Created by jonsnow on 07/02/22.
//
#include "iostream"
#include "../include/imutils/perspective.h"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
typedef std::vector<std::pair<int, int>> vectorpair;
int main() {
vectorpair pair = {{101, 185},
{393, 151},
{479, 323},
{187, 441}};
cv::Mat img = cv::imread("../demo_images/notecard.png", cv::IMREAD_COLOR);
if (img.empty()) {
std::cout << "Could not read the image: " << std::endl;
return 1;
}
cv::Mat imgwarped = Perspective::fourPointTransformation(img, pair);
imshow("Display window",imgwarped );
cv::waitKey(0); // Wait for a keystroke in the window
}
this is the error I am getting
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.5) /build/opencv/src/opencv-4.5.5/modules/imgproc/src/imgwarp.cpp:3392: error: (-215:Assertion failed) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'getPerspectiveTransform'
[1] 9821 abort (core dumped) ./output
The command I have used is
c++ perspective.cpp main.cpp -o output `pkg-config --cflags --libs opencv4` && ./output
The error seems to be caused by conflicting data types however I have already changed the datatype to CV_32F
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
