Skip to content
Advertisement

opencv.js perspective transform

I’m trying to use opencv.js to find a document in a provided image (detect edges, apply perspective transform, etc.

I’ve got a reasonable set of code that (occasionally) detects edges of a document and grabs the bounding box for that. However, I’m struggling to do the perspective transform steps. There are some helpers for this (not in JS) here and here.

Unfortunately I’m getting stuck on something simple. I can find the matching Mat that has 4 edges. Displaying that shows it to be accurate. However, I have no idea how to get some simple X/Y info out of that Mat. I thought minMaxLoc() would be a good option, but I keep getting an error passing in my matching Mat. Any idea why I can draw foundContour and get bounding box info from it, but I can’t call minMaxLoc on it?

Code:

JavaScript

The last line above results in a runtime error (Uncaught (in promise): 6402256 - Exception catching is disabled). I can run minMaxLoc() on other Mat objects.

Advertisement

Answer

For anyone else looking to do this in OpenCV.JS, what I commented above seems to still be accurate. The contour found can’t be used with minMaxLoc, but the X/Y data can be pulled out of data32S[]. That should be all that’s needed to do this perspective transform. Some code is below.

JavaScript

For reference, here is the class definition I was using for SortableContour. The code above is meant as a guide, not as something that can run on its own, however.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement