warpPerspective polygonal region to whole image

921 views
Skip to first unread message

Spectre

unread,
Feb 8, 2012, 10:34:40 AM2/8/12
to android-opencv
Hello everyone,

I'm trying to map polygonal region with 4 vertices to whole screen
after I detect it.
So I have captured screen in Mat mGray, do some preprocessing, find
contour of polygon. Now I want to map that region (inside polygon) to
whole mGray image and display it. I tried to use
getPerspectiveTransform to find transformation matrix like this:

...
Mat polygon = new Mat(4,1,CvType.CV_32FC2);
List<Point> approxPts = new ArrayList<Point>();
org.opencv.utils.Converters.Mat_to_vector_Point(approxContours.get(0),
approxPts);
polygon.put(0, 0, approxPts.get(0).x,approxPts.get(0).y,
approxPts.get(1).x,approxPts.get(1).y,
approxPts.get(2).x,approxPts.get(2).y,
approxPts.get(3).x,approxPts.get(3).y );

Mat screenMat = new Mat(4,1,CvType.CV_32FC2);
screenMat.put(0, 0, 0.0,0.0, mGray.width(),0.0,
mGray.width(),mGray.height(), mGray.width(),mGray.height());

Mat transMat = new Mat(3,3,CvType.CV_32FC2);
transMat = Imgproc.getPerspectiveTransform(polygon, screenMat);
...

now I want to use Imgproc.warpPerspective function but I actualy don't
know what shall I pass as src and dst. I'd be grateful if anyone can
give me an advice.

Thanks in advance.

Rui Marques

unread,
Feb 9, 2012, 11:31:37 AM2/9/12
to android-opencv
Hope this helps somehow:

Imgproc.warpPerspective(source_image, output_mat, transMat,
source_image.width(), source_image.height());

source_image -> If i understood, you want it to be the region of the
mGray with your polygon -> probably a good idea for you to take a look
at opencv's Region of Interest (ROI) functions.

output_mat -> is a new matrix and has to be the same size as
source_image.

I think this will stretch (warp) whatever the "source_image" is, not
sure if it is what you want.

Rui Marques

unread,
Feb 9, 2012, 11:35:29 AM2/9/12
to android-opencv
Let me correct my previous post:

Where you read "source_image.width(), source_image.height()" it should
be output_mat, not source_image.

And output_mat -> is a new matrix and has to be the same TYPE (not
size) as source_image.

Spectre

unread,
Feb 9, 2012, 4:10:40 PM2/9/12
to android-opencv
Thanks, sir.
I already found it out but you are right ... I was wondering what
"source_image" should be and yes it was actually mGray in my case.
After I have called function I have assigned output_mat into mGray and
I was able to display it correctly.

However I want to ask you something more without creating new topic.
Is it possible to do thresholding operation on image after
warpPerspective? When I tried that, it caused error ... maybe it's
matter of CvType.CV_32FC2. If so, What CvType is correct for threshold
operation?

Rui Marques

unread,
Feb 10, 2012, 5:19:12 AM2/10/12
to android-opencv
I don't know enough to give you a full answer but I see no reason why
you can't do that.
After warpPerspective what you get is an image like every other image,
so you should be able to threshold it.
But if you have problems, the type is probably what you have to
correct.

What the docs say about threshold function from Imgproc:

src – Source array (single-channel, 8-bit of 32-bit floating point).
dst – Destination array of the same size and type as src .

By the way, you should also consider adaptiveThreshold function, it
probably gives better results than the regural threshold.
It might be slower but this I'm not sure.

Kirill Kornyakov

unread,
Feb 10, 2012, 5:54:19 AM2/10/12
to android...@googlegroups.com
May be I'm mistaken, but you can try to find the required transformation map for warpPerspective with the findHomography function.

-Kirill
Reply all
Reply to author
Forward
0 new messages