OpenCV Android Create New Image Using the Edges of the Largest Contour

℡╲_俬逩灬. 提交于 2019-12-03 16:56:20

All you need is to find the corners of this contour. You can use extreme points approach.

You should simply find out the point having minimum x and minimum y (this is your topleft), minimum x maximum y (this is your bottom left), and so on.

In C++, there is a library named algoritm which has min/max methods. For instance min_element will help you to find the point having minimum x or y. Don't forget to include header.

After having your 4 points, you can use perspective transform.

First input your points to this method. The destination should be

Point2f dest[4] = {(0,0),(image.width,0),(0,image.height),(image.height,image.width)}

for your case. The matrix (M) you obtain from this method will transform your points to destination using another method.

Good luck.

Edit: On the second thought; since your contour is not a parallelogram, you have your extreme points as min x = bottomleft, min y = topleft, and so on. Then it is easier to find the minimum element.

That's not the way of using drawContour on a new Mat. This document will help you.

Correct way would be

    Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    ...

    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
    Imgproc.drawContours(imgSource, contours, maxAreaIdx, new Scalar(0, 255, 0), 1);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!