List of usage examples for org.opencv.imgproc Imgproc blur
public static void blur(Mat src, Mat dst, Size ksize, Point anchor, int borderType)
From source file:OCV_Blur.java
License:Open Source License
@Override public void run(ImageProcessor ip) { if (ip.getBitDepth() == 8) { // srcdst int imw = ip.getWidth(); int imh = ip.getHeight(); byte[] srcdst_bytes = (byte[]) ip.getPixels(); // mat/*from ww w . ja v a 2 s. com*/ Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1); Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC1); // run src_mat.put(0, 0, srcdst_bytes); Imgproc.blur(src_mat, dst_mat, ksize, new Point(-1, -1), INT_BORDERTYPE[indBorderType]); dst_mat.get(0, 0, srcdst_bytes); } else if (ip.getBitDepth() == 16) { // srcdst int imw = ip.getWidth(); int imh = ip.getHeight(); short[] srcdst_shorts = (short[]) ip.getPixels(); // mat Mat src_mat = new Mat(imh, imw, CvType.CV_16S); Mat dst_mat = new Mat(imh, imw, CvType.CV_16S); // run src_mat.put(0, 0, srcdst_shorts); Imgproc.blur(src_mat, dst_mat, ksize, new Point(-1, -1), INT_BORDERTYPE[indBorderType]); dst_mat.get(0, 0, srcdst_shorts); } else if (ip.getBitDepth() == 24) { // dst int imw = ip.getWidth(); int imh = ip.getHeight(); int[] srcdst_ints = (int[]) ip.getPixels(); // mat Mat src_mat = new Mat(imh, imw, CvType.CV_8UC3); Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC3); // run OCV__LoadLibrary.intarray2mat(srcdst_ints, src_mat, imw, imh); Imgproc.blur(src_mat, dst_mat, ksize, new Point(-1, -1), INT_BORDERTYPE[indBorderType]); OCV__LoadLibrary.mat2intarray(dst_mat, srcdst_ints, imw, imh); } else if (ip.getBitDepth() == 32) { // srcdst int imw = ip.getWidth(); int imh = ip.getHeight(); float[] srcdst_floats = (float[]) ip.getPixels(); // mat Mat src_mat = new Mat(imh, imw, CvType.CV_32F); Mat dst_mat = new Mat(imh, imw, CvType.CV_32F); // run src_mat.put(0, 0, srcdst_floats); Imgproc.blur(src_mat, dst_mat, ksize, new Point(-1, -1), INT_BORDERTYPE[indBorderType]); dst_mat.get(0, 0, srcdst_floats); } else { IJ.error("Wrong image format"); } }
From source file:edu.fiu.cate.breader.BaseSegmentation.java
/** * Finds the bounding box for the book on the stand using * the high resolution image.//w w w . ja v a 2 s .c o m * @param src- High Resolution image of the book * @return Rectangle delineating the book */ public Rect highRes(Mat src) { Mat dst = src.clone(); Imgproc.blur(src, dst, new Size(100.0, 100.0), new Point(-1, -1), Core.BORDER_REPLICATE); Imgproc.threshold(dst, dst, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU); Imgproc.Canny(dst, dst, 50, 200, 3, false); List<MatOfPoint> contours = new LinkedList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); Mat color = new Mat(); Imgproc.cvtColor(src, color, Imgproc.COLOR_GRAY2BGR); for (int k = 0; k < contours.size(); k++) { byte[] vals = ITools.getHeatMapColor((float) k / (float) contours.size()); Imgproc.drawContours(color, contours, k, new Scalar(vals[0], vals[1], vals[2]), 8); } new IViewer("HighRes Contours ", BReaderTools.bufferedImageFromMat(color)); Point center = new Point(src.cols() / 2, src.rows() / 2); //Check hierarchy tree int[] res = polySearch(center, hierarchy, contours, 0); while (res[0] != 1 && res[2] != -1) { res = polySearch(center, hierarchy, contours, res[2]); if (res[0] == 1) break; } MatOfInt tHull = new MatOfInt(); int index = 0; if (res[1] != -1) { index = res[1]; } Imgproc.convexHull(contours.get(index), tHull); //get bounding box MatOfPoint cont = contours.get(index); Point[] points = new Point[tHull.rows()]; for (int i = 0; i < tHull.rows(); i++) { int pIndex = (int) tHull.get(i, 0)[0]; points[i] = new Point(cont.get(pIndex, 0)); } Rect out = Imgproc.boundingRect(new MatOfPoint(points)); return out; }
From source file:edu.fiu.cate.breader.BaseSegmentation.java
/** * Finds the bounding box for the book on the stand using * the depth average image./*w ww . j a v a 2 s.com*/ * @param src- The Depth average image * @return Rectangle delineating the book */ public Rect lowResDist(Mat src) { Mat dst = src.clone(); Imgproc.blur(src, dst, new Size(5, 5), new Point(-1, -1), Core.BORDER_REPLICATE); // Imgproc.threshold(dst, dst, 0,255,Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU); Imgproc.Canny(dst, dst, 50, 200, 3, false); // Canny(src, dst, 20, 60, 3); List<MatOfPoint> contours = new LinkedList<>(); Mat hierarchy = new Mat(); /// Find contours Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); Mat color = new Mat(); Imgproc.cvtColor(src, color, Imgproc.COLOR_GRAY2BGR); for (int k = 0; k < contours.size(); k++) { byte[] vals = ITools.getHeatMapColor((float) k / (float) contours.size()); Imgproc.drawContours(color, contours, k, new Scalar(vals[0], vals[1], vals[2]), 1); } new IViewer("LowRes Contours ", BReaderTools.bufferedImageFromMat(color)); for (int k = 0; k < contours.size(); k++) { MatOfPoint2f tMat = new MatOfPoint2f(); Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(k).toArray()), tMat, 5, true); contours.set(k, new MatOfPoint(tMat.toArray())); } List<Point> points = new LinkedList<Point>(); for (int i = 0; i < contours.size(); i++) { points.addAll(contours.get(i).toList()); } MatOfInt tHull = new MatOfInt(); Imgproc.convexHull(new MatOfPoint(points.toArray(new Point[points.size()])), tHull); //get bounding box Point[] tHullPoints = new Point[tHull.rows()]; for (int i = 0; i < tHull.rows(); i++) { int pIndex = (int) tHull.get(i, 0)[0]; tHullPoints[i] = points.get(pIndex); } Rect out = Imgproc.boundingRect(new MatOfPoint(tHullPoints)); return out; }