public static Mat thresholdOtsu(Mat mat, boolean invert) { Imgproc.threshold(mat, mat, 0, 255, (invert ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY) | Imgproc.THRESH_OTSU); return mat; }
public static List<Element> detectChanges(Mat base, Mat mChanged) { int PIXEL_DIFF_THRESHOLD = 3; int IMAGE_DIFF_THRESHOLD = 5; Mat mBaseGray = Element.getNewMat(); Mat mChangedGray = Element.getNewMat(); Mat mDiffAbs = Element.getNewMat(); Mat mDiffTresh = Element.getNewMat(); Mat mChanges = Element.getNewMat(); List<Element> rectangles = new ArrayList<>(); Imgproc.cvtColor(base, mBaseGray, toGray); Imgproc.cvtColor(mChanged, mChangedGray, toGray); Core.absdiff(mBaseGray, mChangedGray, mDiffAbs); Imgproc.threshold(mDiffAbs, mDiffTresh, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO); if (Core.countNonZero(mDiffTresh) > IMAGE_DIFF_THRESHOLD) { Imgproc.threshold(mDiffAbs, mDiffAbs, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY); Imgproc.dilate(mDiffAbs, mDiffAbs, Element.getNewMat()); Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5)); Imgproc.morphologyEx(mDiffAbs, mDiffAbs, Imgproc.MORPH_CLOSE, se); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat mHierarchy = Element.getNewMat(); Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); rectangles = contoursToRectangle(contours); Core.subtract(mDiffAbs, mDiffAbs, mChanges); Imgproc.drawContours(mChanges, contours, -1, new Scalar(255)); //logShow(mDiffAbs); } return rectangles; }
@Override public Result process(CvPipeline pipeline) throws Exception { Mat mat = pipeline.getWorkingImage(); int type = invert ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY; type |= auto ? Imgproc.THRESH_OTSU : 0; Imgproc.threshold(mat, mat, threshold, 255, type); return null; } }
public FluentCv threshold(double threshold, boolean invert, String... tag) { int type = invert ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY; if (threshold == 0) { type |= Imgproc.THRESH_OTSU; } Imgproc.threshold(mat, mat, threshold, 255, type); return store(mat, tag); }
protected void processOperation() { if(adaptiveMeanString.equals(thresholdMode)){ Imgproc.adaptiveThreshold(originalImage, image, maxval, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, blockSize, constantC); } else if(noneString.equals(thresholdMode)){ image = originalImage.clone(); } else{ Imgproc.threshold(originalImage, image, level, maxval, modeMap.get(thresholdMode)); } updateView(image); }
Imgproc.threshold(edgesImage_, edgesImage_, 0, 1, Imgproc.THRESH_BINARY); Log.v(TAG, "Canny took: " + (System.currentTimeMillis() - start));
private void resetImage() { image = originalImage.clone(); Mat temp = new Mat(); Imgproc.cvtColor(image, temp,Imgproc.COLOR_BGR2GRAY); Imgproc.threshold(temp, binary, imageThreshold, 255.0, Imgproc.THRESH_BINARY_INV); updateView(); }
protected void processOperation() { Imgproc.Canny(originalImage, image, 220, 255, 3, false); Imgproc.threshold(image, image, 100, 255, Imgproc.THRESH_BINARY_INV); Imgproc.distanceTransform(image, image, Imgproc.CV_DIST_L2, 3); image.convertTo(image, CvType.CV_8UC1); Core.multiply(image, new Scalar(20), image); updateView(); }
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); 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; }
public Mat process(Mat inputImage) { Mat foregroundThresh = new Mat(); // Firstly, convert to gray-level image, yields good results with performance Imgproc.cvtColor(inputImage, inputGray, Imgproc.COLOR_BGR2GRAY); // initialize background to 1st frame, convert to floating type if (accumulatedBackground.empty()) inputGray.convertTo(accumulatedBackground, CvType.CV_32F); // convert background to 8U, for differencing with input image accumulatedBackground.convertTo(backImage,CvType.CV_8U); // compute difference between image and background Core.absdiff(backImage,inputGray,foreground); // apply threshold to foreground image Imgproc.threshold(foreground,foregroundThresh, threshold,255, Imgproc.THRESH_BINARY_INV); // accumulate background Mat inputFloating = new Mat(); inputGray.convertTo(inputFloating, CvType.CV_32F); Imgproc.accumulateWeighted(inputFloating, accumulatedBackground,learningRate, foregroundThresh); return negative(foregroundThresh); }
public List<MatOfPoint> getIsoDoseContourPoints(KeyDouble slicePosition, double isoDoseThreshold) { List<MatOfPoint> contours = new ArrayList<>(); // Convert from threshold in cCy to raw pixel value threshold double rawThreshold = (isoDoseThreshold / 100) / this.doseGridScaling; DicomImageElement dosePlane = (DicomImageElement) this.getDosePlaneBySlice(slicePosition.getValue()); int rows = dosePlane.getImage().toMat().rows(); int cols = dosePlane.getImage().toMat().cols(); Mat src = new Mat(rows, cols, CvType.CV_32FC1); Mat thr = new Mat(rows, cols, CvType.CV_32FC1); dosePlane.getImage().toMat().convertTo(src, CvType.CV_32FC1); Mat hierarchy = new Mat(); Imgproc.threshold(src, thr, rawThreshold, 255, Imgproc.THRESH_BINARY); Mat thrSrc = new Mat(rows, cols, CvType.CV_8U); thr.convertTo(thrSrc, CvType.CV_8U); Imgproc.findContours(thrSrc, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); return contours; }
Imgproc.threshold(disparityImage, disparityThreshold, gui.getLevel(), 255.0f, Imgproc.THRESH_BINARY); maskedImage.setTo(new Scalar(0,0,0)); colorImage.copyTo(maskedImage,disparityThreshold);