List of usage examples for org.opencv.core Core mixChannels
public static void mixChannels(List<Mat> src, List<Mat> dst, MatOfInt fromTo)
From source file:classes.ObjectFinder.java
private void computeObjectHistogram() { // Converting the current fram to HSV color space Mat hsvImage = new Mat(this.objectImage.size(), CvType.CV_8UC3); System.out.println(this.objectImage); Imgproc.cvtColor(this.objectImage, hsvImage, Imgproc.COLOR_BGR2HSV); // Getting the pixels that are in te specified ranges Mat maskImage = new Mat(this.objectImage.size(), CvType.CV_8UC1); int hmin = thresholdsVector.get(0); int hmax = thresholdsVector.get(1); int smin = thresholdsVector.get(2); int smax = thresholdsVector.get(3); int vmin = thresholdsVector.get(4); int vmax = thresholdsVector.get(5); Core.inRange(hsvImage, new Scalar(hmin, smin, vmin), new Scalar(hmax, smax, vmax), maskImage); Mat hueImage = new Mat(hsvImage.size(), CvType.CV_8UC1); MatOfInt fromto = new MatOfInt(0, 0); Core.mixChannels(Arrays.asList(hsvImage), Arrays.asList(hueImage), fromto); MatOfInt sizes = new MatOfInt(16); MatOfFloat ranges = new MatOfFloat(0, 180); MatOfInt channels = new MatOfInt(0); Mat histogram = new Mat(); boolean accumulate = false; Imgproc.calcHist(Arrays.asList(hueImage), channels, maskImage, histogram, sizes, ranges, accumulate); Highgui.imwrite("histogram.png", histogram); // The resulting histogram is normalized and placed in the class variable Core.normalize(histogram, objectHistogram, 0, 255, Core.NORM_MINMAX); }
From source file:classes.ObjectFinder.java
private void backprojectObjectHistogram() { // Converting the current fram to HSV color space Mat hsvImage = new Mat(this.objectImage.size(), CvType.CV_8UC3); Imgproc.cvtColor(this.inputFrame, hsvImage, Imgproc.COLOR_BGR2HSV); // Getting the pixels that are in te specified ranges int hmin = this.thresholdsVector.get(0); int hmax = this.thresholdsVector.get(1); int smin = this.thresholdsVector.get(2); int smax = this.thresholdsVector.get(3); int vmin = this.thresholdsVector.get(4); int vmax = this.thresholdsVector.get(5); Mat maskImage = new Mat(this.objectImage.size(), CvType.CV_8UC1); Core.inRange(hsvImage, new Scalar(hmin, smin, vmin), new Scalar(hmax, smax, vmax), maskImage); // Taking the hue channel of the image Mat hueImage = new Mat(hsvImage.size(), hsvImage.depth()); MatOfInt fromto = new MatOfInt(0, 0); Core.mixChannels(Arrays.asList(hsvImage), Arrays.asList(hueImage), fromto); // Backprojecting the histogram over that hue channel image MatOfFloat ranges = new MatOfFloat(0, 180); MatOfInt channels = new MatOfInt(0); Imgproc.calcBackProject(Arrays.asList(hueImage), channels, this.objectHistogram, this.backprojectionImage, ranges, 1);//from w w w . ja v a2s. c o m Core.bitwise_and(backprojectionImage, maskImage, backprojectionImage); }
From source file:com.sikulix.core.SXElement.java
License:Open Source License
protected static Mat makeMat(BufferedImage bImg) { Mat aMat = new Mat(); if (bImg.getType() == BufferedImage.TYPE_INT_RGB) { log.trace("makeMat: INT_RGB (%dx%d)", bImg.getWidth(), bImg.getHeight()); int[] data = ((DataBufferInt) bImg.getRaster().getDataBuffer()).getData(); ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4); IntBuffer intBuffer = byteBuffer.asIntBuffer(); intBuffer.put(data);// w w w. ja v a 2 s .c om aMat = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC4); aMat.put(0, 0, byteBuffer.array()); Mat oMatBGR = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC3); Mat oMatA = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC1); java.util.List<Mat> mixIn = new ArrayList<Mat>(Arrays.asList(new Mat[] { aMat })); java.util.List<Mat> mixOut = new ArrayList<Mat>(Arrays.asList(new Mat[] { oMatA, oMatBGR })); //A 0 - R 1 - G 2 - B 3 -> A 0 - B 1 - G 2 - R 3 Core.mixChannels(mixIn, mixOut, new MatOfInt(0, 0, 1, 3, 2, 2, 3, 1)); return oMatBGR; } else if (bImg.getType() == BufferedImage.TYPE_3BYTE_BGR) { log.error("makeMat: 3BYTE_BGR (%dx%d)", bImg.getWidth(), bImg.getHeight()); byte[] data = ((DataBufferByte) bImg.getRaster().getDataBuffer()).getData(); aMat = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC3); aMat.put(0, 0, data); return aMat; } else if (bImg.getType() == BufferedImage.TYPE_4BYTE_ABGR) { log.trace("makeMat: TYPE_4BYTE_ABGR (%dx%d)", bImg.getWidth(), bImg.getHeight()); byte[] data = ((DataBufferByte) bImg.getRaster().getDataBuffer()).getData(); aMat = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC4); aMat.put(0, 0, data); Mat oMatBGR = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC3); Mat oMatA = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC1); java.util.List<Mat> mixIn = new ArrayList<Mat>(Arrays.asList(new Mat[] { aMat })); java.util.List<Mat> mixOut = new ArrayList<Mat>(Arrays.asList(new Mat[] { oMatA, oMatBGR })); //A 0 - R 1 - G 2 - B 3 -> A 0 - B 1 - G 2 - R 3 Core.mixChannels(mixIn, mixOut, new MatOfInt(0, 0, 1, 1, 2, 2, 3, 3)); return oMatBGR; } else { log.error("makeMat: Type not supported: %d (%dx%d)", bImg.getType(), bImg.getWidth(), bImg.getHeight()); } return aMat; }
From source file:com.sikulix.core.Visual.java
License:Open Source License
protected static Mat makeMat(BufferedImage bImg) { Mat aMat = null;/*from w ww.j av a 2s . c om*/ if (bImg.getType() == BufferedImage.TYPE_INT_RGB) { vLog.trace("makeMat: INT_RGB (%dx%d)", bImg.getWidth(), bImg.getHeight()); int[] data = ((DataBufferInt) bImg.getRaster().getDataBuffer()).getData(); ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4); IntBuffer intBuffer = byteBuffer.asIntBuffer(); intBuffer.put(data); aMat = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC4); aMat.put(0, 0, byteBuffer.array()); Mat oMatBGR = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC3); Mat oMatA = new Mat(bImg.getHeight(), bImg.getWidth(), CvType.CV_8UC1); List<Mat> mixIn = new ArrayList<Mat>(Arrays.asList(new Mat[] { aMat })); List<Mat> mixOut = new ArrayList<Mat>(Arrays.asList(new Mat[] { oMatA, oMatBGR })); //A 0 - R 1 - G 2 - B 3 -> A 0 - B 1 - G 2 - R 3 Core.mixChannels(mixIn, mixOut, new MatOfInt(0, 0, 1, 3, 2, 2, 3, 1)); return oMatBGR; } else if (bImg.getType() == BufferedImage.TYPE_3BYTE_BGR) { vLog.error("makeMat: 3BYTE_BGR (%dx%d)", bImg.getWidth(), bImg.getHeight()); } else { vLog.error("makeMat: Type not supported: %d (%dx%d)", bImg.getType(), bImg.getWidth(), bImg.getHeight()); } return aMat; }
From source file:cx.uni.jk.mms.iaip.filter.LogRedBlue.java
License:Open Source License
@Override public Mat convert(Mat mat) { MinMaxLocResult negativeMmlr, positiveMmlr; double min, max, alpha, beta; /** negative values to positive and log */ Mat negativeMat = mat.clone();/*from w ww . j a v a 2s. c o m*/ Core.min(negativeMat, new Scalar(0.0d), negativeMat); Core.multiply(negativeMat, new Scalar(-1.0d), negativeMat); Core.add(negativeMat, new Scalar(1.0d), negativeMat); Core.log(negativeMat, negativeMat); /** positve values log */ Mat positiveMat = mat.clone(); Core.max(positiveMat, new Scalar(0.0d), positiveMat); Core.add(positiveMat, new Scalar(1.0d), positiveMat); Core.log(positiveMat, positiveMat); /** find common contrast and brightness to fit into 8 bit */ negativeMmlr = Core.minMaxLoc(negativeMat); positiveMmlr = Core.minMaxLoc(positiveMat); min = 0; max = Math.max(negativeMmlr.maxVal, positiveMmlr.maxVal); alpha = 256.0d / (max - min); beta = -min * alpha; /** conversion of both matrices to 8 bit */ negativeMat.convertTo(negativeMat, CvType.CV_8UC1, alpha, beta); positiveMat.convertTo(positiveMat, CvType.CV_8UC1, alpha, beta); /** combine both matrices into one 8 bit 3 channel rgb picture */ Mat tempMat = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC3); List<Mat> mixSrcMats = new ArrayList<>(); mixSrcMats.add(negativeMat); // 1 channel: 0 mixSrcMats.add(positiveMat); // 1 channel: 1 List<Mat> mixDstMats = new ArrayList<>(); mixDstMats.add(tempMat); // 3 channels: 0-2 MatOfInt fromToMat = new MatOfInt(0, 0 /* neg->red */, -1, 1/* * null->green */, 1, 2 /* * pos- * > * blue */); Core.mixChannels(mixSrcMats, mixDstMats, fromToMat); return tempMat; }
From source file:cx.uni.jk.mms.iaip.filter.LogYellowCyan.java
License:Open Source License
@Override public Mat convert(Mat mat) { MinMaxLocResult negativeMmlr, positiveMmlr; double min, max, alpha, beta; /** negative values to positive and log */ Mat negativeMat = mat.clone();//from w w w .j av a 2 s . co m Core.min(negativeMat, new Scalar(0.0d), negativeMat); Core.multiply(negativeMat, new Scalar(-1.0d), negativeMat); Core.add(negativeMat, new Scalar(1.0d), negativeMat); Core.log(negativeMat, negativeMat); /** positve values log */ Mat positiveMat = mat.clone(); Core.max(positiveMat, new Scalar(0.0d), positiveMat); Core.add(positiveMat, new Scalar(1.0d), positiveMat); Core.log(positiveMat, positiveMat); /** find common contrast and brightness to fit into 8 bit */ negativeMmlr = Core.minMaxLoc(negativeMat); positiveMmlr = Core.minMaxLoc(positiveMat); min = 0; max = Math.max(negativeMmlr.maxVal, positiveMmlr.maxVal); alpha = 256.0d / (max - min); beta = -min * alpha; /** conversion of both matrices to 8 bit */ negativeMat.convertTo(negativeMat, CvType.CV_8UC1, alpha, beta); positiveMat.convertTo(positiveMat, CvType.CV_8UC1, alpha, beta); /** create additional mat for saturated green */ Mat brightMat = negativeMat.clone(); Core.max(negativeMat, positiveMat, brightMat); // Core.absdiff(brightMat, new Scalar(255.0d), brightMat); // Core.multiply(brightMat, new Scalar(1.0d/3.0d), brightMat); /** combine all matrices into one 8 bit 3 channel rgb picture */ Mat tempMat = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC3); List<Mat> mixSrcMats = new ArrayList<>(); mixSrcMats.add(negativeMat); // 1 channel: 0 mixSrcMats.add(positiveMat); // 1 channel: 1 mixSrcMats.add(brightMat); // 1 channel: 2 List<Mat> mixDstMats = new ArrayList<>(); mixDstMats.add(tempMat); // 3 channels: 0-2 MatOfInt fromToMat = new MatOfInt(0, 0 /* neg->red */, 2, 1/* * avg->green */, 1, 2 /* * pos- * > * blue */); Core.mixChannels(mixSrcMats, mixDstMats, fromToMat); return tempMat; }