Skip to content

Commit

Permalink
Merge pull request #10 from royshil/master
Browse files Browse the repository at this point in the history
Complete OpenCV2.4 C++11 rehaul with CMake
  • Loading branch information
aperrault committed Mar 8, 2016
2 parents 5f95d34 + 3dbd81a commit bdf4046
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 371 deletions.
36 changes: 36 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cmake_minimum_required (VERSION 3.2)
project (DetectText)

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

set(CMAKE_BUILD_TYPE Debug)

find_package(OpenCV REQUIRED core imgproc video highgui features2d)

################ Boost #################
set(Boost_USE_STATIC_LIBS ON)
FIND_PACKAGE(Boost COMPONENTS system REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
########################################

file(GLOB DETECT_TEXT_SOURCES *.cpp *.c)
file(GLOB DETECT_TEXT_HEADERS *.hpp *.h)

add_executable(DetectText
${DETECT_TEXT_SOURCES}
${DETECT_TEXT_HEADERS}
)

target_link_libraries(DetectText
${Boost}
${OpenCV_LIBS}
)
120 changes: 56 additions & 64 deletions FeaturesMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,84 +19,76 @@
*/
#include <cassert>
#include <fstream>
#include "TextDetection.h"
#include <opencv/highgui.h>
#include <exception>
#include <iostream>

void convertToFloatImage ( IplImage * byteImage, IplImage * floatImage )
{
cvConvertScale ( byteImage, floatImage, 1 / 255., 0 );
}
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

class FeatureError : public std::exception
{
std::string message;
public:
FeatureError ( const std::string & msg, const std::string & file )
{
std::stringstream ss;
#include "TextDetection.h"

ss << msg << " " << file;
message = msg.c_str ();
}
~FeatureError () throw ( )
using namespace std;
using namespace cv;
using namespace DetectText;

void convertToFloatImage ( Mat& byteImage, Mat& floatImage )
{
byteImage.convertTo(floatImage, CV_32FC1, 1 / 255.);
}

class FeatureError: public std::exception {
std::string message;
public:
FeatureError(const std::string & msg, const std::string & file) {
std::stringstream ss;

ss << msg << " " << file;
message = msg.c_str();
}
~FeatureError() throw () {
}
};

IplImage * loadByteImage ( const char * name )
{
IplImage * image = cvLoadImage ( name );
Mat loadByteImage(const char * name) {
Mat image = imread(name);

if ( !image )
{
return 0;
}
cvCvtColor ( image, image, CV_BGR2RGB );
return image;
if (image.empty()) {
return Mat();
}
cvtColor(image, image, CV_BGR2RGB);
return image;
}

IplImage * loadFloatImage ( const char * name )
{
IplImage * image = cvLoadImage ( name );
Mat loadFloatImage(const char * name) {
Mat image = imread(name);

if ( !image )
{
return 0;
}
cvCvtColor ( image, image, CV_BGR2RGB );
IplImage * floatingImage = cvCreateImage ( cvGetSize ( image ),
IPL_DEPTH_32F, 3 );
cvConvertScale ( image, floatingImage, 1 / 255., 0 );
cvReleaseImage ( &image );
return floatingImage;
if (image.empty()) {
return Mat();
}
cvtColor(image, image, CV_BGR2RGB);
Mat floatingImage(image.size(), CV_32FC3);
image.convertTo(floatingImage, CV_32F, 1 / 255.);
return floatingImage;
}

int mainTextDetection ( int argc, char * * argv )
{
IplImage * byteQueryImage = loadByteImage ( argv[1] );
if ( !byteQueryImage )
{
printf ( "couldn't load query image\n" );
return -1;
}

// Detect text in the image
IplImage * output = textDetection ( byteQueryImage, atoi(argv[3]) );
cvReleaseImage ( &byteQueryImage );
cvSaveImage ( argv[2], output );
cvReleaseImage ( &output );
return 0;
int mainTextDetection(int argc, char** argv) {
Mat byteQueryImage = loadByteImage(argv[1]);
if (byteQueryImage.empty()) {
cerr << "couldn't load query image" << endl;
return -1;
}

// Detect text in the image
Mat output = textDetection(byteQueryImage, atoi(argv[3]));
imwrite(argv[2], output);
return 0;
}

int main ( int argc, char * * argv )
{
if ( ( argc != 4 ) )
{
printf ( "usage: %s imagefile resultImage darkText\n",
argv[0] );

return -1;
}
return mainTextDetection ( argc, argv );
int main(int argc, char** argv) {
if ((argc != 4)) {
cerr << "usage: " << argv[0] << " imagefile resultImage darkText" << endl;
return -1;
}
return mainTextDetection(argc, argv);
}
24 changes: 16 additions & 8 deletions README → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ DetectText

Detect text with stroke width transform.

Dependencies: OpenCV, boost.
## Dependencies
OpenCV 2.4+, boost.

## Compile

g++ -o DetectText TextDetection.cpp FeaturesMain.cpp -lopencv_core -lopencv_highgui -lopencv_imgproc -I/path/to/current/directory

To compile:
g++ -o DetectText TextDetection.cpp FeaturesMain.cpp -lopencv_core -lopencv_highgui -lopencv_imgproc -I/path/to/current/directory
where /path/to/current/directory is replaced with the absolute path to the current directory.

To run:
### Using CMake

mkdir build
cd build
cmake ..
make

## To run
./TextDetection input_file output_file dark_on_light
where dark_on_light is 1 or 0, indicating whether the text is darker or lighter than the background.

GitHub repository:
https://github.com/aperrau/DetectText

More details on the algorithm can be found in:
## More
Details on the algorithm can be found in:
http:https://www.cs.cornell.edu/courses/cs4670/2010fa/projects/final/results/group_of_arp86_sk2357/Writeup.pdf
Loading

0 comments on commit bdf4046

Please sign in to comment.