OpenCV Error: Bad argument (Unknown array type) in cv::cvarrToMat
Good Day Guys, I am detecting a paper by finding out biggest rectangle in an image which is performed by code given below which i found in the link link text. andjava code Image link text But still not able to retrive the found biggest rectangle the the image
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
public class detection {
private static double angle(Point p1, Point p2, Point p0 ) {
double dx1 = p1.x - p0.x;
double dy1 = p1.y - p0.y;
double dx2 = p2.x - p0.x;
double dy2 = p2.y - p0.y;
return (dx1 * dx2 + dy1 * dy2) / Math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}
private static MatOfPoint find(Mat src) throws Exception {
Mat blurred = src.clone();
Imgproc.medianBlur(src, blurred, 9);
Mat gray0 = new Mat(blurred.size(), CvType.CV_8U), gray = new Mat();
List<MatOfPoint> contours = new ArrayList<>();
List<Mat> blurredChannel = new ArrayList<>();
blurredChannel.add(blurred);
List<Mat> gray0Channel = new ArrayList<>();
gray0Channel.add(gray0);
MatOfPoint2f approxCurve;
double maxArea = 0;
int maxId = -1;
for (int c = 0; c < 3; c++) {
int ch[] = {c, 0};
Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));
int thresholdLevel = 1;
for (int t = 0; t < thresholdLevel; t++) {
if (t == 0) {
Imgproc.Canny(gray0, gray, 10, 20, 3, true); // true ?
Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1 ?
} else {
Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, (src.width() + src.height()) / 200, t);
}
Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
for (MatOfPoint contour : contours) {
MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());
double area = Imgproc.contourArea(contour);
approxCurve = new MatOfPoint2f();
Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);
if (approxCurve.total() == 4 && area >= maxArea) {
double maxCosine = 0;
List<Point> curves = approxCurve.toList();
for (int j = 2; j < 5; j++)
{
double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
maxCosine = Math.max(maxCosine, cosine);
}
if (maxCosine < 0.3) {
maxArea = area;
maxId = contours.indexOf(contour);
//contours.set(maxId, getHull(contour));
}
}
}
}
}
if (maxId >= 0) {
return contours.get(maxId);
//Imgproc.drawContours(src, contours, maxId, new Scalar(255, 0, 0, .8), 8);
}
return null;
}
public static void main(String args[]) throws Exception
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
detection d=new detection();
Mat image=Highgui.imread("qwe.jpg");
Mat image1=new Mat();
MatOfPoint contour = d.find(image);
System.out.println(contour.size());
List<MatOfPoint> contours = new ArrayList<>();
contours.add(contour);
for (int i = 0; i < contours.size(); i++) {
Imgproc.drawContours(image1, contours, 0, new Scalar(255, 255, 255), -1);
}
Highgui.imwrite("debug_image.jpg", image1);
//Highgui.imwrite("a.jpg", contour);
/* MatOfPoint2f approxCurve = new MatOfPoint2f();
//Imgproc.drawContours(image, contours, Core.FILLED, new Scalar(255 ...
can you at least try to find out where in your "wall of code" the error happens ?
@berak Getting the following error OpenCV Error: Bad argument (Unknown array type) in cv::cvarrToMat, file ........\opencv\modules\core\src\matrix.cpp, line 698 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ........\opencv\modules\core\src\matrix.cpp:698: error: (-5) Unknown array type in function cv::cvarrToMat ] at org.opencv.imgproc.Imgproc.drawContours_1(Native Method) at org.opencv.imgproc.Imgproc.drawContours(Imgproc.java:5222) at detection.main(detection.java:107)
you load image, but draw into image1 (which has nothing in it)
also, you always draw contour 0 (probably should be an ì` there)
@berak The image is loading. how to use imwrite function on MatofPoint.The function find() returns a Matofpoint.How do i get an output in for of image.Sorry this might be a silly question but help me through it.I have presented the image in a link. Thanks :)
@berak followed ur advice it works .Still have a problem the image is not as desired i have added the image