Java Opencv implementation of thinning binarization

1. OpenCV is used to verify corrosion and expansion, and only screenshots are needed for recovery. Please refer to Chapter 6 of he Dongjian courseware and source code or other resources for details.

2. Use OpenCV to verify the refinement, only the screenshot reply is needed. Please refer to Chapter 6 of he Dongjian courseware and source code or other resources for details.

3. For other methods, you can first understand the basic principle and then understand the mathematical derivation.


Algorithm theory article: https://blog.csdn.net/baidu_21578557/article/details/51871134 


Verification of corrosion and expansion with OpenCV

public class threshold {

    private final static String path = System.getProperty("user.dir") + "\\catton.jpg";

    static {
        platformUtils.loadLibraries();
    }


    public static void main(String[] args) {

        Mat src= Imgcodecs.imread(path);

        Mat dest=new Mat();

        Imgproc.threshold(src,dest,100,500,Imgproc.THRESH_BINARY);

        //get data
        byte [] data=new byte[dest.rows()*dest.cols()*(int)dest.elemSize()];
        dest.get(0,0,data);

        //Turn to binarization image buffer As a storage object, binary image can be stored or grayscale image can be stored
        BufferedImage bufferedImage=new BufferedImage(dest.cols(),dest.rows(),BufferedImage.TYPE_BYTE_GRAY);

        bufferedImage.getRaster().setDataElements(0,0,dest.cols(),dest.rows(),data);

        JFrame frame=new JFrame();
        frame.getContentPane().add(new JLabel(new ImageIcon(bufferedImage)));
        frame.pack();
        frame.setVisible(true);


        HighGui.imshow(" threshold image",dest);
        HighGui.waitKey(0);
    }
}



Verifying refinement with OpenCV

//Extract the skeleton of the image
void ImgThin(cv::Mat src,int maxIterations=-1)
{
    if (src.empty()) return;//Image is empty, return directly
    cv::threshold(src, src, m_dThreshold, 1, CV_THRESH_BINARY);//Image converted to 0 or 1

    int ImgHeight = src.rows;
    int ImgWidth = src.cols;

    int count = 0;  //Record iterations
    while (true)
    {
        count++;
        if (maxIterations != -1 && count > maxIterations) //Limit the number of times and the number of iterations reaches
            break;

        vector<pair<int, int> > mFlag; //Used to mark points to be deleted
        //Point to point marker
        for (int i = 0; i < ImgHeight; ++i)
        {
            for (int j = 0; j < ImgWidth; ++j)
            {
                //Mark if four conditions are met
                //  p9 p2 p3
                //  p8 p1 p4
                //  p7 p6 p5
                int p1 = src.at<uchar>(i, j);
                int p2 = (i == 0) ? 0 : src.at<uchar>(i - 1, j);
                int p3 = (i == 0 || j == ImgWidth - 1) ? 0 : src.at<uchar>(i - 1, j + 1);
                int p4 = (j == ImgWidth - 1) ? 0 : src.at<uchar>(i, j + 1);
                int p5 = (i == ImgHeight - 1 || j == ImgWidth - 1) ? 0 : src.at<uchar>(i + 1, j + 1);
                int p6 = (i == ImgHeight - 1) ? 0 : src.at<uchar>(i + 1, j);
                int p7 = (i == ImgHeight - 1 || j == 0) ? 0 : src.at<uchar>(i + 1, j - 1);
                int p8 = (j == 0) ? 0 : src.at<uchar>(i, j - 1);
                int p9 = (i == 0 || j == 0) ? 0 : src.at<uchar>(i - 1, j - 1);

                if ((p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) >= 2 && (p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) <= 6)
                {
                    int ap = 0;
                    if (p2 == 0 && p3 == 1) ++ap;
                    if (p3 == 0 && p4 == 1) ++ap;
                    if (p4 == 0 && p5 == 1) ++ap;
                    if (p5 == 0 && p6 == 1) ++ap;
                    if (p6 == 0 && p7 == 1) ++ap;
                    if (p7 == 0 && p8 == 1) ++ap;
                    if (p8 == 0 && p9 == 1) ++ap;
                    if (p9 == 0 && p2 == 1) ++ap;

                    if (ap == 1)
                    {
                        if (p2*p4*p6 == 0)
                        {
                            if (p4*p6*p8 == 0)
                            {
                                //sign
                                mFlag.push_back(make_pair(i, j));
                            }
                        }
                    }
                }
            }
        }

        //Delete marked points
        for (vector<pair<int, int> >::iterator i = mFlag.begin(); i != mFlag.end(); ++i)
        {
            src.at<uchar>(i->first, i->second) = 0;
        }

        //Until no point is satisfied, the algorithm ends
        if (mFlag.size() == 0) break;
        else mFlag.clear();//take mFlag empty

        //Point to point marker
        for (int i = 0; i < ImgHeight; ++i)
        {
            for (int j = 0; j < ImgWidth; ++j)
            {
                //Mark if four conditions are met
                //  p9 p2 p3
                //  p8 p1 p4
                //  p7 p6 p5
                int p1 = src.at<uchar>(i, j);
                if (p1 != 1) continue;
                int p2 = (i == 0) ? 0 : src.at<uchar>(i - 1, j);
                int p3 = (i == 0 || j == ImgWidth - 1) ? 0 : src.at<uchar>(i - 1, j + 1);
                int p4 = (j == ImgWidth - 1) ? 0 : src.at<uchar>(i, j + 1);
                int p5 = (i == ImgHeight - 1 || j == ImgWidth - 1) ? 0 : src.at<uchar>(i + 1, j + 1);
                int p6 = (i == ImgHeight - 1) ? 0 : src.at<uchar>(i + 1, j);
                int p7 = (i == ImgHeight - 1 || j == 0) ? 0 : src.at<uchar>(i + 1, j - 1);
                int p8 = (j == 0) ? 0 : src.at<uchar>(i, j - 1);
                int p9 = (i == 0 || j == 0) ? 0 : src.at<uchar>(i - 1, j - 1);

                if ((p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) >= 2 && (p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) <= 6)
                {
                    int ap = 0;
                    if (p2 == 0 && p3 == 1) ++ap;
                    if (p3 == 0 && p4 == 1) ++ap;
                    if (p4 == 0 && p5 == 1) ++ap;
                    if (p5 == 0 && p6 == 1) ++ap;
                    if (p6 == 0 && p7 == 1) ++ap;
                    if (p7 == 0 && p8 == 1) ++ap;
                    if (p8 == 0 && p9 == 1) ++ap;
                    if (p9 == 0 && p2 == 1) ++ap;

                    if (ap == 1)
                    {
                        if (p2*p4*p8 == 0)
                        {
                            if (p2*p6*p8 == 0)
                            {
                                //sign
                                mFlag.push_back(make_pair(i, j));
                            }
                        }
                    }
                }
            }
        }
        //delete
        for (vector<pair<int, int> >::iterator i = mFlag.begin(); i != mFlag.end(); ++i)
        {
            src.at<uchar>(i->first, i->second) = 0;
        }

        //Until no point is satisfied, the algorithm ends
        if (mFlag.size() == 0) break;
        else mFlag.clear();//take mFlag empty
    }

    cv::threshold(src, src, 0, 255, CV_THRESH_BINARY);//Binary image
}

Tags: P4 OpenCV

Posted on Fri, 08 May 2020 07:16:56 -0700 by coool