List of usage examples for org.opencv.core Core mean
public static Scalar mean(Mat src)
From source file:Questao3.java
void media() { /**/*from w w w . j a v a 2 s.c o m*/ * Core.mean(Mat) retorna o valor da media dos valores */ Scalar d = Core.mean(ruido); System.out.println("Mdia do rudo: " + d.val[0]); }
From source file:classes.FloodFiller.java
private void fillFrom(Point seed, int lo, int up, Scalar backgroundColor, Scalar contourFillingColor) { Mat object = ObjectGenerator.extract(image, seed.x, seed.y, 10, 10); this.meanColor = Core.mean(object); Rect ccomp = new Rect(); Mat mask = Mat.zeros(image.rows() + 2, image.cols() + 2, CvType.CV_8UC1); int connectivity = 4; int newMaskVal = 255; int ffillMode = 1; int flags = connectivity + (newMaskVal << 8) + (ffillMode == 1 ? Imgproc.FLOODFILL_FIXED_RANGE : 0); Scalar newVal = new Scalar(0.299, 0.587, 0.114); Imgproc.threshold(mask, mask, 1, 128, Imgproc.THRESH_BINARY); filledArea = Imgproc.floodFill(image.clone(), mask, seed, newVal, ccomp, new Scalar(lo, lo, lo), new Scalar(up, up, up), flags); // Highgui.imwrite("mask.png", mask); ImageUtils.saveImage(mask, "mask.png", request); morphologicalImage = new Mat(image.size(), CvType.CV_8UC3); Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1)); ArrayList<Mat> mask3 = new ArrayList<Mat>(); mask3.add(mask);//from w ww . j a va2 s . c o m mask3.add(mask); mask3.add(mask); Core.merge(mask3, mask); // Applying morphological filters Imgproc.erode(mask, morphologicalImage, element); Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_CLOSE, element, new Point(-1, -1), 9); Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_OPEN, element, new Point(-1, -1), 2); Imgproc.resize(morphologicalImage, morphologicalImage, image.size()); // Highgui.imwrite("morphologicalImage.png", morphologicalImage); ImageUtils.saveImage(morphologicalImage, "morphologicalImage.png", request); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Core.split(mask, mask3); Mat binarymorphologicalImage = mask3.get(0); Imgproc.findContours(binarymorphologicalImage.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); contoursImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor); int thickness = -1; // Thicknes should be lower than zero in order to drawn the filled contours Imgproc.drawContours(contoursImage, contours, -1, contourFillingColor, thickness); // Drawing all the contours found // Highgui.imwrite("allContoursImage.png", contoursImage); ImageUtils.saveImage(contoursImage, "allContoursImage.png", request); if (contours.size() > 1) { int minContourWith = 20; int minContourHeight = 20; int maxContourWith = 6400 / 2; int maxContourHeight = 4800 / 2; contours = filterContours(contours, minContourWith, minContourHeight, maxContourWith, maxContourHeight); } if (contours.size() > 0) { MatOfPoint biggestContour = contours.get(0); // getting the biggest contour contourArea = Imgproc.contourArea(biggestContour); if (contours.size() > 1) { biggestContour = Collections.max(contours, new ContourComparator()); // getting the biggest contour in case there are more than one } Point[] points = biggestContour.toArray(); path = "M " + (int) points[0].x + " " + (int) points[0].y + " "; for (int i = 1; i < points.length; ++i) { Point v = points[i]; path += "L " + (int) v.x + " " + (int) v.y + " "; } path += "Z"; biggestContourImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor); Imgproc.drawContours(biggestContourImage, contours, 0, contourFillingColor, thickness); // Highgui.imwrite("biggestContourImage.png", biggestContourImage); ImageUtils.saveImage(biggestContourImage, "biggestContourImage.png", request); Mat maskForColorExtraction = biggestContourImage.clone(); if (isWhite(backgroundColor)) { Imgproc.dilate(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3); } else { Imgproc.erode(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3); } // Highgui.imwrite("maskForColorExtraction.png", maskForColorExtraction); ImageUtils.saveImage(maskForColorExtraction, "maskForColorExtraction.png", request); Mat extractedColor = new Mat(); if (isBlack(backgroundColor) && isWhite(contourFillingColor)) { Core.bitwise_and(maskForColorExtraction, image, extractedColor); } else { Core.bitwise_or(maskForColorExtraction, image, extractedColor); } // Highgui.imwrite("extractedColor.png", extractedColor); ImageUtils.saveImage(extractedColor, "extractedColor.png", request); computedSearchWindow = Imgproc.boundingRect(biggestContour); topLeftCorner = computedSearchWindow.tl(); Rect croppingRect = new Rect(computedSearchWindow.x, computedSearchWindow.y, computedSearchWindow.width - 1, computedSearchWindow.height - 1); Mat imageForTextRecognition = new Mat(extractedColor.clone(), croppingRect); // Highgui.imwrite(outImageName, imageForTextRecognition); ImageUtils.saveImage(imageForTextRecognition, outImageName, request); // // // Mat data = new Mat(imageForTextRecognition.size(), CvType.CV_8UC3, backgroundColor); // imageForTextRecognition.copyTo(data); // data.convertTo(data, CvType.CV_8UC3); // // // The meanColor variable represents the color in the GBR space, the following line transforms this to the RGB color space, which // // is assumed in the prepareImage method of the TextRecognitionPreparer class // Scalar userColor = new Scalar(meanColor.val[2], meanColor.val[1], meanColor.val[0]); // // ArrayList<String> recognizableImageNames = TextRecognitionPreparer.generateRecognizableImagesNames(data, backgroundColor, userColor); // for (String imageName : recognizableImageNames) { // // try { // // First recognition step // String recognizedText = TextRecognizer.recognize(imageName, true).trim(); // if (recognizedText != null && !recognizedText.isEmpty()) { // recognizedStrings.add(recognizedText); // } // // Second recognition step // recognizedText = TextRecognizer.recognize(imageName, false).trim(); // if (recognizedText != null && !recognizedText.isEmpty()) { // recognizedStrings.add(recognizedText); // } // // } catch (Exception e) { // } // } // //// ArrayList<BufferedImage> recognizableBufferedImages = TextRecognitionPreparer.generateRecognizableBufferedImages(data, backgroundColor, userColor); //// for (BufferedImage bufferedImage : recognizableBufferedImages) { //// try { //// // First recognition step //// String recognizedText = TextRecognizer.recognize(bufferedImage, true).trim(); //// if (recognizedText != null && !recognizedText.isEmpty()) { //// recognizedStrings.add(recognizedText); //// } //// // Second recognition step //// recognizedText = TextRecognizer.recognize(bufferedImage, false).trim(); //// if (recognizedText != null && !recognizedText.isEmpty()) { //// recognizedStrings.add(recognizedText); //// } //// //// } catch (Exception e) { //// } //// } // // // // compute all moments Moments mom = Imgproc.moments(biggestContour); massCenter = new Point(mom.get_m10() / mom.get_m00(), mom.get_m01() / mom.get_m00()); // draw black dot Core.circle(contoursImage, massCenter, 4, contourFillingColor, 8); } }
From source file:classes.ObjectFinder.java
public ObjectFinder(Mat objectImage, ArrayList<Integer> thresholdsVector) { this.objectImage = objectImage; Highgui.imwrite("objectImage.png", objectImage); this.thresholdsVector = thresholdsVector; this.objectHistogram = new Mat(); this.meanColor = Core.mean(objectImage); computeObjectHistogram();//from w ww . j a v a 2 s . c om }
From source file:com.blogspot.thedsweb.engine.Brightness.java
License:Open Source License
private int meanCalculation(Mat rgb) { // Convert RGB to YcrCb for easier luminance calculation final Mat yCrCb = new Mat(); Imgproc.cvtColor(rgb, yCrCb, Imgproc.COLOR_RGB2YCrCb); // Calculate luminance final Scalar mainMean = Core.mean(yCrCb); int meanLumaValue = (int) mainMean.val[0]; // Test if backlit conditions are true if (!firstRun && backlitDetection(yCrCb, mainMean, meanLumaValue)) { Debug.LOG.log(Level.CONFIG, "Backlit detected."); backlit = true;//from w ww. j a va 2 s .co m if (meanLumaValue < current) { meanLumaValue = current; } } else { backlit = false; } // Set the first run parameter to false so that the next time the mean // calculation method is called the backlit detection is also running if (firstRun) { firstRun = false; } // If the frame is completely black or white the camera must already in // use by another program if (meanLumaValue == 0) { meanLumaValue = current; } return meanLumaValue; }
From source file:com.blogspot.thedsweb.engine.Brightness.java
License:Open Source License
private boolean backlitDetection(Mat yCrCb, Scalar mainMean, int mainMeanLumaValue) { // Save the mean brightness and chroma value of the whole frame. Plus // the width and height of the frame. final int mainMeanChromaValue = (int) mainMean.val[2]; final int PARTS = 8; final int w = yCrCb.width(); final int h = yCrCb.height(); final int wStep = w >> 2; final int hStep = h >> 1; // Separate the image into 8 equal parts. final Mat[] tiles = new Mat[PARTS]; tiles[0] = yCrCb.submat(0, hStep, 0, wStep); tiles[1] = yCrCb.submat(0, hStep, wStep, wStep * 2); tiles[2] = yCrCb.submat(hStep, h, 0, wStep); tiles[3] = yCrCb.submat(hStep, h, wStep, wStep * 2); tiles[4] = yCrCb.submat(0, hStep, wStep * 2, wStep * 3); tiles[5] = yCrCb.submat(0, hStep, wStep * 2, w); tiles[6] = yCrCb.submat(hStep, h, wStep * 2, wStep * 3); tiles[7] = yCrCb.submat(hStep, h, wStep * 2, w); // Calculate the mean value of all parts. final Scalar[] tileMean = new Scalar[PARTS]; for (int i = 0; i < tileMean.length; i++) { tileMean[i] = Core.mean(tiles[i]); }//from ww w .ja va 2 s . co m // Save the mean brightness and chroma of all parts. final int[] tileMeanLuma = new int[PARTS]; final int[] tileMeanChroma = new int[PARTS]; // Save min and max value in container MinMaxContainer con = null; for (int j = 0; j < tileMean.length; j++) { tileMeanLuma[j] = (int) tileMean[j].val[0]; if (j == 0) { con = new MinMaxContainer(tileMeanLuma[j]); } else { con.add(tileMeanLuma[j]); } tileMeanChroma[j] = (int) tileMean[j].val[2]; } // Get the highest and lowest brightness value of all frame tiles. final int min = con.getMin(); final int max = con.getMax() >> 1; // Check if their is a consistent chroma distribution and a brightness // spike to detect backlit conditions. if (checkChroma(tileMeanChroma, mainMeanChromaValue) && min < max) { return true; } return false; }
From source file:com.example.afs.makingmusic.process.MusicGenerator.java
License:Open Source License
private Player findPlayer(Frame frame, Rect item) { int index;/*from w w w. j a v a2s.c om*/ if (assignmentMethod == null || assignmentMethod == AssignmentMethod.POSITION) { int height = frame.getImageMatrix().height(); index = MulDiv.scale(height, item.y, players.size()); } else { Mat itemMat = frame.getImageMatrix().submat(item); Scalar mean = Core.mean(itemMat); float[] hsb = new float[3]; Color.RGBtoHSB((int) mean.val[2], (int) mean.val[1], (int) mean.val[0], hsb); int hue = (int) (hsb[0] * 360); // Red hues are 0-30 and 330-360 so rotate by 30 to group together if (hue > 330) { hue = hue - 330; } else { hue += 30; } // RGBtoHSB hsb[0] is 0-1 inclusive for a total of 361 hue values index = MulDiv.scale(361, hue, players.size()); } return players.get(index); }
From source file:com.shootoff.camera.autocalibration.AutoCalibrationManager.java
License:Open Source License
private void findColors(Mat frame, Mat warpedBoardCorners) { final Point rCenter = findChessBoardSquareCenter(warpedBoardCorners, 2, 3); final Point gCenter = findChessBoardSquareCenter(warpedBoardCorners, 2, 5); final Point bCenter = findChessBoardSquareCenter(warpedBoardCorners, 2, 7); if (logger.isDebugEnabled()) { logger.debug("findColors {} {} {}", rCenter, gCenter, bCenter); logger.debug("findColors r {} {} {} {}", (int) rCenter.y - 10, (int) rCenter.y + 10, (int) rCenter.x - 10, (int) rCenter.x + 10); }// w w w . j a va2s . c o m final Scalar rMeanColor = Core.mean(frame.submat((int) rCenter.y - 10, (int) rCenter.y + 10, (int) rCenter.x - 10, (int) rCenter.x + 10)); final Scalar gMeanColor = Core.mean(frame.submat((int) gCenter.y - 10, (int) gCenter.y + 10, (int) gCenter.x - 10, (int) gCenter.x + 10)); final Scalar bMeanColor = Core.mean(frame.submat((int) bCenter.y - 10, (int) bCenter.y + 10, (int) bCenter.x - 10, (int) bCenter.x + 10)); if (logger.isTraceEnabled()) { String filename = String.format("rColor.png"); File file = new File(filename); filename = file.toString(); Highgui.imwrite(filename, frame.submat((int) rCenter.y - 10, (int) rCenter.y + 10, (int) rCenter.x - 10, (int) rCenter.x + 10)); filename = String.format("gColor.png"); file = new File(filename); filename = file.toString(); Highgui.imwrite(filename, frame.submat((int) gCenter.y - 10, (int) gCenter.y + 10, (int) gCenter.x - 10, (int) gCenter.x + 10)); filename = String.format("bColor.png"); file = new File(filename); filename = file.toString(); Highgui.imwrite(filename, frame.submat((int) bCenter.y - 10, (int) bCenter.y + 10, (int) bCenter.x - 10, (int) bCenter.x + 10)); } if (logger.isDebugEnabled()) logger.debug("meanColor {} {} {}", rMeanColor, gMeanColor, bMeanColor); }
From source file:ctPrincipal.Graficos.java
public double calculaMedia() { Scalar d = Core.mean(ruido); return d.val[0]; }
From source file:cubesolversimulator.VisualInputForm.java
private void findAvg(List<Rect> roi) { cols = new int[9]; for (int i = 0; i < roi.size(); i++) { Mat ri = new Mat(blured, roi.get(i)); ri.convertTo(ri, -1, 1.0);//ww w.j a va 2 s .c o m Highgui.imwrite("extract" + i + ".jpg", ri); Scalar clr = new Scalar(0, 0, 0); clr = Core.mean(ri); System.out.println("old col: " + clr); if (clr.val[0] > 30) clr.val[0] = clr.val[0] - 30; else clr.val[0] = 0; //if(clr.val[2]<200) // clr.val[2]=clr.val[2]+20; //else // clr.val[0]=0; System.out.println("new col: " + clr); getDistance(clr, i); } displayLabel(); }
From source file:info.jmfavreau.bifrostcore.imageprocessing.ImageToColor.java
License:Open Source License
private Mat threshold_mask(Mat main_region) { Mat result = main_region.clone();//from ww w . j av a 2 s . c o m Imgproc.threshold(main_region, result, Core.mean(main_region).val[0], 255, Imgproc.THRESH_TOZERO); return result; }