File: textdetection_swt.cpp

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (89 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Sample code which demonstrates the working of
// stroke width transform in the text module of OpenCV
#include <opencv2/text.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;
using namespace cv;

static void help(const CommandLineParser& cmd, const string& errorMessage)
{
    cout << errorMessage << endl;
    cout << "Avaible options:" << endl;
    cmd.printMessage();
}

static bool fileExists (const string& filename)
{
    ifstream f(filename.c_str());
    return f.good();
}

int main(int argc, const char * argv[])
{
    const char* keys =
        "{help h usage ? |false | print this message }"
        "{@image         |      | path to image }"
        "{@darkOnLight   |false | indicates whether text to be extracted is dark on a light brackground. Defaults to false. }"
        ;

    CommandLineParser cmd(argc, argv, keys);

    if(cmd.get<bool>("help"))
    {
        help(cmd, "Usage: ./textdetection_swt [options] \nExample: ./textdetection_swt scenetext_segmented_word03.jpg true");
        return EXIT_FAILURE;
    }

    string filepath = cmd.get<string>("@image");

    if (!fileExists(filepath)) {
        help(cmd, "ERROR: Could not find the image file. Please check the path.");
        return EXIT_FAILURE;
    }

    bool dark_on_light = cmd.get<bool>("@darkOnLight");

    Mat image = imread(filepath, IMREAD_COLOR);

    if (image.empty())
    {
        help(cmd, "ERROR: Could not load the image file");
        return EXIT_FAILURE;
    }

    cout << "Starting SWT Text Detection Demo with dark_on_light variable set to " << dark_on_light << endl;

    imshow("Input Image", image);
    waitKey(1);

    vector<cv::Rect> components;
    Mat out;
    vector<cv::Rect> regions;
    cv::text::detectTextSWT(image, components, dark_on_light, out, regions);

    imshow ("Letter Candidates", out);
    waitKey(1);

    cout << components.size() << " letter candidates found." << endl;

    Mat image_copy = image.clone();

    for (unsigned int i = 0; i < regions.size(); i++) {
        rectangle(image_copy, regions[i], cv::Scalar(0, 0, 0), 3);
    }
    cout << regions.size() << " chains were obtained after merging suitable pairs" << endl;
    cout << "Recognition finished. Press any key to exit..." << endl;

    imshow ("Chains After Merging", image_copy);
    waitKey();

    return 0;
}