List of usage examples for org.opencv.core Mat channels
public int channels()
From source file:by.zuyeu.deyestracker.core.detection.DemoPanel.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 ww w.j av a2 s . c o m*/ public boolean convertMatToBufferedImage(Mat matBGR) { int width = matBGR.width(), height = matBGR.height(), channels = matBGR.channels(); byte[] sourcePixels = new byte[width * height * channels]; matBGR.get(0, 0, sourcePixels); // create new image and get reference to backing data image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); return true; }
From source file:ch.zhaw.facerecognitionlibrary.PreProcessor.StandardPreprocessing.GrayScale.java
License:Open Source License
public PreProcessor preprocessImage(PreProcessor preProcessor) { List<Mat> images = preProcessor.getImages(); List<Mat> processed = new ArrayList<Mat>(); for (Mat img : images) { if (img.channels() > 1) { Imgproc.cvtColor(img, img, Imgproc.COLOR_RGBA2GRAY); }/*from w w w .j a v a 2 s .c o m*/ processed.add(img); } preProcessor.setImages(processed); return preProcessor; }
From source file:Clases.Segmentador.java
public BufferedImage Mat2BufferedImage(Mat m) { //source: http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/ //Fastest code //The output can be assigned either to a BufferedImage or to an Image int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; }//from ww w . j a v a2s .com int bufferSize = m.channels() * m.cols() * m.rows(); byte[] b = new byte[bufferSize]; m.get(0, 0, b); // get all the pixels BufferedImage image = new BufferedImage(m.cols(), m.rows(), type); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(b, 0, targetPixels, 0, b.length); return image; }
From source file:classes.TextRecognitionPreparer.java
public static Mat equalizeIntensity(Mat inputImage) { if (inputImage.channels() >= 3) { Mat ycrcb = new Mat(); Imgproc.cvtColor(inputImage, ycrcb, Imgproc.COLOR_BGR2YUV); ArrayList<Mat> channels = new ArrayList<Mat>(); Core.split(ycrcb, channels);/*from w w w. j av a 2s . co m*/ Mat equalized = new Mat(); Imgproc.equalizeHist(channels.get(0), equalized); channels.set(0, equalized); Core.merge(channels, ycrcb); Mat result = new Mat(); Imgproc.cvtColor(ycrcb, result, Imgproc.COLOR_YUV2BGR); return result; } return null; }
From source file:classes.Util.java
public static BufferedImage mat2Img(Mat in) { BufferedImage out;/*from ww w. j a va 2 s. c om*/ byte[] data = new byte[in.width() * in.height() * (int) in.elemSize()]; int type; in.get(0, 0, data); if (in.channels() == 1) { type = BufferedImage.TYPE_BYTE_GRAY; } else { type = BufferedImage.TYPE_3BYTE_BGR; } out = new BufferedImage(in.width(), in.height(), type); out.getRaster().setDataElements(0, 0, in.width(), in.height(), data); return out; }
From source file:cmib_4_4.Feature.java
public int[] featureVector(Mat image1) { Mat image = new Mat(); Size sz = new Size(30, 30); Imgproc.resize(image1, image, sz);//from w ww. j a v a 2 s. com int size = (int) (image1.total() * image1.channels()); int size2 = (image.width() * image.height()); double[][] spec1 = new double[size2][3]; FeatureVector A = new FeatureVector(); int k = 0; for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { //image.get(i, j, rgb); double[] rgb = image.get(i, j); double[] a = A.cartToSpec(rgb[0], rgb[1], rgb[2]); double x = Math.toRadians(90); spec1[k][0] = a[0] / x; spec1[k][1] = a[1] / x; spec1[k][2] = a[2] / x; //System.out.println(rgb[0]); //System.out.println(spec1[k][2]); k++; } } int[][] b = new int[11][11]; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { b[i][j] = 0; } } for (int i = 0; i < 900; i++) { int x1 = (int) (Math.round(spec1[i][1] * 10)); int y1 = (int) (Math.round(spec1[i][2] * 10)); b[x1][y1] = b[x1][y1] + 1; //System.out.println(x1+"and"+y1); } int l = 0; int[] c = new int[121]; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { c[l] = b[i][j]; l++; //System.out.println(c[l-1]); } } return c; }
From source file:cmib_4_4.FeatureVector.java
public Mat removeNoisePixcels(Mat noiseGreyImage, Mat rbgImage) { Mat image = noiseGreyImage; Mat image1 = rbgImage;/* ww w.j a v a 2 s .c o m*/ int size = (int) (image.total() * image.channels()); byte[] get = new byte[size]; byte[] temp1 = new byte[size]; int size1 = (int) (image1.total() * image1.channels()); byte[] rgb1 = new byte[size1]; for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { image.get(i, j, get); if (get[0] == -1) { image.put(i, j, 1); } image.get(i, j, get); //System.out.println(get[0]); if (get[0] == 1) { if (i == 0 & j == 0) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j + 1, a1); image.get(i + 1, j + 1, a2); image.get(i + 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 0.0); //System.out.println("1"); } else { image.put(i, j, 0); } } else if (i == 0 & j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j - 1, a1); image.get(i + 1, j - 1, a2); image.get(i + 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("2"); } else { image.put(i, j, 0); } } else if (i == image.rows() & j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { // image1.put(j, i, 1.0); // System.out.println("3"); } else { image.put(i, j, 0); } } else if (j == 0 & i == image.rows()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j + 1, a1); image.get(i - 1, j + 1, a2); image.get(i - 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("4"); } else { image.put(i, j, 0); } } else if (j == 0) { double[] a1 = image.get(i - 1, j); double[] a2 = image.get(i - 1, j + 1); double[] a3 = image.get(i, j + 1); double[] a4 = image.get(i + 1, j + 1); double[] a5 = image.get(i + 1, j); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("5"); } else { image.put(i, j, 0); } } else if (i == 0) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i, j - 1, a1); image.get(i + 1, j - 1, a2); image.get(i + 1, j, a3); image.get(i + 1, j + 1, a4); image.get(i, j + 1, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("6"); } else { image.put(i, j, 0); } } else if (j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); image.get(i + 1, j - 1, a4); image.get(i + 1, j, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("7"); } else { image.put(i, j, 0); } } else if (i == image.rows()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i, j + 1, a1); image.get(i - 1, j + 1, a2); image.get(i - 1, j, a3); image.get(i - 1, j - 1, a4); image.get(i, j - 1, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("8"); } else { image.put(i, j, 0); } } else { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; byte[] a6 = new byte[1]; byte[] a7 = new byte[1]; byte[] a8 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); image.get(i + 1, j - 1, a4); image.get(i + 1, j, a5); image.get(i + 1, j + 1, a6); image.get(i, j + 1, a7); image.get(i - 1, j + 1, a8); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == 1 & a4[0] == -1 & a5[0] == -1) | (a4[0] == -1 & a5[0] == -1 & a6[0] == -1) | (a5[0] == -1 & a6[0] == 1 & a7[0] == -1) | (a6[0] == -1 & a7[0] == -1 & a8[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("9"); } else { image.put(i, j, 0); } } } } } ////////////////////// Obtain RGB final blood cell image ///////////////////////////// for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { image.get(i, j, temp1); image1.get(i, j, rgb1); if (temp1[0] == -1) { image.put(i, j, 1); } image.get(i, j, temp1); //System.out.println(temp1[0]); byte r = (byte) (rgb1[0] * temp1[0]); byte g = (byte) (rgb1[1] * temp1[0]); byte b = (byte) (rgb1[2] * temp1[0]); image1.put(i, j, new byte[] { r, g, b }); } } ///////////////////////////////////////////////////////////////////////////////////// return image1; }
From source file:cmib_4_4.NoiseRemove.java
public Mat removeNoisePixcels(Mat noiseGreyImage, Mat rbgImage) { Mat image = noiseGreyImage; Mat image1 = rbgImage;//from w w w. j av a 2 s. c o m int size = (int) (image.total() * image.channels()); byte[] get = new byte[size]; byte[] temp1 = new byte[size]; int size1 = (int) (image1.total() * image1.channels()); byte[] rgb1 = new byte[size1]; for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { image.get(i, j, get); if (get[0] == -1) { image.put(i, j, 1); } image.get(i, j, get); //System.out.println(get[0]); if (get[0] == 1) { if (i == 0 & j == 0) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j + 1, a1); image.get(i + 1, j + 1, a2); image.get(i + 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 0.0); //System.out.println("1"); } else { image.put(i, j, 0); } } else if (i == 0 & j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j - 1, a1); image.get(i + 1, j - 1, a2); image.get(i + 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("2"); } else { image.put(i, j, 0); } } else if (i == image.rows() & j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { // image1.put(j, i, 1.0); // System.out.println("3"); } else { image.put(i, j, 0); } } else if (j == 0 & i == image.rows()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j + 1, a1); image.get(i - 1, j + 1, a2); image.get(i - 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("4"); } else { image.put(i, j, 0); } } else if (j == 0) { double[] a1 = image.get(i - 1, j); double[] a2 = image.get(i - 1, j + 1); double[] a3 = image.get(i, j + 1); double[] a4 = image.get(i + 1, j + 1); double[] a5 = image.get(i + 1, j); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("5"); } else { image.put(i, j, 0); } } else if (i == 0) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i, j - 1, a1); image.get(i + 1, j - 1, a2); image.get(i + 1, j, a3); image.get(i + 1, j + 1, a4); image.get(i, j + 1, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("6"); } else { image.put(i, j, 0); } } else if (j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); image.get(i + 1, j - 1, a4); image.get(i + 1, j, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("7"); } else { image.put(i, j, 0); } } else if (i == image.rows()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i, j + 1, a1); image.get(i - 1, j + 1, a2); image.get(i - 1, j, a3); image.get(i - 1, j - 1, a4); image.get(i, j - 1, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("8"); } else { image.put(i, j, 0); } } else { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; byte[] a6 = new byte[1]; byte[] a7 = new byte[1]; byte[] a8 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); image.get(i + 1, j - 1, a4); image.get(i + 1, j, a5); image.get(i + 1, j + 1, a6); image.get(i, j + 1, a7); image.get(i - 1, j + 1, a8); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == 1 & a4[0] == -1 & a5[0] == -1) | (a4[0] == -1 & a5[0] == -1 & a6[0] == -1) | (a5[0] == -1 & a6[0] == 1 & a7[0] == -1) | (a6[0] == -1 & a7[0] == -1 & a8[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("9"); } else { image.put(i, j, 0); } } } } } ////////////////////// Obtain RGB final blood cell image ///////////////////////////// for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { image.get(i, j, temp1); image1.get(i, j, rgb1); if (temp1[0] == -1) { image.put(i, j, 1); } image.get(i, j, temp1); byte r = (byte) (rgb1[0] * temp1[0]); byte g = (byte) (rgb1[1] * temp1[0]); byte b = (byte) (rgb1[2] * temp1[0]); image1.put(i, j, new byte[] { r, g, b }); } } return image1; }
From source file:com.astrocytes.core.ImageHelper.java
License:Open Source License
public static BufferedImage convertMatToBufferedImage(Mat in) { BufferedImage out;//from w w w .java 2 s. co m byte[] data = new byte[in.cols() * in.rows() * (int) in.elemSize()]; in.get(0, 0, data); int type = BufferedImage.TYPE_3BYTE_BGR; switch (in.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; } out = new BufferedImage(in.cols(), in.rows(), type); out.getRaster().setDataElements(0, 0, in.cols(), in.rows(), data); return out; }
From source file:com.astrocytes.core.operationsengine.CoreOperations.java
License:Open Source License
/** * Applies thresholding for color image. * * @param src - color source image./*from w ww .j a v a2s. c o m*/ * @param r - the value for red value in threshold. * @param g - the value for green value in threshold. * @param b - the value for blue value in threshold. * @return thresholded color image. */ public static Mat threshold(Mat src, int r, int g, int b) { if (src.channels() < 3) return src; Mat dest = new Mat(); Mat srcBin = new Mat(); Imgproc.threshold(src, srcBin, 1, 255, Imgproc.THRESH_BINARY); Core.inRange(src, new Scalar(0), new Scalar(r, g, b), dest); dest = invert(dest); cvtColor(dest, dest, Imgproc.COLOR_GRAY2BGR); dest = xor(srcBin, dest); dest = and(src, dest); return dest; }