List of usage examples for org.opencv.core Mat size
public Size size()
From source file:org.lasarobotics.vision.image.Transform.java
License:Open Source License
/** * Scale an image by a scale factor// w ww . j av a 2s . com * * @param img The image to scale * @param factor The scale factor. If greater than 1, image will dilate. Otherwise, * image will constrict. */ private static void scale(Mat img, double factor) { resize(img, new Size(img.size().width * factor, img.size().height * factor)); }
From source file:org.lasarobotics.vision.image.Transform.java
License:Open Source License
/** * Scales an image to an approximate size. The scale will always be equal * on the x and y axis, regardless of the approxSize. * * @param img The image//from www .ja v a 2 s. c om * @param approxSize The target size * @param maximize If maximize is true, then if the approxSize aspect ratio * does not match the target, then the largest possible image * would be used. If false (default), the the smallest image * would be used. * @param integerScale If true (default), then only integer scale factors would be used. * Otherwise, any scale factor can be used. */ private static double makeScale(Mat img, Size approxSize, boolean maximize, boolean integerScale) { Size imageSize = img.size(); double ratioWidth = approxSize.width / imageSize.width; double ratioHeight = approxSize.height / imageSize.height; double ratio = maximize ? Math.max(ratioWidth, ratioHeight) : Math.min(ratioWidth, ratioHeight); if (MathUtil.equal(ratio, 1)) return 1; if (integerScale) { //The scale factor is always greater than 1 double scale = (ratio < 1) ? 1 / ratio : ratio; //If you are actually increasing the size of the object, use ceiling() //Otherwise, use floor() scale = maximize ^ (ratio < 1) ? Math.ceil(scale) : Math.floor(scale); //Get the actual ratio again return (ratio < 1) ? 1 / scale : scale; } else { return ratio; } }
From source file:org.lasarobotics.vision.image.Transform.java
License:Open Source License
private static void resize(Mat img, Size size) { int interpolation; if (MathUtil.equal(size.area(), img.size().area())) return;/*from w ww .ja v a 2 s . c o m*/ else if (size.width > img.size().width && size.height > img.size().height) interpolation = Imgproc.CV_INTER_CUBIC; //enlarge image else if (size.width < img.size().width && size.height < img.size().height) interpolation = Imgproc.CV_INTER_AREA; //shrink image else interpolation = Imgproc.CV_INTER_LINEAR; //not entirely sure, so use safe option Imgproc.resize(img, img, size, 0, 0, interpolation); }
From source file:org.openpnp.machine.reference.ReferenceCamera.java
License:Open Source License
private Mat crop(Mat mat) { if (cropWidth != 0 || cropHeight != 0) { Rect roi = new Rect((int) ((mat.size().width / 2) - (cropWidth / 2)), (int) ((mat.size().height / 2) - (cropHeight / 2)), cropWidth, cropHeight); Mat tmp = new Mat(mat, roi); tmp.copyTo(mat);/*from w w w.j a va2s . c o m*/ tmp.release(); } return mat; }
From source file:org.openpnp.machine.reference.ReferenceCamera.java
License:Open Source License
private Mat rotate(Mat mat, double rotation) { if (rotation == 0D) { return mat; }/*from w w w .ja va 2s .c o m*/ // See: // http://stackoverflow.com/questions/22041699/rotate-an-image-without-cropping-in-opencv-in-c Point center = new Point(mat.width() / 2D, mat.height() / 2D); Mat mapMatrix = Imgproc.getRotationMatrix2D(center, rotation, 1.0); // determine bounding rectangle Rect bbox = new RotatedRect(center, mat.size(), rotation).boundingRect(); // adjust transformation matrix double[] cx = mapMatrix.get(0, 2); double[] cy = mapMatrix.get(1, 2); cx[0] += bbox.width / 2D - center.x; cy[0] += bbox.height / 2D - center.y; mapMatrix.put(0, 2, cx); mapMatrix.put(1, 2, cy); Mat dst = new Mat(bbox.width, bbox.height, mat.type()); Imgproc.warpAffine(mat, dst, mapMatrix, bbox.size(), Imgproc.INTER_LINEAR); mat.release(); mapMatrix.release(); return dst; }
From source file:org.openpnp.machine.reference.ReferenceCamera.java
License:Open Source License
private Mat offset(Mat mat, int offsetX, int offsetY) { if (offsetX == 0D && offsetY == 0D) { return mat; }//from ww w . j av a2 s . c o m Mat mapMatrix = new Mat(2, 3, CvType.CV_32F) { { put(0, 0, 1, 0, offsetX); put(1, 0, 0, 1, offsetY); } }; Mat dst = mat.clone(); Imgproc.warpAffine(mat, dst, mapMatrix, mat.size(), Imgproc.INTER_LINEAR); mat.release(); mapMatrix.release(); return dst; }
From source file:org.openpnp.machine.reference.ReferenceCamera.java
License:Open Source License
private Mat undistort(Mat mat) { if (!calibration.isEnabled()) { return mat; }//from ww w . j a v a 2 s .c o m if (undistortionMap1 == null || undistortionMap2 == null) { undistortionMap1 = new Mat(); undistortionMap2 = new Mat(); Mat rectification = Mat.eye(3, 3, CvType.CV_32F); Imgproc.initUndistortRectifyMap(calibration.getCameraMatrixMat(), calibration.getDistortionCoefficientsMat(), rectification, calibration.getCameraMatrixMat(), mat.size(), CvType.CV_32FC1, undistortionMap1, undistortionMap2); rectification.release(); } Mat dst = mat.clone(); Imgproc.remap(mat, dst, undistortionMap1, undistortionMap2, Imgproc.INTER_LINEAR); mat.release(); return dst; }
From source file:org.pattern.detection.contour.ContourDetectionAlgorithm.java
@Override public List<? extends Particle> detectAndAssign(ParticleImage image) { // take the copy of image that we dont modify the original Mat img = new Mat(); image.getPixels().copyTo(img);/*from www. j a v a2s. c om*/ // blur the image to denoise //Imgproc.blur(imagePixels, imagePixels, new Size(3, 3)); // thresholds the image Mat thresholded = new Mat(); // Imgproc.threshold(imagePixels, thresholded, // THRESHOLD, MAX, Imgproc.THRESH_TOZERO_INV); // thresholding Imgproc.adaptiveThreshold(img, thresholded, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 155, 15); Highgui.imwrite("1_thresholded.jpg", thresholded); Mat edges = new Mat(); Imgproc.Canny(img, edges, 100, 200); Highgui.imwrite("1_canny.jpg", edges); // remove small noises // Mat kernel = Mat.ones(new Size(3, 3), CvType.CV_8UC1); Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(5, 5)); Imgproc.morphologyEx(thresholded, thresholded, Imgproc.MORPH_OPEN, kernel); Highgui.imwrite("2_opening.jpg", thresholded); // Imgproc.erode(thresholded, thresholded, kernel, ORIGIN, 3); // Highgui.imwrite("3_erode.jpg", thresholded); Mat distTransform = new Mat(); Imgproc.distanceTransform(thresholded, distTransform, Imgproc.CV_DIST_C, 5); distTransform.convertTo(distTransform, CvType.CV_8UC1); Imgproc.equalizeHist(distTransform, distTransform); Highgui.imwrite("4_distance_transform.jpg", distTransform); Mat markerMask = Mat.zeros(img.size(), CvType.CV_8UC1); double max = Core.minMaxLoc(distTransform).maxVal; Imgproc.threshold(distTransform, markerMask, max * 0.9, 255, Imgproc.THRESH_BINARY); markerMask.convertTo(markerMask, CvType.CV_8UC1); Highgui.imwrite("5_thresholded_distance.jpg", markerMask); List<MatOfPoint> contours = new ArrayList<>(); Imgproc.findContours(markerMask, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, ORIGIN); Mat markers = Mat.zeros(img.size(), CvType.CV_32S); //markers.setTo(Scalar.all(0)); Random rand = new Random(); for (int idx = 0; idx < contours.size(); idx++) { Scalar color = new Scalar(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)); Imgproc.drawContours(markers, contours, idx, color, -1); } Highgui.imwrite("6_markers.jpg", markers); Imgproc.cvtColor(img, img, Imgproc.COLOR_GRAY2RGB); img.convertTo(img, CvType.CV_8UC3); Imgproc.watershed(img, markers); Highgui.imwrite("7_wattershed.jpg", markers); // detect contours // List<MatOfPoint> contours = new ArrayList<>(); Imgproc.findContours(thresholded, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, ORIGIN); // create particle from each contour List<Particle> particles = new ArrayList<>(); int i = 0; for (MatOfPoint contour : contours) { Point cog = calcCog(contour); if (!Double.isNaN(cog.x) && !Double.isNaN(cog.y)) { System.out.println(cog); Particle p = new Particle(cog, contour); particles.add(p); // just for reorting reasons image.assign(p); } } return particles; }
From source file:org.sikuli.script.Finder.java
License:MIT License
private Mat doFindMatch(Probe probe, Mat base, Mat target) { Mat res = new Mat(); Mat bi = new Mat(); Mat pi = new Mat(); if (!probe.img.isPlainColor()) { Imgproc.matchTemplate(base, target, res, Imgproc.TM_CCOEFF_NORMED); } else {//from www. ja v a2 s . c o m if (probe.img.isBlack()) { Core.bitwise_not(base, bi); Core.bitwise_not(target, pi); } else { bi = base; pi = target; } Imgproc.matchTemplate(bi, pi, res, Imgproc.TM_SQDIFF_NORMED); Core.subtract(Mat.ones(res.size(), CvType.CV_32F), res, res); } return res; }
From source file:org.sikuli.script.ImageFind.java
License:MIT License
private Core.MinMaxLocResult doFindMatch(Mat base, Mat probe) { Mat res = new Mat(); Mat bi = new Mat(); Mat pi = new Mat(); if (!isPlainColor) { Imgproc.matchTemplate(base, probe, res, Imgproc.TM_CCOEFF_NORMED); } else {/*from w w w . j av a2 s . c o m*/ if (isBlack) { Core.bitwise_not(base, bi); Core.bitwise_not(probe, pi); } else { bi = base; pi = probe; } Imgproc.matchTemplate(bi, pi, res, Imgproc.TM_SQDIFF_NORMED); Core.subtract(Mat.ones(res.size(), CvType.CV_32F), res, res); } return Core.minMaxLoc(res); }