Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); for(int i=0;i<contours.size();i++){ //Convert contours(i) from MatOfPoint to MatOfPoint2f contours.get(i).convertTo(mMOP2f1, CvType.CV_32FC2); //Processing on mMOP2f1 which is in type MatOfPoint2f Imgproc.approxPolyDP(mMOP2f1, mMOP2f2, approxDistance, true); //Convert back to MatOfPoint and put the new values back into the contours list mMOP2f2.convertTo(contours.get(i), CvType.CV_32S); }
/// Source variable MatOfPoint SrcMtx; /// New variable MatOfPoint2f NewMtx = new MatOfPoint2f( SrcMtx.toArray() );
String inputFileName="simm_01"; String inputExtension = "jpg"; String inputDir = getCacheDir().getAbsolutePath(); // use the cache directory for i/o String outputDir = getCacheDir().getAbsolutePath(); String outputExtension = "png"; String inputFilePath = inputDir + File.separator + inputFileName + "." + inputExtension; Log.d (this.getClass().getSimpleName(), "loading " + inputFilePath + "..."); Mat image = Imgcodecs.imread(inputFilePath); Log.d (this.getClass().getSimpleName(), "width of " + inputFileName + ": " + image.width()); // if width is 0 then it did not read your image. // for the canny edge detection algorithm, play with these to see different results int threshold1 = 70; int threshold2 = 100; Mat im_canny = new Mat(); // you have to initialize output image before giving it to the Canny method Imgproc.Canny(image, im_canny, threshold1, threshold2); String cannyFilename = outputDir + File.separator + inputFileName + "_canny-" + threshold1 + "-" + threshold2 + "." + outputExtension; Log.d (this.getClass().getSimpleName(), "Writing " + cannyFilename); Imgcodecs.imwrite(cannyFilename, im_canny);
Mat mYuv = new Mat(); Mat mRgba = new Mat(); Mat thresholdImage = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1); mYuv.put(0, 0, data); Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4); Imgproc.cvtColor(mRgba, thresholdImage, Imgproc.COLOR_RGB2GRAY, 4); Imgproc.Canny(thresholdImage, thresholdImage, 80, 100, 3); Mat lines = new Mat(); int threshold = 50; int minLineSize = 20; int lineGap = 20; Imgproc.HoughLinesP(thresholdImage, lines, 1, Math.PI/180, threshold, minLineSize, lineGap); for (int x = 0; x < lines.cols(); x++) { double[] vec = lines.get(0, x); double x1 = vec[0], y1 = vec[1], x2 = vec[2], y2 = vec[3]; Point start = new Point(x1, y1); Point end = new Point(x2, y2); Core.line(mRgba, start, end, new Scalar(255,0,0), 3); } Bitmap bmp = Bitmap.createBitmap(getFrameWidth(), getFrameHeight(), Bitmap.Config.ARGB_8888); if (Utils.matToBitmap(mRgba, bmp)) return bmp;
public static Mat drawContoursInImage(List<MatOfPoint> contours, Mat mBase) { Mat mResult = Element.getNewMat(); Mat mWork = new Mat(); Imgproc.cvtColor(mBase, mWork, toGray); Imgproc.cvtColor(mWork, mResult, toColor); Imgproc.drawContours(mResult, contours, -1, new Scalar(0, 0, 255)); return mResult; }
public Element next() { Element match = null; if (hasNext()) { match = new Element(new Element(currentX + offX, currentY + offY, target.w, target.h), currentScore); int margin = getPurgeMargin(); Range rangeX = new Range(Math.max(currentX - margin, 0), currentX + 1); Range rangeY = new Range(Math.max(currentY - margin, 0), currentY + 1); result.colRange(rangeX).rowRange(rangeY).setTo(new Scalar(0f)); } return match; }
/** * create a sub image from this image * * @param x pixel column * @param y pixel row * @param w width * @param h height * @return the new image */ public Picture getSub(int x, int y, int w, int h) { Picture img = new Picture(); if (isValid()) { img = new Picture(getContent().submat(new Rect(x, y, w, h))); } return img; }
private List<MatOfPoint> elementToPoints(Element element) { List<Point> points = new ArrayList<>(); points.add(new Point(element.x, element.y)); points.add(new Point(element.x + element.w, element.y)); points.add(new Point(element.x + element.w, element.y + element.h)); points.add(new Point(element.x, element.y + element.h)); MatOfPoint mPoints = new MatOfPoint(); mPoints.fromList(points); List<MatOfPoint> finalPoints = new ArrayList<MatOfPoint>(); finalPoints.add(mPoints); return finalPoints; }
public static void showResult(Mat img) { Imgproc.resize(img, img, new Size(640, 480)); MatOfByte matOfByte = new MatOfByte(); Highgui.imencode(".jpg", img, matOfByte); byte[] byteArray = matOfByte.toArray(); BufferedImage bufImage = null; try { InputStream in = new ByteArrayInputStream(byteArray); bufImage = ImageIO.read(in); JFrame frame = new JFrame(); frame.getContentPane().add(new JLabel(new ImageIcon(bufImage))); frame.pack(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
public void setMask(Mat mMask) { if (mMask.type() == CvType.CV_8UC1) { mask = mMask; } }
public boolean isValid() { return !mBase.empty(); }
public Mat getContentA() { if (hasMask()) { return mask; } else { return getNewMat(content.size(), 1, 255); } }
Mat croppedimage = cropImage(image,rect); Mat resizeimage = new Mat(); Size sz = new Size(100,100); Imgproc.resize( croppedimage, resizeimage, sz );
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
private static boolean isGray(Mat mat) { return mat.type() == gray; }
Mat featuredImg = new Mat(); Scalar kpColor = new Scalar(255,159,10);//this will be color of keypoints //featuredImg will be the output of first image Features2d.drawKeypoints(img1, keypoints1, featuredImg , kpColor, 0); //featuredImg will be the output of first image Features2d.drawKeypoints(img1, keypoints1, featuredImg , kpColor, 0);