0

Hello I have this code until now but can't remove the grid so I can keep only the characters on the captcha. The function applyfilters dilutes and erodes. I am out of ideas how to solve this problem. I am reading books about image processing but still I am out of ideas...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);
  • In your example, the numbers are darker then the grid, why not using a global threshold ? – PSchn Sep 19 '16 at 19:41
  • Another more sophisticated approacj could be to remove the grid with fourier transform! – PSchn Sep 19 '16 at 20:06
  • Can you provide me the code to try for thredshold? I am applying on grayscaled image and it isn't working –  Sep 19 '16 at 20:28
  • Try to use `threshold(imgBlurred,imgThresh,yourThresholdValue,maxValue, CV_THRESH_BINARY_INV)`. With this all the values greater then `yourThresholdValue` are set to 0, the rest to `maxValue` (e.g. 255) – PSchn Sep 19 '16 at 20:35
  • For the filtering in the frequency domain you can see a nice example [here](https://books.google.de/books?id=RAPOBQAAQBAJ&pg=PA155&lpg=PA155&dq=top+hat+remove+periodic+grid&source=bl&ots=noFkvKL8mL&sig=JHWHC28P_MNcAR4AztuAtn0BSZQ&hl=de&sa=X&ved=0ahUKEwiu7JrYpZzPAhUHB8AKHdW-CD0Q6AEIUjAP#v=onepage&q&f=false). – PSchn Sep 19 '16 at 20:49

1 Answers1

0

As in the comments mentioned one possible solution is to use a global threshold. For your sample image i got pretty good results with a fixed threshold of 128:

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

you can also use Otsu's method to calculate the threshold:

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

enter image description here

PSchn
  • 718
  • 4
  • 14
  • Thanks. Works perfect. I am wondering how can I remove the background if it was in mixed colours for example for extracting text from a random background or text from a picture etc –  Sep 21 '16 at 06:35
  • You could read [here](http://stackoverflow.com/questions/23506105/extracting-text-opencv) for example – PSchn Sep 21 '16 at 07:22