OpenCV C ++从验证码中删除网格

时间:2016-09-19 19:28:16

标签: c++ image opencv image-processing captcha

你好,我现在有这个代码,但是不能删除网格,所以我只能保留验证码上的字符。函数applyfilters稀释和侵蚀。我没有想法如何解决这个问题。我正在阅读有关图像处理的书籍,但我仍然没有想法...... captcha example

cv::Mat imgTrainingNumbers;         // imazhi hyrje
cv::Mat imgGrayscale;               // 
cv::Mat imgBlurred;                 // transformime te imazhit
cv::Mat imgThresh;                  //
cv::Mat imgThreshCopy;              //

std::vector<std::vector<cv::Point> > ptContours;        // vektori me konturet
std::vector<cv::Vec4i> v4iHierarchy;                    // hierarkia e kontureve

cv::Mat matClassificationInts;      // trajnimi i klasifikimeve,  duhen bere disa konvertime para se te shkruajme ne skedar

//imazhet e trajnimit, deklarohet si imazh tek dhe me pas shtojme tek ky imazh si te ishte nje vektor. Ne fund duhen ber konvertime para se te shkruhet ne skedar
cv::Mat matTrainingImagesAsFlattenedFloats;

//per te treguar konceptin, po lexoj dhe parashikoj vetem numrat me shkrim dore. Njesoj veprohet edhe per shkronjat
std::vector<int> intValidChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

imgTrainingNumbers = cv::imread("data/19-09-16_091821.png");          // lexoj imazhin me bashkesine e trajnimit

if (imgTrainingNumbers.empty()) {                               
    std::cout << "error: Imazhi i trajnimit nuk u lexua\n\n";         
    return(0);                                                  
}


cv::cvtColor(imgTrainingNumbers, imgGrayscale, CV_BGR2GRAY);        // kthe ne greyscale
cv::imshow("greyscale", imgGrayscale);

//cv::Mat canny_output;
// Detect edges using canny
//cv::Canny(imgGrayscale, canny_output, 100, 100 * 2, 3);
//cv::imshow("canny output", canny_output);

cv::GaussianBlur(imgGrayscale,          // imazh hyrje
    imgBlurred,                             // imazh dajle
    cv::Size(5, 5),                         // zbut gjeresine dhe gjatesine e dritares ne pixel
    0);                                     // vlera sigma tregon se sa blur do i vendoset imazhit, 0 e lejon algoritmin zgjedh menyr automatike vleren

// nga grayscale kthejme ne bardhezi (binarizimi i imazhit)
cv::adaptiveThreshold(imgBlurred,           // imazh hyrje
    imgThresh,                              // imazh dalje
    255,                                    // pixelat qe kalojne limitin i bejme te bardhe te plota (255 rgb)
    cv::ADAPTIVE_THRESH_GAUSSIAN_C,         // shperndarje gaussiane
    cv::THRESH_BINARY_INV,                  // backgroundi i zi, foregroundi i bardhe
    11,                                     // vlera e pixelit fqinj e perdorur te llogaritet vlera thredsholdid
    2);                                     // konstante e zbritur nga mesatarja e peshuar

cv::imshow("Binarizimi i imazhit", imgThresh);         // shfaq imazhin e binarizuar per reference

Mat afterFilter;
afterFilter = applyFilters(imgThresh);
cv::imshow("After Filters", afterFilter);
//imgThresh = applyFilters(imgThresh);
//cv::imshow("After Filters", imgThresh);

1 个答案:

答案 0 :(得分:0)

正如在提到的评论中,一种可能的解决方案是使用全局阈值。对于您的样本图像,我得到了相当不错的结果,固定阈值为128:

threshold(imgBlurred,imgThresh,128,255, CV_THRESH_BINARY_INV)

您也可以使用Otsu的方法来计算阈值:

threshold(imgBlurred,imgThresh,0,255, CV_THRESH_BINARY_INV+CV_THRESH_OTSU)

enter image description here

相关问题