Skip to content

qdrin/image2text

Repository files navigation

QImage2Text class. Text words detect and representing them as cv:String's

We are using OpenCV library to work with images We are using Tesseract provided with opencv-contrib part of OpenCV

Necessary dependencies

opencv: library and opencv-contrib (for Tesseract)

Example using this repo

creating QImageToText object from file

  #include "imagetotext.hpp"
  using namespace std;
  using namespace cv;

  String filename = "./erundulki.jpg";
  QImageToText workTess(filename);

creating QImageToText object from existing cv::Mat

#include "imagetotext.hpp"
using namespace std;
using namespace cv;

Mat img;
// Here we make the image not empty

QImageToText work(img);

Recognise image's words with Tesseract algorithm upon the whole image

#include "imagetotext.hpp"
using namespace std;
using namespace cv;

Mat someImage;
// Fill someImage with sence
QImageToText workTess(someImage);

if(workTess.tessToText()) {
  vector<QWord> twords = workTess.words();
  cout << "Tesseract text:\n";
  for(vector<QWord>::iterator i=twords.begin(); i != twords.end(); i++) {
    // QWord struct consists of confidence, cv::Rect, and cv::String 
    if(i->confidence > minConfidence) {
      rectangle(work.image(), i->rect, Scalar(0, 255, 0), 1);
      cout << "tessword: " << i->word << ", confidence: " << i->confidence << ", rect: " << i->rect << endl;
    }
  }
}

Detecting and recognizing particular words

#include "imagetotext.hpp"
using namespace std;
using namespace cv;

float minConfidence = 80.0;  // Used to filter noise
cv::Mat someImage;
// Fill someImage with sence
QImageToText work(someImage);

// Detecting candidates-to-be-words in the image
vector<Rect> wordRects = work.detectWords();

// Apply Tesseract OCR to every rectangle in wordRects
// result words can also be found in work.words()
for(int i = 0; i < wordRects.size(); i++) {
  QWord &w = *(work.candidateToWord(i));
}

for(vector<QWord>::const_iterator i=work.words().begin(); i != work.words().end(); i++) {
  if(i->confidence > minConfidence) {
    cout << "cvword: " << i->word << ", confidence: " << i->confidence << ", rect: " << i->rect << endl;
  }
}

// Another form for working with words
const vector<QWord> &words = work.words();
for(int i=0; i < words.size(); i++) {
  if(words[i].confidence > minConfidence) {
    cout << "cvword2: " << words[i].word << ", confidence: " << words[i].confidence << ", rect: " << words[i].rect << endl;
  }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages