【问题标题】:OpenCV C++ remove grid from captchaOpenCV C++ 从验证码中删除网格
【发布时间】:2017-01-27 14:02:33
【问题描述】:

您好,到目前为止,我有此代码,但无法删除网格,因此我只能保留验证码上的字符。函数 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);

【问题讨论】:

  • 在您的示例中,数字比网格更暗,为什么不使用全局阈值?
  • 另一种更复杂的方法是用傅里叶变换去除网格!
  • 你能提供我尝试阈值的代码吗?我正在应用灰度图像,但它不起作用
  • 尝试使用threshold(imgBlurred,imgThresh,yourThresholdValue,maxValue, CV_THRESH_BINARY_INV)。这样所有大于yourThresholdValue 的值都设置为0,其余设置为maxValue(例如255)
  • 对于频域中的过滤,你可以看到一个很好的例子here

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


【解决方案1】:

正如在 cmets 中提到的,一种可能的解决方案是使用全局阈值。对于您的示例图像,我在固定阈值为 128 的情况下得到了很好的结果:

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

你也可以使用大津的方法来计算阈值:

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

【讨论】:

  • 谢谢。完美运行。我想知道如果背景是混合颜色的,如何删除背景,例如从随机背景中提取文本或从图片中提取文本等
  • 例如你可以阅读here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 2019-02-21
  • 2020-11-11
  • 2020-03-31
  • 1970-01-01
  • 2015-04-08
相关资源
最近更新 更多