List of usage examples for org.opencv.core Mat Mat
public Mat(Mat m, Range rowRange, Range colRange)
From source file:cx.uni.jk.mms.iaip.mat.MatModel.java
License:Open Source License
/** * loads and decodes an image with Java ImageIO. * //w w w. j av a 2 s.co m * drawback: only gray scale images allowed, fewer file types supported. @see * <a * href="http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html"> * java tutorials</a> for details. * * @throws UnsupportedImageTypeException * if the image is not an 8 bit gray scale image. */ protected Mat loadAndDecodeImageWithJavaImageIO(Path path) throws IOException { this.logger.finer("Loading and decoding image with Java ImageIO."); BufferedImage img = ImageIO.read(path.toUri().toURL()); int cvType; switch (img.getType()) { case BufferedImage.TYPE_BYTE_GRAY: cvType = CvType.CV_8U; break; default: throw new UnsupportedImageTypeException(); } Mat mat = new Mat(img.getHeight(), img.getWidth(), cvType); mat.put(0, 0, ((DataBufferByte) img.getRaster().getDataBuffer()).getData()); return mat; }
From source file:depthDataFromStereoCamsOpenCV.ProcessImages.java
/** * BufferedImage2Mat//from w w w . j a va 2 s . com * @param BufferedImage image * @return Mat */ public static Mat BufferedImage2Mat(BufferedImage image) { //source: http://stackoverflow.com/questions/18581633/fill-in-and-detect-contour-rectangles-in-java-opencv byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8U); mat.put(0, 0, data); return mat; }
From source file:edu.sust.cse.util.Histogram.java
public static Mat getHistogram(Mat image) { try {/* w w w . j ava 2 s .c o m*/ Mat src = new Mat(image.height(), image.width(), CvType.CV_8UC2); Imgproc.cvtColor(image, src, Imgproc.COLOR_RGB2GRAY); ArrayList<Mat> bgr_planes = new ArrayList<>(); Core.split(src, bgr_planes); MatOfInt histSize = new MatOfInt(256); final MatOfFloat histRange = new MatOfFloat(0f, 256f); boolean accumulate = false; Mat b_hist = new Mat(); Imgproc.calcHist(bgr_planes, new MatOfInt(0), new Mat(), b_hist, histSize, histRange, accumulate); int hist_w = 512; int hist_h = 600; long bin_w; bin_w = Math.round((double) (hist_w / 256)); Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC1); Core.normalize(b_hist, b_hist, 3, histImage.rows(), Core.NORM_MINMAX); for (int i = 1; i < 256; i++) { Core.line(histImage, new Point(bin_w * (i - 1), hist_h - Math.round(b_hist.get(i - 1, 0)[0])), new Point(bin_w * (i), hist_h - Math.round(Math.round(b_hist.get(i, 0)[0]))), new Scalar(255, 0, 0), 2, 8, 0); } return histImage; } catch (Exception ex) { System.out.println("[HISTOGRAM][ERROR][" + ex.getMessage() + "]"); return null; } }
From source file:emotion.Eye.java
private void templatingOuterCorner(Mat eyeRegion, boolean rightEyeFlag) { // Mat template=imread("E:\\Studia\\II YEAR\\Team Project\\" // + "Face database\\eyecorners\\rightOuter.jpg",CV_8UC1); Mat template = imread("src\\Templates\\rightOuter.jpg", CV_8UC1); Mat temp = new Mat(eyeRegion.width(), eyeRegion.height(), CV_8UC1); cvtColor(eyeRegion, temp, Imgproc.COLOR_BGR2GRAY); temp = rightEyeFlag//w w w .j a va 2 s . c o m ? new Mat(temp, new Rect((int) (temp.width() * 0.5), 0, (int) (temp.width() * 0.5), temp.height())) : new Mat(temp, new Rect(0, 0, (int) (temp.width() * 0.5), temp.height())); Mat result = new Mat(eyeRegion.width(), eyeRegion.height(), eyeRegion.type()); //(9,9)- coordinates of eye outerCorner in the template if (rightEyeFlag) { imwrite("rightEyeForOuterTemplating.jpg", temp); Imgproc.matchTemplate(temp, template, result, Imgproc.TM_CCOEFF_NORMED); Core.normalize(result, result, 0, 100, Core.NORM_MINMAX); Core.MinMaxLocResult maxVal = Core.minMaxLoc(result); //(9,9)- coordinates of eye outerCorner in the template Point outerCorner = new Point(maxVal.maxLoc.x + 9, maxVal.maxLoc.y + 9); //Adjust coordinates according to whole face outerCorner.y += Eye.rightRect.y; outerCorner.x += Eye.rightRect.x; outerCorner.x += temp.width(); //We examine just right half on the right eye //////////////////////////////////////////// EyeRegion.rightOuterEyeCorner = outerCorner; } else { imwrite("leftEyeForOuterTemplating.jpg", temp); Core.flip(template, template, 1); Imgproc.matchTemplate(temp, template, result, Imgproc.TM_CCOEFF_NORMED); Core.normalize(result, result, 0, 100, Core.NORM_MINMAX); Core.MinMaxLocResult maxVal = Core.minMaxLoc(result); Point outerCorner = new Point(maxVal.maxLoc.x + 4, maxVal.maxLoc.y + 9); //Adjust coordinates according to whole face outerCorner.y += Eye.leftRect.y; outerCorner.x += Eye.leftRect.x; //////////////////////////////////////////// EyeRegion.leftOuterEyeCorner = outerCorner; } //Mat tempw=reg._face.clone(); //Face.drawCross(tempw, outerCorner); //imwrite("checkcorner.png",tempw); }
From source file:emotion.Eye.java
private void templatingInnerCorner(Mat eyeRegion, boolean rightEyeFlag) { // Mat template=imread("E:\\Studia\\II YEAR\\Team Project\\" // + "Face database\\eyecorners\\rightInner.jpg",CV_8UC1); Mat template = imread("src\\Templates\\rightInner.jpg", CV_8UC1); Mat temp = new Mat(eyeRegion.width(), eyeRegion.height(), CV_8UC1); cvtColor(eyeRegion, temp, Imgproc.COLOR_BGR2GRAY); temp = rightEyeFlag ? new Mat(temp, new Rect(0, 0, (int) (temp.width() * 0.5), temp.height())) : new Mat(temp, new Rect((int) (temp.width() * 0.5), 0, (int) (temp.width() * 0.5), temp.height())); Mat result = new Mat(eyeRegion.width(), eyeRegion.height(), eyeRegion.type()); //(4,7)- coordinates of eye innerCorner in the template if (rightEyeFlag) { imwrite("template4righteye.jpg", template); imwrite("rightEyeForInnerTemplating.jpg", temp); Imgproc.matchTemplate(temp, template, result, Imgproc.TM_CCOEFF_NORMED); Core.normalize(result, result, 0, 100, Core.NORM_MINMAX); Core.MinMaxLocResult maxVal = Core.minMaxLoc(result); //(4,7)- coordinates of eye innerCorner in the template Point innerCorner = new Point(maxVal.maxLoc.x + 4, maxVal.maxLoc.y + 7); StaticFunctions.drawCross(temp, innerCorner, StaticFunctions.Features.EYE_CORNERS); imwrite("rightEyeForInnerTemplating.jpg", temp); //Adjust coordinates according to whole face innerCorner.y += Eye.rightRect.y; innerCorner.x += Eye.rightRect.x; //We examine just left half on the right eye //////////////////////////////////////////// EyeRegion.rightInnerEyeCorner = innerCorner; } else {//w ww .j av a2s . c o m imwrite("leftEyeForInnerTemplating.jpg", temp); Core.flip(template, template, 1); Imgproc.matchTemplate(temp, template, result, Imgproc.TM_CCOEFF_NORMED); Core.normalize(result, result, 0, 100, Core.NORM_MINMAX); Core.MinMaxLocResult maxVal = Core.minMaxLoc(result); Point innerCorner = new Point(maxVal.maxLoc.x + 8, maxVal.maxLoc.y + 7); //Adjust coordinates according to whole face innerCorner.y += Eye.leftRect.y; innerCorner.x += Eye.leftRect.x; //We examine just right half on the left eye innerCorner.x += temp.width(); //////////////////////////////////////////// EyeRegion.leftInnerEyeCorner = innerCorner; } }
From source file:emotion.StaticFunctions.java
static public Mat convolution(int[] mask, Mat image) { if (mask.length != 9) return null; //output image Mat destination = new Mat(image.rows(), image.cols(), image.type()); //Convolution kernel Mat kernel = new Mat(3, 3, CvType.CV_32F) { {/*from ww w. j a va 2 s . co m*/ for (int i = 0, it = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j, ++it) { put(i, j, mask[it]); } } } }; Imgproc.filter2D(image, destination, -1, kernel); imwrite("convol.jpg", destination); return destination; }
From source file:es.ugr.osgiliart.features.opencv.Histogram.java
License:Open Source License
@Override public double[] extract(Mat image) { Mat hsvImage = new Mat(image.width(), image.height(), image.type()); Mat histHue = new Mat(); Mat histSaturation = new Mat(); Imgproc.cvtColor(image, hsvImage, Imgproc.COLOR_BGR2HSV); List<Mat> channels = new ArrayList<Mat>(); Core.split(hsvImage, channels);//from ww w .j a v a 2 s. c o m //Histogram for hue Imgproc.calcHist(Arrays.asList(new Mat[] { channels.get(0) }), new MatOfInt(0), new Mat(), histHue, new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE)); //Histogram for saturation Imgproc.calcHist(Arrays.asList(new Mat[] { channels.get(1) }), new MatOfInt(0), new Mat(), histSaturation, new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE)); double sum = Core.sumElems(histHue).val[0]; double[] values = new double[histHue.height() + histSaturation.height()]; int k = 0; for (int i = 0; i < histHue.height(); ++i) { values[k++] = histHue.get(i, 0)[0] / sum; } sum = Core.sumElems(histSaturation).val[0]; for (int i = 0; i < histSaturation.height(); ++i) { values[k++] = histSaturation.get(i, 0)[0] / sum; } return values; }
From source file:es.ugr.osgiliart.features.opencv.MatchImage.java
License:Open Source License
public MatchImage(String templatePath) { Mat template = Highgui.imread(templatePath); Mat resized = new Mat(SIZE, SIZE, template.type()); //Mat blurred = new Mat(); Imgproc.resize(template, resized, new Size(SIZE, SIZE)); //Imgproc.blur(resized, blurred, new Size(FILTER_SIZE,FILTER_SIZE) ); templateChannels = new ArrayList<Mat>(); Core.split(resized, templateChannels); }
From source file:es.ugr.osgiliart.features.opencv.MatchImage.java
License:Open Source License
public double match(String path) { Mat img = Highgui.imread(path);/*from w w w. j a va2s . c om*/ Mat resizedImg = new Mat(SIZE, SIZE, img.type()); //Mat blurredImg = new Mat(); Imgproc.resize(img, resizedImg, new Size(SIZE, SIZE)); //Imgproc.blur(resizedImg, blurredImg, new Size(FILTER_SIZE,FILTER_SIZE) ); ArrayList<Mat> channels = new ArrayList<Mat>(); Core.split(resizedImg, channels); int conta = 0; double corrcoef = 0; for (int i = 0; i < 1; ++i) { /* for(int px = 0; px < SIZE; px++){ for(int py = 0; py < SIZE; py++){ if(resizedImg.get(px, py)[i]!=0.0){ double im_orig = templateChannels.get(i).get(px, py)[0]; double im_indi = resizedImg.get(px, py)[i]; corrcoef += Math.pow(im_orig ,2) - Math.pow(im_indi, 2); conta++; } } }*/ Mat result = new Mat(); Imgproc.matchTemplate(channels.get(i), templateChannels.get(i), result, Imgproc.TM_CCOEFF_NORMED); //Imgproc.matchTemplate(channels.get(i), templateChannels.get(i), result, Imgproc.TM_SQDIFF); corrcoef += result.get(0, 0)[0]; //corrcoef += result.get(0, 0)[0]; } corrcoef /= 3.0; //return (corrcoef/conta/(255*3)); return (corrcoef); }
From source file:es.ugr.osgiliart.features.opencv.MatchImageNoBackground.java
License:Open Source License
public MatchImageNoBackground(String templatePath) { Mat template = Highgui.imread(templatePath); templateResized = new Mat(SIZE, SIZE, template.type()); Imgproc.resize(template, templateResized, new Size(SIZE, SIZE)); }