C++ opencv-3.4.1 提取圖片中的直線

對(duì)于一張掃描的圖,可能需要把答案寫(xiě)在上面,使閱卷方便,首先要做的就是把直線提出來(lái)。

如圖:


直接進(jìn)行直線出現(xiàn)許多的問(wèn)題,所以我們需要對(duì)圖片進(jìn)行處理。

  1. 二值化
  2. 形態(tài)學(xué)操作,過(guò)濾文字
  3. 進(jìn)行霍夫變換檢測(cè)直線
  4. 對(duì)直線過(guò)濾
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
using namespace std;
Mat src, dst, gray_src, roiImage;
char input_image[] = "input image";
char output_image[] = "output image";
int max_count = 255;
int threshold_value = 100;
void morhpologyLines(int, void*);
void detectLines(int, void*);

int main(int argc, char ** argv){

    src = imread("case2.jpg",IMREAD_GRAYSCALE);
    if (src.empty()){
        printf("colud not load image ..\n");
        return -1;
    }

    namedWindow(input_image, CV_WINDOW_AUTOSIZE);
    namedWindow(output_image, CV_WINDOW_AUTOSIZE);
    imshow(input_image, src);

    Rect roi = Rect(10, 10, src.cols - 20, src.rows - 20);
    roiImage = src(roi);
    imshow("ROI image", roiImage);

    //createTrackbar("threshold", output_image, &threshold_value, max_count, detectLines);
    //detectLines(0, 0);
    morhpologyLines(0,0);

    waitKey(0);
    return 0;
}

void detectLines(int, void*){ // 效果太差了
    Canny(roiImage, dst, threshold_value, threshold_value * 2, 3, false);
    // threshold(roiImage, dst, 0, 255, THRESH_BINARY | THRESH_OTSU);
    imshow("canny", dst);
    vector<Vec4i> lines;
    HoughLinesP(dst, lines, 1, CV_PI / 180.0, 30, 30.0, 0);
    cvtColor(dst, dst, COLOR_GRAY2BGR);
    for (size_t t = 0; t < lines.size(); t++){
        Vec4i ln = lines[t];
        line(dst,Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);

    }
    imshow(output_image, dst);
}


void morhpologyLines(int, void*){
    // binary image
    Mat binaryImage, morhpImage;
    threshold(roiImage, binaryImage, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
    imshow("binary", binaryImage);


    // morphology operation
    Mat kernel = getStructuringElement(MORPH_RECT, Size(20,1), Point(-1, -1));
    morphologyEx(binaryImage, morhpImage, MORPH_OPEN, kernel, Point(-1, -1));
    imshow("morpgology reslut", morhpImage);
    
    //dilate image
    kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
    dilate(morhpImage, morhpImage, kernel);
    imshow("morphology lines", morhpImage);

    // hough lines
    vector<Vec4i> lines, restLines;
    HoughLinesP(morhpImage, lines, 1, CV_PI / 180.0, 40, 40.0, 0);
    Mat resultImage = roiImage.clone();
    cvtColor(resultImage, resultImage, COLOR_GRAY2BGR);
    for (size_t t = 0; t < lines.size(); t++){
        Vec4i ln = lines[t];
        // cout << ln << endl;
        cout << ln[2] - ln[0] << endl;
        if ((ln[2] - ln[0]) > 100)
        {   
            bool yes = true;
            for (size_t r = 0; r < restLines.size(); r++){
                Vec4i ln1 = restLines[r];
                if (ln1[1] - ln[1] < 5)
                    yes = false;
            }
            if (yes){
                line(resultImage, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);
                putText(resultImage, "A", Point((ln[0] + ln[2]) / 2, (ln[1] + ln[3]) / 2), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(12, 255, 300), 1, 8);
                restLines.push_back(ln); 
            }
        }
    }
    imshow(output_image, resultImage);
}

二值化

形態(tài)學(xué)操作

霍夫檢測(cè)直線

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容