Comment effacer les petits points noirs de l'image dans OpenCV en utilisant Java?


Ceci est l'image originaleJ'ai également appliqué le seuil global,le seuil adaptatif, la dilatation et l'érosion, mais je ne peux pas obtenir le résultat attendu.

Imgproc.threshold(source2, destination2, 147, 255,Imgproc.THRESH_BINARY ); 

Highgui.imwrite("threshold.jpg", destination2);


Imgproc.adaptiveThreshold(destination2, destination2, 255,
Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 11,2);
Highgui.imwrite("Adpthreshold.jpg", destination2);


Mat destination3 = new Mat(source.rows(),source.cols(),source.type());double erosion_size = 0;
int dilation_size = 1;

Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new  Size(2*erosion_size + 1, 2*erosion_size+1));
Imgproc.erode(destination2, destination3, element);
Highgui.imwrite("erosion.jpg", destination3);


Mat element1 = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new  Size(2*dilation_size + 1, 2*dilation_size+1));
Imgproc.dilate(destination3, destination3, element1);
Highgui.imwrite("dilation.jpg", destination3);

Voici l'image finale:

Author: kush, 2015-10-01

1 answers

Ce code est ouvert à l'amélioration selon votre désiré result.by cette approche, vous pouvez supprimer la plupart des petits points. j'espère que cela vous aide.

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int, char** argv )
{
    Mat src,src_gray;
    src = imread("4sE4p.jpg");
    if (src.empty())
    {
        cerr << "No image supplied ..." << endl;
        return -1;
    }
    cvtColor( src, src_gray, COLOR_BGR2GRAY );
    src_gray = src_gray >160;
    src_gray.copyTo(src);
    imshow( "src_gray", src_gray );
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    cv::Mat kernel = cv::Mat::ones(2, 10, CV_8U);
    erode(src_gray,src_gray, kernel, Point(-1,-1),2);


    findContours( src_gray, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE, Point(0, 0) );
    for( size_t i = 0; i< contours.size(); i++ )
    {
        Rect R = boundingRect(Mat(contours[i]));
        if( R.width*R.height < 300 )
        {
            Mat roi(src_gray,R);

            if (countNonZero(roi) < R.width*R.height*0.9 )
            {
                rectangle(src,R,Scalar(0,0,255));
                Mat croi(src,R);
                croi.setTo(255); // this line is to clear small dots
            }
        }
    }
    imshow( "result", src );

    waitKey(0);
    return(0);
}
 4
Author: sturkmen, 2015-10-02 23:12:14