java OpenCV檢測表格線

代碼

public static void main(String[] args) {
    OpencvTest2 opencvTest2 = new OpencvTest2();

    //讀入圖片
    Mat src = Imgcodecs.imread("F:\\opencvPhoto\\photo\\1-temp.jpg");
    //灰度化
    Mat gray = opencvTest2.gray(src);
    //二值化
    Mat adaptiveThreshold = opencvTest2.adaptiveThreshold(gray);
    Imgcodecs.imwrite("F:\\opencvPhoto\\result\\adaptiveThreshold.jpg",src);
    //膨脹+腐蝕:補全表格線內的空洞
    Mat element  = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,new Size(3, 3));
    Imgproc.dilate(adaptiveThreshold, adaptiveThreshold, element);
    Imgproc.erode(adaptiveThreshold, adaptiveThreshold, element);
    Imgcodecs.imwrite("F:\\opencvPhoto\\result\\dilateAndErode.jpg",src);
    //獲得橫線
    Mat horizontalLine =opencvTest2.getHorizontal(adaptiveThreshold.clone());
    Imgcodecs.imwrite("F:\\opencvPhoto\\result\\horizontal.jpg",horizontalLine);
    //獲得豎線
    Mat verticalLine =opencvTest2.getVertical(adaptiveThreshold.clone());
    Imgcodecs.imwrite("F:\\opencvPhoto\\result\\vertical.jpg",verticalLine);
    //橫豎線合併
    Mat tableLine = opencvTest2.getOr(horizontalLine, verticalLine);
    Imgcodecs.imwrite("F:\\opencvPhoto\\result\\tableLine.jpg",tableLine);

}

/**
 * 灰度化
 * @param src 原圖
 * @return
 */
public Mat gray(Mat src){
    Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
    return src;
}

/**
 * 自適應二值化
 * @param src 灰度圖
 * @return
 */
public Mat adaptiveThreshold(Mat src){
    Imgproc.adaptiveThreshold(src, src, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV , 3, 3);
    return src;
}

/**
 * 得到橫線並進行延長
 * @param adaptiveThreshold
 * @return
 */
public Mat getHorizontal(Mat adaptiveThreshold){
    int scale = 20; //這個值越大,檢測到的直線越多
    int horizontalsize = adaptiveThreshold.cols() / scale;

    Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT
            ,new Size(horizontalsize, 1));
    // 獲得橫線:先腐蝕再膨脹
    Imgproc.erode(adaptiveThreshold, adaptiveThreshold, horizontalStructure);
    Imgproc.dilate(adaptiveThreshold, adaptiveThreshold, horizontalStructure);

    return adaptiveThreshold;
}

/**
 * 得到豎線並進行相連
 * @param adaptiveThreshold
 * @return
 */
public Mat getVertical(Mat adaptiveThreshold){
    int scale = 20; //這個值越大,檢測到的直線越多
    int verticalsize  = adaptiveThreshold.rows() / scale;

    if(verticalsize == 0) {
        verticalsize = 1;
    }

    Mat verticalStructure  = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,new Size(1, verticalsize));
    // 獲得豎線:先腐蝕再膨脹
    Imgproc.erode(adaptiveThreshold, adaptiveThreshold, verticalStructure);
    Imgproc.dilate(adaptiveThreshold, adaptiveThreshold, verticalStructure);

    return adaptiveThreshold;
}

/**
 * 橫豎線合併
 * @param horizontal
 * @param vertical
 * @return
 */
public Mat getOr(Mat horizontal,Mat vertical){
    Mat or=new Mat();
    Core.bitwise_or(horizontal,vertical,or);
    return or;
}

原圖

在這裏插入圖片描述

二值化圖

在這裏插入圖片描述

膨脹+腐蝕補全空洞

在這裏插入圖片描述

獲得橫線

在這裏插入圖片描述

獲得豎線

在這裏插入圖片描述

橫豎線合併獲得表格線

在這裏插入圖片描述

參考鏈接

https://blog.csdn.net/yomo127/article/details/52045146?utm_source=blogxgwz1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章