Crop rectangle and apply transformation from image?
Hello all,
i want to detect paper sheet from image.i applied medianBlur, Canny ,dilate,threshold,Etc. algorithms to find.i am able to find sheet but dont know how to crop rectangle and apply transformation
this is my code:-
Mat blurred = new Mat();
Imgproc.medianBlur(src, blurred, 9);
// Set up images to use.
Mat gray0 = new Mat(blurred.size(), CvType.CV_8U);
Mat gray = new Mat();
// For Core.mixChannels.
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
List<MatOfPoint2f> rectangles = new ArrayList<MatOfPoint2f>();
List<Mat> sources = new ArrayList<Mat>();
sources.add(blurred);
List<Mat> destinations = new ArrayList<Mat>();
destinations.add(gray0);
// To filter rectangles by their areas.
int srcArea = src.rows() * src.cols();
// Find squares in every color plane of the image.
for (int c = 0; c < 3; c++) {
int[] ch = {c, 0};
MatOfInt fromTo = new MatOfInt(ch);
Core.mixChannels(sources, destinations, fromTo);
// Try several threshold levels.
for (int l = 0; l < N; l++) {
if (l == 0) {
// HACK: Use Canny instead of zero threshold level.
// Canny helps to catch squares with gradient shading.
// NOTE: No kernel size parameters on Java API.
Imgproc.Canny(gray0, gray, 0, CANNY_THRESHOLD);
// Dilate Canny output to remove potential holes between edge segments.
Imgproc.dilate(gray, gray, Mat.ones(new Size(3, 3), 0));
} else {
int threshold = (l + 1) * 255 / N;
Imgproc.threshold(gray0, gray, threshold, 255, Imgproc.THRESH_BINARY);
}
// Find contours and store them all as a list.
Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
int i=0;
for (MatOfPoint contour : contours) {
MatOfPoint2f contourFloat = GeomUtils.toMatOfPointFloat(contour);
double arcLen = Imgproc.arcLength(contourFloat, true) * 0.02;
// Approximate polygonal curves.
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.approxPolyDP(contourFloat, approx, arcLen, true);
if (isRectangle(approx, srcArea)) {
Imgproc.drawContours(src, contours, i, new Scalar(255, 0, 0), 3);
//rectangles.add(approx);
/*Rect rect = Imgproc.boundingRect(contour);
Log.e("Rectangle Finder:-" + i, "Height:-" + rect.height + ", Width:-" + rect.width + " and Area:-" + rect.area() + "\nX:-" + rect.x + ",Y:-" + rect.y);*/
}
i++;
}
}
i want to select only white papersheet.please help me
Thanks in advance
You already calculated the contours of your image. The paper sheet should be the biggest contour. So find the biggest contour in your image and you get your paper sheet. With minAreaRect() you can calculate a rotated rectangle around that contour.
did you see http://answers.opencv.org/question/94...
Yes @Missing i calculated contours but i want find largest contour and crop into new mat.please help me.thanks
Simply iterate over your contours and retrieve the contour area by calling contourAea(contour) and store the biggest one. With minAreaRect()you get the rotated rectangle for that contour. If you want to extract that rectangle take a look at that question here: http://answers.opencv.org/question/49...
Hello @Missing,i have tried with following code:
when i draw largest contour its return this :- http://i.stack.imgur.com/BhNhI.jpg
Hello @Missing.please help me
Could you post your image that you use findContours() on? Your contour follows along the border of the image and not the paper sheet. Seems yout threshold is not good enough.
Means original image? this is original image http://i.stack.imgur.com/ojhGb.jpg. and my code is mentioned in my question
No your image named "gray"...This is the black and white image you use to calculate the contours. This image should just contain the white paper sheet.