List of usage examples for org.opencv.core Mat channels
public int channels()
From source file:javacv.JavaCV.java
/** * Converts/writes a Mat into a BufferedImage. * * @param matrix Mat of type CV_8UC3 or CV_8UC1 * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY *//*from w w w . ja v a2s. co m*/ public static BufferedImage matToBufferedImage(Mat matrix) { int cols = matrix.cols(); int rows = matrix.rows(); int elemSize = (int) matrix.elemSize(); byte[] data = new byte[cols * rows * elemSize]; int type; matrix.get(0, 0, data); switch (matrix.channels()) { case 1: type = BufferedImage.TYPE_BYTE_GRAY; break; case 3: type = BufferedImage.TYPE_3BYTE_BGR; // bgr to rgb byte b; for (int i = 0; i < data.length; i = i + 3) { b = data[i]; data[i] = data[i + 2]; data[i + 2] = b; } break; default: return null; } BufferedImage image2 = new BufferedImage(cols, rows, type); image2.getRaster().setDataElements(0, 0, cols, rows, data); return image2; }
From source file:karthik.Barcode.MatrixBarcode.java
License:Open Source License
private void calcHistograms() { /* calculate histogram by masking for angles inside each bin, thresholding to set all those values to 1 and then creating an integral image. We can now calculate histograms for any size tile within the original image more efficiently than by using the built in calcHist method which would have to recalculate the histogram for every tile size. *//*from w w w .j ava2s.c o m*/ Mat target; angles = img_details.gradient_direction.clone(); target = img_details.temp_integral; for (int binRange = 1, integralIndex = 0; binRange < 181; binRange += img_details.BIN_WIDTH, integralIndex++) { target.setTo(ZERO_SCALAR); img_details.gradient_direction.copyTo(angles); Core.inRange(img_details.gradient_direction, scalarDict.get(binRange), scalarDict.get(binRange + img_details.BIN_WIDTH), mask); Core.bitwise_not(mask, mask); angles.setTo(ZERO_SCALAR, mask); Imgproc.threshold(angles, target, 0, 1, Imgproc.THRESH_BINARY); Imgproc.integral(target, target); target.get(0, 0, img_details.histIntegralArrays[integralIndex]); } // there is some problem if the created integral image does not have exactly one channel assert (target.channels() == 1) : "Integral does not have exactly one channel"; }
From source file:LetsStart.utils.ImageProcessor.java
public BufferedImage toBufferedImage(Mat matrix) { int type = BufferedImage.TYPE_BYTE_GRAY; if (matrix.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; }/*from ww w . j a va2 s .co m*/ int bufferSize = matrix.channels() * matrix.cols() * matrix.rows(); byte[] buffer = new byte[bufferSize]; matrix.get(0, 0, buffer); // get all the pixels BufferedImage image = new BufferedImage(matrix.cols(), matrix.rows(), type); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(buffer, 0, targetPixels, 0, buffer.length); return image; }
From source file:LetsStart.utils.ImageViewer.java
public Image toBufferedImage(Mat matrix) { int type = BufferedImage.TYPE_BYTE_GRAY; if (matrix.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; }/*ww w. j a v a2s .c o m*/ int bufferSize = matrix.channels() * matrix.cols() * matrix.rows(); byte[] buffer = new byte[bufferSize]; matrix.get(0, 0, buffer); // get all the pixels BufferedImage image = new BufferedImage(matrix.cols(), matrix.rows(), type); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(buffer, 0, targetPixels, 0, buffer.length); return image; }
From source file:Main.Camera.CameraController.java
public static BufferedImage matToBufferedImage(Mat matrix, BufferedImage bimg) { if (matrix != null) { int cols = matrix.cols(); int rows = matrix.rows(); int elemSize = (int) matrix.elemSize(); byte[] data = new byte[cols * rows * elemSize]; int type; matrix.get(0, 0, data);/* w w w. j a v a 2 s. c om*/ switch (matrix.channels()) { case 1: type = BufferedImage.TYPE_BYTE_GRAY; break; case 3: type = BufferedImage.TYPE_3BYTE_BGR; // bgr to rgb byte b; for (int i = 0; i < data.length; i = i + 3) { b = data[i]; data[i] = data[i + 2]; data[i + 2] = b; } break; default: return null; } // Reuse existing BufferedImage if possible if (bimg == null || bimg.getWidth() != cols || bimg.getHeight() != rows || bimg.getType() != type) { bimg = new BufferedImage(cols, rows, type); } bimg.getRaster().setDataElements(0, 0, cols, rows, data); } else { // mat was null bimg = null; } return bimg; }
From source file:main.PGMReader.java
public BufferedImage matToBufferedImage(Mat original) { // init//from w ww.jav a2s .c o m BufferedImage image = null; int width = original.width(); int height = original.height(); int channels = original.channels(); byte[] sourcePixels = new byte[width * height * channels]; original.get(0, 0, sourcePixels); if (original.channels() > 1) { image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); } else { image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); } final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); return image; }
From source file:main.Utils.java
public BufferedImage convertMatToImage(Mat mat) { int type = BufferedImage.TYPE_BYTE_GRAY; if (mat.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; }/*from w w w . jav a2 s .c o m*/ int bufferSize = mat.channels() * mat.cols() * mat.rows(); byte[] bytes = new byte[bufferSize]; mat.get(0, 0, bytes); BufferedImage imagem = new BufferedImage(mat.cols(), mat.rows(), type); byte[] targetPixels = ((DataBufferByte) imagem.getRaster().getDataBuffer()).getData(); System.arraycopy(bytes, 0, targetPixels, 0, bytes.length); return imagem; }
From source file:main.Utils.java
public BufferedImage matToBufferedImage(Mat original) { // init/*ww w. j a va 2 s. c om*/ BufferedImage image = null; int width = original.width(), height = original.height(), channels = original.channels(); byte[] sourcePixels = new byte[width * height * channels]; original.get(0, 0, sourcePixels); if (original.channels() > 1) { image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); } else { image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); } final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); return image; }
From source file:openbjk.Identificador.java
public Mat norm_0_255(Mat input) { Mat dst = null;/*from w w w. j av a2 s .co m*/ switch (input.channels()) { case 1: Core.normalize(input, dst, 0, 255, 2, CvType.CV_8UC1); break; case 3: Core.normalize(input, dst, 0, 255, 2, CvType.CV_8UC3); break; default: input.copyTo(dst); } return dst; }
From source file:opencv.CaptchaDetection.java
private static Mat k_means_spilter(Mat src) { Mat dst = Mat.zeros(src.size(), CvType.CV_8UC1); int width = src.cols(); int height = src.rows(); int dims = src.channels(); // //from w w w . jav a 2 s . c om int clusterCount = 3; Mat points = new Mat(width * height, dims, CvType.CV_32F, new Scalar(0)); Mat centers = new Mat(clusterCount, dims, CvType.CV_32F); Mat labels = new Mat(width * height, 1, CvType.CV_32S); // points for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int index = row * width + col; double[] s_data = src.get(row, col); for (int channel = 0; channel < 3; channel++) { float[] f_buff = new float[1]; f_buff[0] = (float) s_data[channel]; points.put(index, channel, f_buff); } } } // knn ? TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER, 10, 0.1); Core.kmeans(points, clusterCount, labels, criteria, 3, Core.KMEANS_PP_CENTERS, centers); // ??? label index Map<Integer, Integer> tmp = new TreeMap<>(); for (int i = 0; i < clusterCount; i++) { int sum = 0; for (int j = 0; j < dims; j++) { sum += centers.get(i, j)[0]; } while (tmp.containsKey(sum)) sum++; tmp.put(sum, i); } int count = 0; int[] label_order = new int[clusterCount]; for (Map.Entry<Integer, Integer> iter : tmp.entrySet()) { label_order[count++] = iter.getValue(); } for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int index = row * width + col; int label = (int) labels.get(index, 0)[0]; if (label == label_order[1]) { byte[] d_buff = new byte[1]; d_buff[0] = (byte) 255; dst.put(row, col, d_buff); } } } return dst; }