List of usage examples for org.opencv.core Core countNonZero
public static int countNonZero(Mat src)
From source file:com.example.colordetector.CamMainActivity.java
License:Apache License
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { // The frame currently captured by the camera, converted in the color RGBA rgbaFrame = inputFrame.rgba();/*from w w w .ja v a 2 s. co m*/ // Convert the frame in the HSV color space, to be able to identify the color with the thresholds Imgproc.cvtColor(rgbaFrame, rgbFrame, Imgproc.COLOR_RGBA2RGB); // Cant't convert directly rgba->hsv Imgproc.cvtColor(rgbFrame, hsvFrame, Imgproc.COLOR_RGB2HSV); // Create a mask with ONLY zones of the chosen color on the frame currently captured Core.inRange(hsvFrame, thresMin, thresMax, inRangeMask); filteredFrame.setTo(new Scalar(0, 0, 0)); rgbFrame.copyTo(filteredFrame, inRangeMask); // if the method of shooting image is set to manual, exit and return the filtered image... if (!methodAuto) { return filteredFrame; } //...else it was setted the automatic method, so continue with the method // Check the H channel of the image to see if the searched color is present on the frame Core.extractChannel(filteredFrame, hChannel, 0); /* There are two method to verify the color presence; below a little explanation */ /* checkRange: if almost one pixel of the searched color is found, continue with the countdown * Pro -> fast. * Versus -> less accurate, possible presence of false positive depending the quality of the camera * if(!Core.checkRange(hChannel, true, 0, 1)){ */ /* Percentage: count the pixel of the searched color, and if there are almost the * 0.1% of total pixel of the frame with the searched color, continue with the countdown * Pro: more accurate, lower risk of false positive * Versus: slower than checkRange * N.B.: the threshold percentage is imposted with a low value, otherwise small object will not be seen */ int perc = Core.countNonZero(hChannel); // Percentage if (perc > (frameDim * 0.001)) { // if the shooting method is setted to 'immediate', the photo is returned now; // otherwise continue with the countdown if (!countDown) { takePicture(); return rgbaFrame; } // 'point' is where the countdown will be visualized; in that case at // a quarter of height and width than left up angle Point point = new Point(rgbaFrame.cols() >> 2, rgbaFrame.rows() >> 2); // Update the osd countdown every 75*8 ms (if color searched is present) // Use the division in 75 ms cause a higher value would give the user the feeling of screen/app 'blocked'. if (timeToElapse % 8 == 0) { if (osdSecond.compareTo("") == 0) osdSecond = ((Integer) (timeToElapse >> 3)).toString(); else osdSecond = osdSecond.concat(".." + (((Integer) (timeToElapse >> 3)).toString())); Core.putText(rgbaFrame, osdSecond, point, 1, 3, Scalar.all(255)); } timeToElapse -= 1; // the user has framed an object for more than 3 seconds; shoot the photo if (timeToElapse <= 0) { timeToElapse = 24; takePicture(); } // the user has framed an object for less than 3 seconds; wait else { try { synchronized (this) { wait(75); } } catch (InterruptedException e) { e.printStackTrace(); } } } // the user has NOT framed a color searched object; reset osd else { timeToElapse = 24; osdSecond = ""; } return rgbaFrame; }
From source file:com.sikulix.core.Finder.java
License:Open Source License
public boolean hasChanges(Mat base, Mat current) { int PIXEL_DIFF_THRESHOLD = 5; int IMAGE_DIFF_THRESHOLD = 5; Mat bg = new Mat(); Mat cg = new Mat(); Mat diff = new Mat(); Mat tdiff = new Mat(); Imgproc.cvtColor(base, bg, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(current, cg, Imgproc.COLOR_BGR2GRAY); Core.absdiff(bg, cg, diff);//from w ww . j ava2s .c o m Imgproc.threshold(diff, tdiff, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO); if (Core.countNonZero(tdiff) <= IMAGE_DIFF_THRESHOLD) { return false; } Imgproc.threshold(diff, diff, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY); Imgproc.dilate(diff, diff, new Mat()); Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5)); Imgproc.morphologyEx(diff, diff, Imgproc.MORPH_CLOSE, se); List<MatOfPoint> points = new ArrayList<MatOfPoint>(); Mat contours = new Mat(); Imgproc.findContours(diff, points, contours, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); int n = 0; for (Mat pm : points) { log.trace("(%d) %s", n++, pm); printMatI(pm); } log.trace("contours: %s", contours); printMatI(contours); return true; }
From source file:cv.recon.controller.OutputDisplayController.java
License:Open Source License
/** * Subtract background using BackgroundSubtractorMOG2 * @param src Source Mat/*from w w w.j av a2 s . c om*/ */ private void subtractBackground(Mat src) { if (bsmog != null) { bsmog.apply(src, fgMask); Imgproc.erode(fgMask, fgMask, kernel); Imgproc.dilate(fgMask, fgMask, kernel); output.setTo(new Scalar(0)); src.copyTo(output, fgMask); if (isFirstFrame) { nonZeroCount = 0; isFirstFrame = false; } else { nonZeroCount = Core.countNonZero(fgMask); } nonZeroLabel.setText("" + nonZeroCount); } }
From source file:info.jmfavreau.bifrostcore.imageprocessing.ImageToColor.java
License:Open Source License
private Mat extract_main_region(Mat img, Mat roi) { Mat hist = new Mat(); int h_bins = 30; int s_bins = 32; MatOfInt mHistSize = new MatOfInt(h_bins, s_bins); MatOfFloat mRanges = new MatOfFloat(0, 179, 0, 255); MatOfInt mChannels = new MatOfInt(0, 1); Imgproc.calcHist(Arrays.asList(img), mChannels, roi, hist, mHistSize, mRanges, false); Core.normalize(hist, hist, 0, 255, Core.NORM_MINMAX, -1, new Mat()); Mat backproj = new Mat(); Imgproc.calcBackProject(Arrays.asList(img), mChannels, hist, backproj, mRanges, 1); Log.w("bifrostcore", "Number of pixels in the biggest region: " + String.valueOf(Core.countNonZero(backproj))); return backproj.mul(roi); }
From source file:logic.featurepointextractor.EyeBrowsFPE.java
/** * getSkeleton obtain thin 1-pixel region from contour. * @param src input binary image/*from w w w .ja v a2s . c o m*/ * @return binary image */ private Mat getSkeleton(Mat src) { Mat skel = new Mat(src.rows(), src.cols(), CV_8UC1, new Scalar(0)); Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(3, 3)); Mat tmp = new Mat(); Mat eroded = new Mat(); boolean done = false; do { Imgproc.morphologyEx(src, eroded, Imgproc.MORPH_ERODE, element); Imgproc.morphologyEx(eroded, tmp, Imgproc.MORPH_DILATE, element); Core.subtract(src, tmp, tmp); Core.bitwise_or(skel, tmp, skel); eroded.copyTo(src); done = (Core.countNonZero(src) == 0); } while (!done); return skel; }
From source file:org.pattern.utils.MatUtils.java
/** * Compares if two image matrices contains similar data. * // w w w. j av a 2s.c o m * @param mat1 * @param mat2 * @return */ public static boolean similar(Mat mat1, Mat mat2) { if (mat1.cols() != mat2.cols() || mat1.rows() != mat2.rows()) { return false; } Mat mat = new Mat(); Core.compare(mat1, mat2, mat, Core.CMP_EQ); return Core.countNonZero(mat) != 0; }
From source file:org.sikuli.script.Finder.java
License:MIT License
public boolean hasChanges(Mat current) { int PIXEL_DIFF_THRESHOLD = 5; int IMAGE_DIFF_THRESHOLD = 5; Mat bg = new Mat(); Mat cg = new Mat(); Mat diff = new Mat(); Mat tdiff = new Mat(); Imgproc.cvtColor(base, bg, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(current, cg, Imgproc.COLOR_BGR2GRAY); Core.absdiff(bg, cg, diff);/* www.j a v a2 s . c o m*/ Imgproc.threshold(diff, tdiff, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO); if (Core.countNonZero(tdiff) <= IMAGE_DIFF_THRESHOLD) { return false; } Imgproc.threshold(diff, diff, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY); Imgproc.dilate(diff, diff, new Mat()); Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5)); Imgproc.morphologyEx(diff, diff, Imgproc.MORPH_CLOSE, se); List<MatOfPoint> points = new ArrayList<MatOfPoint>(); Mat contours = new Mat(); Imgproc.findContours(diff, points, contours, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); int n = 0; for (Mat pm : points) { log(lvl, "(%d) %s", n++, pm); printMatI(pm); } log(lvl, "contours: %s", contours); printMatI(contours); return true; }