Java Code Examples for org.opencv.imgproc.Imgproc#morphologyEx()
The following examples show how to use
org.opencv.imgproc.Imgproc#morphologyEx() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenCVNonMavenExamples.java From Java-for-Data-Science with MIT License | 6 votes |
public static void denoise() { String imgInPath = "captchaExample.jpg"; imgInPath = "MyCaptcha.PNG"; imgInPath = "blurredtext.jpg"; String imgOutPath = "captchaNoiseRemovedExample.png"; imgOutPath = "MyNoiseRemovedCaptcha.PNG"; Mat image = Imgcodecs.imread(imgInPath); Mat out = new Mat(); Mat tmp = new Mat(); Mat kernel = new Mat(new Size(3, 3), CvType.CV_8UC1, new Scalar(255)); // Mat kernel = new Mat(image.size(), CvType.CV_8UC1, new Scalar(255)); Imgproc.morphologyEx(image, tmp, Imgproc.MORPH_OPEN, kernel); Imgproc.morphologyEx(tmp, out, Imgproc.MORPH_CLOSE, kernel); Imgcodecs.imwrite(imgOutPath, out); }
Example 2
Source File: StepByStepTestActivity.java From CVScanner with GNU General Public License v3.0 | 6 votes |
Mat buildSkeleton(Mat img){ Mat morph = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_CROSS, new Size(3, 3)); Mat skel = new Mat(img.size(), CvType.CV_8UC1, Scalar.all(0)); Mat eroded = new Mat(); Mat temp = new Mat(); boolean done = false; do{ Imgproc.morphologyEx(img, eroded, Imgproc.MORPH_ERODE, morph); Imgproc.morphologyEx(eroded, temp, Imgproc.MORPH_DILATE, morph); Core.subtract(img, temp, temp); Core.bitwise_or(skel, temp, skel); eroded.copyTo(img); done = Core.countNonZero(img) == 0; }while (!done); return skel; }
Example 3
Source File: Finder.java From SikuliX1 with MIT License | 5 votes |
public static List<Region> findChanges(FindInput2 findInput) { findInput.setAttributes(); Mat previousGray = SXOpenCV.newMat(); Mat nextGray = SXOpenCV.newMat(); Mat mDiffAbs = SXOpenCV.newMat(); Mat mDiffThresh = SXOpenCV.newMat(); Imgproc.cvtColor(findInput.getBase(), previousGray, toGray); Imgproc.cvtColor(findInput.getTarget(), nextGray, toGray); Core.absdiff(previousGray, nextGray, mDiffAbs); Imgproc.threshold(mDiffAbs, mDiffThresh, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO); List<Region> rectangles = new ArrayList<>(); if (Core.countNonZero(mDiffThresh) > IMAGE_DIFF_THRESHOLD) { Imgproc.threshold(mDiffAbs, mDiffAbs, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY); Imgproc.dilate(mDiffAbs, mDiffAbs, SXOpenCV.newMat()); 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 = SXOpenCV.newMat(); Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); for (MatOfPoint contour : contours) { int x1 = 99999; int y1 = 99999; int x2 = 0; int y2 = 0; List<org.opencv.core.Point> points = contour.toList(); for (Point point : points) { int x = (int) point.x; int y = (int) point.y; if (x < x1) x1 = x; if (x > x2) x2 = x; if (y < y1) y1 = y; if (y > y2) y2 = y; } Region rect = new Region(x1, y1, x2 - x1, y2 - y1); rectangles.add(rect); } } return rectangles; }
Example 4
Source File: SXOpenCV.java From SikuliX1 with MIT License | 5 votes |
public static List<Match> doFindChanges(Image original, Image changed) { List<Match> changes = new ArrayList<>(); if (changed.isValid()) { int PIXEL_DIFF_THRESHOLD = 3; int IMAGE_DIFF_THRESHOLD = 5; Mat previousGray = SXOpenCV.newMat(); Mat nextGray = SXOpenCV.newMat(); Mat mDiffAbs = SXOpenCV.newMat(); Mat mDiffTresh = SXOpenCV.newMat(); Imgproc.cvtColor(original.getContent(), previousGray, toGray); Imgproc.cvtColor(changed.getContent(), nextGray, toGray); Core.absdiff(previousGray, nextGray, 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, SXOpenCV.newMat()); 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 = SXOpenCV.newMat(); Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); changes = contoursToRectangle(contours); } } return changes; }
Example 5
Source File: OpenCVNonMavenExamples.java From Java-for-Data-Science with MIT License | 5 votes |
public void noiseRemoval() { // Mat Kernel = cv::Mat(cv::Size(Maximum_Width_of_Noise,Maximum_Height_of_noise),CV_8UC1,cv::Scalar(255)); Mat Kernel = new Mat(new Size(3, 3), CvType.CV_8U, new Scalar(255)); Mat source = Imgcodecs.imread("noiseExample.png"); Mat temp = new Mat(); Mat topHat = new Mat(); Mat destination = new Mat(); Imgproc.morphologyEx(source, temp, Imgproc.MORPH_OPEN, Kernel); Imgproc.morphologyEx(temp, destination, Imgproc.MORPH_CLOSE, Kernel); // Imgproc.morphologyEx(temp, topHat, Imgproc.MORPH_GRADIENT, Kernel); // Imgproc.morphologyEx(topHat, destination, Imgproc.MORPH_CLOSE, Kernel); Imgcodecs.imwrite("noiseRemovedExample.png", source); }
Example 6
Source File: CVProcessor.java From CVScanner with GNU General Public License v3.0 | 5 votes |
public static List<MatOfPoint> findContoursForMRZ(Mat src){ Mat img = src.clone(); src.release(); double ratio = getScaleRatio(img.size()); int width = (int) (img.size().width / ratio); int height = (int) (img.size().height / ratio); Size newSize = new Size(width, height); Mat resizedImg = new Mat(newSize, CvType.CV_8UC4); Imgproc.resize(img, resizedImg, newSize); Mat gray = new Mat(); Imgproc.cvtColor(resizedImg, gray, Imgproc.COLOR_BGR2GRAY); Imgproc.medianBlur(gray, gray, 3); //Imgproc.blur(gray, gray, new Size(3, 3)); Mat morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(13, 5)); Mat dilatedImg = new Mat(); Imgproc.morphologyEx(gray, dilatedImg, Imgproc.MORPH_BLACKHAT, morph); gray.release(); Mat gradX = new Mat(); Imgproc.Sobel(dilatedImg, gradX, CvType.CV_32F, 1, 0); dilatedImg.release(); Core.convertScaleAbs(gradX, gradX, 1, 0); Core.MinMaxLocResult minMax = Core.minMaxLoc(gradX); Core.convertScaleAbs(gradX, gradX, (255/(minMax.maxVal - minMax.minVal)), - ((minMax.minVal * 255) / (minMax.maxVal - minMax.minVal))); Imgproc.morphologyEx(gradX, gradX, Imgproc.MORPH_CLOSE, morph); Mat thresh = new Mat(); Imgproc.threshold(gradX, thresh, 0, 255, Imgproc.THRESH_OTSU); gradX.release(); morph.release(); morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 21)); Imgproc.morphologyEx(thresh, thresh, Imgproc.MORPH_CLOSE, morph); Imgproc.erode(thresh, thresh, new Mat(), new Point(-1, -1), 4); morph.release(); int col = (int) resizedImg.size().width; int p = (int) (resizedImg.size().width * 0.05); int row = (int) resizedImg.size().height; for(int i = 0; i < row; i++) { for(int j = 0; j < p; j++){ thresh.put(i, j, 0); thresh.put(i, col-j, 0); } } List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(thresh, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); hierarchy.release(); Log.d(TAG, "contours found: " + contours.size()); Collections.sort(contours, new Comparator<MatOfPoint>() { @Override public int compare(MatOfPoint o1, MatOfPoint o2) { return Double.valueOf(Imgproc.contourArea(o2)).compareTo(Imgproc.contourArea(o1)); } }); return contours; }
Example 7
Source File: Finder.java From SikuliNG with MIT License | 5 votes |
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; }