Skip to content

Instantly share code, notes, and snippets.

@ivder
Created November 22, 2019 08:01
Show Gist options
  • Save ivder/99d31d46f5fc76ec44d377d5c0c46e9d to your computer and use it in GitHub Desktop.
Save ivder/99d31d46f5fc76ec44d377d5c0c46e9d to your computer and use it in GitHub Desktop.
Milepost digits detection
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
std::string extractIntegerFromString(string str)
{
std::string result;
std::stringstream ss;
ss << str[0];
for (int i = 1; i < str.size(); i++) {
ss << ' ' << str[i];
}
/* Store the String into String Stream */
/* Running loop till the end of the stream */
string tempStr;
int found;
while (ss >> tempStr) {
/* Checking the given whether word is integer or not */
if (stringstream(tempStr) >> found) {
result += std::to_string(found);
}
}
return result;
}
int main()
{
cv::Mat img, resized, threshTop, threshBot, dilated, outTop, outBot;
char *outText;
std::string ocrOutput, finalOutput;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
// Open input image with leptonica library
img = cv::imread("C:/Users/super/OneDrive/Desktop/roadmark/crop.jpg",0);
cv::resize(img, resized, cv::Size(4 * img.cols, 4 * img.rows));
cv::Rect topRoi = cv::Rect(0, 0, resized.cols, resized.rows / 2);
cv::Rect botRoi = cv::Rect(0, resized.rows / 2, resized.cols, resized.rows / 2);
cv::threshold(resized(topRoi), threshTop, 128, 255, cv::THRESH_BINARY_INV);
cv::threshold(resized(botRoi), threshBot, 128, 255, cv::THRESH_BINARY);
cv::dilate(threshTop, dilated, cv::Mat(), cv::Point(-1, -1), 3, 1, 1);
outTop = dilated.clone();
outBot = threshBot.clone();
api->SetPageSegMode(tesseract::PSM_SINGLE_LINE);
api->SetImage((uchar*)outTop.data, outTop.size().width, outTop.size().height, outTop.channels(), outTop.step1());
ocrOutput = api->GetUTF8Text();
finalOutput += extractIntegerFromString(ocrOutput);
api->SetImage((uchar*)outBot.data, outBot.size().width, outBot.size().height, outBot.channels(), outBot.step1());
ocrOutput = api->GetUTF8Text();
finalOutput += '.'+extractIntegerFromString(ocrOutput);
std::cout << finalOutput;
cv::namedWindow("out top", cv::WINDOW_AUTOSIZE);
cv::namedWindow("clean", cv::WINDOW_AUTOSIZE);
cv::namedWindow("out bot", cv::WINDOW_AUTOSIZE);
cv::imshow("out top", outTop);
cv::imshow("clean", threshTop);
cv::imshow("out bot", outBot);
cv::waitKey(0);
cv::destroyAllWindows();
// Destroy used object and release memory
api->End();
//delete[] outText;
img.release();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment