Quantcast
Channel: OpenCV Q&A Forum - Latest question feed
Viewing all articles
Browse latest Browse all 19555

OpenCV Error: Bad argument (Unknown array type) in cv::cvarrToMat

$
0
0
![image description](/upfiles/1481288549611778.jpg) 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](http://stackoverflow.com/questions/8667818/opencv-c-obj-c-detecting-a-sheet-of-paper-square-detection/8863060#8863060). and[java code](http://stackoverflow.com/questions/18020455/java-opencv-tesseract-ocr-code-regocnition/18042054#18042054) Image [link text](http://cl.ly/2o3V2g3l3f0e3m0s1p1K) 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 contours = new ArrayList<>(); List blurredChannel = new ArrayList<>(); blurredChannel.add(blurred); List 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 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 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, 255, 255)); MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(0).toArray() ); //Processing on mMOP2f1 which is in type MatOfPoint2f double approxDistance = Imgproc.arcLength(contour2f, true)*0.02; Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true); //Convert back to MatOfPoint MatOfPoint points = new MatOfPoint( approxCurve.toArray() ); // Get bounding rect of contour Rect rect = Imgproc.boundingRect(points); // draw enclosing rectangle (all same color, but you could use variable i to make them unique) Core.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,255,0)); Rect rect_crop = new Rect(rect.x-1, rect.y-1, rect.width+1, rect.height+1); Mat image_roi = new Mat(image,rect_crop); Highgui.imwrite("a.jpg",image_roi); */ } } Thanks

Viewing all articles
Browse latest Browse all 19555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>