Krishan Kavinda
Krishan Kavinda

Reputation: 65

How to denoise an image using java opencv

In my case I want to remove the all black dots of my image. here my image can be presented as follows. when i was doing using my program the image was smoothing how ever the program doesn't remove the black dots. please help me to remove black dots.please reply me soon Original Image the codes are as follows.

public class Denoise {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try{
            System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source =Imgcodecs.imread("C:\\Users\\My Kindom\\Downloads\\printscreen.JPG",Imgcodecs.CV_LOAD_IMAGE_COLOR);

         Mat destination = new Mat(source.rows(),source.cols(),source.type());
         destination = source;
         Photo.fastNlMeansDenoisingColored(source,destination, 10, 10, 7, 21);
         Imgcodecs.imwrite("C:\\Users\\My Kindom\\Downloads\\Denoise.jpg", destination);

        }catch(Exception e){}
        // TODO code application logic here
    }

Destination Image

Upvotes: 4

Views: 4327

Answers (1)

dhanushka
dhanushka

Reputation: 10682

Simply, you can apply a threshold to segment the black dots. Then, using this as a mask, do an inpainting. Inpainting won't affect other regions of the image as denoising. I'm not quite sure what you mean by black dots, so I've applied a simple threshold. You can try with different thresholds, use inRange or whatever to produce the mask. I'm also using an arbitrary inpainting-radius. You may be able to make it better by analyzing contour areas in the mask and then deciding on a radius.

original

original

mask and inpainted: threshold = 70, radius = 20

mask70dn70

mask and inpainted: threshold = 100, radius = 20

mask100dn100

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import org.opencv.photo.Photo;

public class Dnoise {

    public static void doDnoise()
    {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat rgb = Highgui.imread("ybD8q.jpg");

        Mat gray = new Mat(rgb.size(), CvType.CV_8U);
        Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_BGR2GRAY);
        Mat mask = new Mat(rgb.size(), CvType.CV_8U);
        Imgproc.threshold(gray, mask, 70, 255, Imgproc.THRESH_BINARY_INV);
        Mat dn = new Mat(rgb.size(), CvType.CV_8UC3);
        Photo.inpaint(rgb, mask, dn, 20, Photo.INPAINT_TELEA);
    }

}

Upvotes: 2

Related Questions