justadudewhohacks / justadudewhohacks/opencv4nodejs

Problem with Mat::WarpPerspective

Open
#662 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
5.1k
Forks
826
PR merge metrics
No merged PRs in 30d

Description

Hello, I am tryng to translate this code from python/C+ to Js version, the code is for match picture of a paper document with the template of the same document to later extract data with some OCR functions. This code use Homografy on the proccess but during the code I am doing some thing wrong, because the type of varieble is allways wrong for this function FindHomography().

The link with a those code in Python and C+ :
https://www.learnopencv.com/image-alignment-feature-based-using-opencv-c-python/

My code:

```
const cv = require('../');

var maxFeatures = 400
var bestMatches = 50
var goodMatchPercent = 0.15

const documentMatching = ({img1,img2, detector, matchFunc}) =>{

//change to grayscale
const im1Gray = img1.cvtColor( cv.COLOR_BGR2GRAY)
const im2Gray = img2.cvtColor( cv.COLOR_BGR2GRAY)

//# Detect ORB features and compute descriptors.

// detect keypoints
const keyPoints1 = detector.detect(img1);
const keyPoints2 = detector.detect(img2);

// compute feature descriptors
const descriptors1 = detector.compute(img1, keyPoints1);
const descriptors2 = detector.compute(img2, keyPoints2);

// match the feature descriptors
const matches = matchFunc(descriptors1, descriptors2);

// Remove not so good matches
const numGoodMatches = matches.length*goodMatchPercent;
matches.splice(matches[0]+numGoodMatches,matches[matches.length-1])


// only keep good matches
const bestN = bestMatches;
const points = matches.sort(
(match1, match2) => match1.distance - match2.distance
).slice(0, bestN);

// Draw top matches
const imgMatches = cv.drawMatches( img1, img2, keyPoints1, keyPoints2, points);

//cv.imshowWait('ImagesMatches',imgMatches);

// find cutPoints
points1 = []
points2 = []

for( i = 0; i < matches.length; i++ )
{
points1.push( keyPoints1[ matches[i].queryIdx ].pt );
points2.push( keyPoints2[ matches[i].trainIdx ].pt );
}

// Find homography
h = cv.findHomography( points1, points2 );

// Use homography to warp image


const finalIMG = img1.warpPerspective(img2,h);

cv.imshowWait('Images',finalIMG);
}

const img1 = cv.imread('../data/IAtemplate.png');
const img2 = cv.imread('../data/IAphoto.png');

const doc = documentMatching({img1,img2,detector: new cv.ORBDetector(),
matchFunc: cv.matchBruteForceHamming})

```

# Error Message
Mat::WarpPerspective - OpenCV Error: ((M0.type() == CV_32F || M0.type() == CV_64F) && M0.rows == 3 && M0.cols == 3) in cv::warpPerspective

*console.log('img2') = Mat {
step: 1260,
elemSize: 3,
sizes: [ 555, 420 ],
empty: 0,
depth: 0,
dims: 2,
channels: 3,
type: 16,
cols: 420,
rows: 555 }

*console.log('h') = { homography:
Mat {
step: 24,
elemSize: 8,
sizes: [ 3, 3 ],
empty: 0,
depth: 6,
dims: 2,
channels: 1,
type: 6,
cols: 3,
rows: 3 },
mask:
Mat {
step: 1,
elemSize: 1,
sizes: [ 472, 1 ],
empty: 0,
depth: 0,
dims: 2,
channels: 1,
type: 0,
cols: 1,
rows: 472 } }

OpenCV 4.0 - Core
OpenCV 4.0 - Contrib
Windows 10

Thanks for you time!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.