Java Code Examples for org.opencv.imgproc.Imgproc#medianBlur()
The following examples show how to use
org.opencv.imgproc.Imgproc#medianBlur() .
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: MainActivity.java From MOAAP with MIT License | 6 votes |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { //Put it there, just in case:) super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case SELECT_PHOTO: if(resultCode == RESULT_OK && read_external_storage_granted){ try { final Uri imageUri = imageReturnedIntent.getData(); final InputStream imageStream = getContentResolver().openInputStream(imageUri); final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); src = new Mat(selectedImage.getHeight(), selectedImage.getWidth(), CvType.CV_8UC4); Utils.bitmapToMat(selectedImage, src); src_gray = new Mat(selectedImage.getHeight(), selectedImage.getWidth(), CvType.CV_8UC1); switch (ACTION_MODE) { case HomeActivity.GAUSSIAN_BLUR: Imgproc.GaussianBlur(src, src, new Size(9, 9), 0); break; case HomeActivity.MEAN_BLUR: Imgproc.blur(src, src, new Size(9, 9)); break; case HomeActivity.MEDIAN_BLUR: Imgproc.medianBlur(src, src, 9); break; case HomeActivity.SHARPEN: Mat kernel = new Mat(3, 3, CvType.CV_16SC1); //int[] values = {0, -1, 0, -1, 5, -1, 0, -1, 0}; Log.d("imageType", CvType.typeToString(src.type()) + ""); kernel.put(0, 0, 0, -1, 0, -1, 5, -1, 0, -1, 0); Imgproc.filter2D(src, src, src_gray.depth(), kernel); break; case HomeActivity.DILATE: Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY); Imgproc.threshold(src_gray, src_gray, 100, 255, Imgproc.THRESH_BINARY); Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3)); Imgproc.dilate(src_gray, src_gray, kernelDilate); Imgproc.cvtColor(src_gray, src, Imgproc.COLOR_GRAY2RGBA, 4); break; case HomeActivity.ERODE: Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY); Imgproc.threshold(src_gray, src_gray, 100, 255, Imgproc.THRESH_BINARY); Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5)); Imgproc.erode(src_gray, src_gray, kernelErode); Imgproc.cvtColor(src_gray, src, Imgproc.COLOR_GRAY2RGBA, 4); break; case HomeActivity.THRESHOLD: Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY); Imgproc.threshold(src_gray, src_gray, 100, 255, Imgproc.THRESH_BINARY); Imgproc.cvtColor(src_gray, src, Imgproc.COLOR_GRAY2RGBA, 4); break; case HomeActivity.ADAPTIVE_THRESHOLD: Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY); Imgproc.adaptiveThreshold(src_gray, src_gray, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 3, 0); Imgproc.cvtColor(src_gray, src, Imgproc.COLOR_GRAY2RGBA, 4); break; } Bitmap processedImage = Bitmap.createBitmap(src.cols(), src.rows(), Bitmap.Config.ARGB_8888); Log.i("imageType", CvType.typeToString(src.type()) + ""); Utils.matToBitmap(src, processedImage); ivImage.setImageBitmap(selectedImage); ivImageProcessed.setImageBitmap(processedImage); Log.i("process", "process done"); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; } }
Example 2
Source File: GripPipeline.java From FtcSamples with MIT License | 6 votes |
/** * Softens an image using one of several filters. * @param input The image on which to perform the blur. * @param type The blurType to perform. * @param doubleRadius The radius for the blur. * @param output The image in which to store the output. */ private void blur(Mat input, BlurType type, double doubleRadius, Mat output) { int radius = (int)(doubleRadius + 0.5); int kernelSize; switch(type){ case BOX: kernelSize = 2 * radius + 1; Imgproc.blur(input, output, new Size(kernelSize, kernelSize)); break; case GAUSSIAN: kernelSize = 6 * radius + 1; Imgproc.GaussianBlur(input,output, new Size(kernelSize, kernelSize), radius); break; case MEDIAN: kernelSize = 2 * radius + 1; Imgproc.medianBlur(input, output, kernelSize); break; case BILATERAL: Imgproc.bilateralFilter(input, output, -1, radius, radius); break; } }
Example 3
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 4
Source File: NativeClass.java From AndroidDocumentScanner with MIT License | 4 votes |
public List<MatOfPoint2f> getPoints(Mat src) { // Blur the image to filter out the noise. Mat blurred = new Mat(); Imgproc.medianBlur(src, blurred, 9); // Set up images to use. Mat gray0 = new Mat(blurred.size(), CvType.CV_8U); Mat gray = new Mat(); // For Core.mixChannels. List<MatOfPoint> contours = new ArrayList<>(); List<MatOfPoint2f> rectangles = new ArrayList<>(); List<Mat> sources = new ArrayList<>(); sources.add(blurred); List<Mat> destinations = new ArrayList<>(); destinations.add(gray0); // To filter rectangles by their areas. int srcArea = src.rows() * src.cols(); // Find squares in every color plane of the image. for (int c = 0; c < 3; c++) { int[] ch = {c, 0}; MatOfInt fromTo = new MatOfInt(ch); Core.mixChannels(sources, destinations, fromTo); // Try several threshold levels. for (int l = 0; l < THRESHOLD_LEVEL; l++) { if (l == 0) { // HACK: Use Canny instead of zero threshold level. // Canny helps to catch squares with gradient shading. // NOTE: No kernel size parameters on Java API. Imgproc.Canny(gray0, gray, 10, 20); // Dilate Canny output to remove potential holes between edge segments. Imgproc.dilate(gray, gray, Mat.ones(new Size(3, 3), 0)); } else { int threshold = (l + 1) * 255 / THRESHOLD_LEVEL; Imgproc.threshold(gray0, gray, threshold, 255, Imgproc.THRESH_BINARY); } // Find contours and store them all as a list. Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); for (MatOfPoint contour : contours) { MatOfPoint2f contourFloat = MathUtils.toMatOfPointFloat(contour); double arcLen = Imgproc.arcLength(contourFloat, true) * 0.02; // Approximate polygonal curves. MatOfPoint2f approx = new MatOfPoint2f(); Imgproc.approxPolyDP(contourFloat, approx, arcLen, true); if (isRectangle(approx, srcArea)) { rectangles.add(approx); } } } } return rectangles; }
Example 5
Source File: CVProcessor.java From CVScanner with GNU General Public License v3.0 | 4 votes |
public static List<MatOfPoint> findContours(Mat src){ Mat img = src.clone(); //find contours 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); img.release(); Imgproc.medianBlur(resizedImg, resizedImg, 7); Mat cannedImg = new Mat(newSize, CvType.CV_8UC1); Imgproc.Canny(resizedImg, cannedImg, 70, 200, 3, true); resizedImg.release(); Imgproc.threshold(cannedImg, cannedImg, 70, 255, Imgproc.THRESH_OTSU); Mat dilatedImg = new Mat(newSize, CvType.CV_8UC1); Mat morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3)); Imgproc.dilate(cannedImg, dilatedImg, morph, new Point(-1, -1), 2, 1, new Scalar(1)); cannedImg.release(); morph.release(); ArrayList<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(dilatedImg, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); hierarchy.release(); dilatedImg.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; }