I have a C++ project in Visual Studio 2010 and wish to use OCR. I came across many "tutorials" for Tesseract but sadly, all I got was a headache and wasted time.
In my project I have an image stored as a Mat. One solution to my problem is to save this Mat as an image (image.jpg for example) and then call Tesseract executable file like this:
system("tesseract.exe image.jpg out");
Which gets me an output out.txt and then I call
infile.open ("out.txt");
to read the output from Tesseract.
It is all good and works like a chair but it is not an optimal solution. In my project I am processing a video so save/call .exe/write/read at 10+ FPS is not what I am really looking for. I want to implement Tesseract to existing code so to be able to pass a Mat as an argument and immediately get a result as a String.
Do you know any good tutorial(pref. step-by-step) to implement Tesseract OCR with Visual Studio 2010? Or your own solution?
OK, I figured it out but it works for Release and Win32 configuration only (No debug or x64). There are many linking errors under Debug configuration.
So,
1. First of all, download prepared library folder(Tesseract + Leptonica) here:
Mirror 1(Google Drive)
Mirror 2(MediaFire)
2. Extract tesseract.zip to C:\
3. In Visual Studio, go under C/C++ > General > Additional Include Directories
Insert C:\tesseract\include
4. Under Linker > General > Additional Library Directories
Insert C:\tesseract\lib
5. Under Linker > Input > Additional Dependencies
Add:
liblept168.lib
libtesseract302.lib
Sample code should look like this:
#include <tesseract\baseapi.h>
#include <leptonica\allheaders.h>
#include <iostream>
using namespace std;
int main(void){
tesseract::TessBaseAPI api;
api.Init("", "eng", tesseract::OEM_DEFAULT);
api.SetPageSegMode(static_cast<tesseract::PageSegMode>(7));
api.SetOutputName("out");
cout<<"File name:";
char image[256];
cin>>image;
PIX *pixs = pixRead(image);
STRING text_out;
api.ProcessPages(image, NULL, 0, &text_out);
cout<<text_out.string();
system("pause");
}
For interaction with OpenCV and Mat type images look HERE
It has been a lot since the last reply but it may be help to others;
I think you must also add "liblept168.lib" and "liblept168d.lib" to Additional Dependencies
Add "liblept168.dll" and "liblept168d.dll" to the destination of your exe.
Add #include to your code.
(This answer must be a comment to Bruce's answer. Sorry for confusion. )
You need to use the library through the API.
Most probably:
start by downlaoding the libs ( https://code.google.com/p/tesseract-ocr/downloads/detail?name=tesseract-3.02.02-win32-lib-include-dirs.zip&can=2&q= ). They're compiled with Visual 2008 but it should be enough
Use the API directly (example, look at an open source project using it: https://code.google.com/p/qtesseract/source/browse/#svn%2Ftrunk%2Ftessdata ) and read the links from this answer : How can i use tesseract ocr(or any other free ocr) in small c++ project?
Related
I have found two similar questions this and this .
But they both use opencv, and opencv indeed provide the corresponding lib. I don't use opencv, and my project is very simple, just hello world.I have changed project default configuration like this
except for these configurations, others all take defaults
I just want to test my project configuration,that works find for win32 debug and release. But not work for x64 debug and release, they all tell me LINK : fatal error LNK1104: cannot open file 'opencv_world341d.lib'
I indeed know my project does not use any opencv lib, but why they tell me I need to use opencv_world341d.lib
my code
#include<iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
}
Thanks for everyone who comments on the question. I have solved the problem, although this problem not relevant to OpenCV to much, but I think the solution to the problem may be helpful to others. When I build project, visual studio 2019 tell me cannot link opencv_world341d.lib, so I go to Project->Properties->Linker->Input->Additional Dependencies , and I found opencv_world341d.lib. So I need to remove it, but it's readonly. From this we know visual studio using settings file (Microsoft.cpp..users.props) to control global settings including Global search path. These files are located at $(USERPROFILE)\appdata\local\microsoft\msbuild\v4.0 directory. Then I reedit Microsoft.cpp.<Platform>.users.props, delete opencv_world341d.lib, reboot visual studio, problem solved.
I am trying to run OpenCV on Visual Studios 2017. I built the libraries and bin with CMake, so far it is working good. However, when I try to run the code it says this:
This is the error code message that shows when I launch the "local windows debugger":
For some reason, it says that it can't find the DLL. I already assign the Aditional Dependencies in properties also I linked the "Included Directories" and "Included Libraries" to openCV in Visual Studios, the project is set to run in x64, and there are no errors on the programming it's just that error of DLL that shows.
The DLL can be found in a separate folder made by the compiler in a bin folder. However, it still says it can't be found.
This is the locations of the DLL files:
Is there a solution for this?
I tried to add pictures I'm new in the forum it doesn't let me post them yet. I am not sure if the ones I upload will show.
This is the code I tried to run and bring the error message. The same happen with any other code.
I will add more images that may help to understand what I did so far and thanks in advance for the help.
This are the Visual Studios C/C++ Directories:
This is the Linked section in Visual Studios properties:
This is the environment PATH from Environment Variables:
This is all I did so far in the process to install OpenCV in Visual Studios.
#include "stdafx.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
int main() {
cv::Mat image;
std::cout << "This image is" << image.rows << "X" << image.cols << std::endl;
image = cv::imread("puppy.bmp");
if (image.empty()) {
}
cv::Mat result;
cv::flip(image, result, 1);
cv::waitKey(0);
return 0;
}
For me, restart to Visual Studio solved the problem, I guess that's because when I added the OpenCV to Environment Variable the Visual Studio was open
Just copy opencv_world341d.dll to the x64 debug folder, and run it.
I also restart my computer after I add it to PATH. So the solution is just restart your computer when you add to PATH for the first time
I've also faced this issue and solved it simply by adding the OpenCV bin path to the environment variable.
I am trying to get a graphical representation of a chess-board with SFML 2.0. The problem is that I simply cannot load a texture, the command loadFromFile does not work and I don't know why. I already searched the web, also here in Stack Overflow but I did not find a solution.
What I did right now:
Setting all the dependencies for includes and libraries, .dll's and .libs
Changing text format from unicode to multibyte.
Copying the .dll's into the working folder (there was an error that visual studio couldnt find the dlls even though I linked them in the project)
Using the whole path for the image file
using the LoadFile-command with 2 arguments (even though the second one is optional I guess)
Changing runtime library from MDd to MD (trying to get it work in release mode)
Not linking all libraries, but only the ones with a ....-d.lib ending (as supposed in another Stack Overflow thread)
I use Windows 7, Visual Studio Version 15.3.3
This is my code:
#include <SFML/Graphics.hpp>
#include <time.h>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(437, 437), "MattseChess!");
sf::Texture t1;
if (!t1.loadFromFile("C:/Users/Mattse/source/repos/Chess/Chess/images/figures.png")) {
std::cout << "Error loading texture" << std::endl;
}
return 0;
}
What shall I try out next?
You should always open files with a relative path ("images/figures.png"). It's relative from the executable.
And check with an other PNG images to see if it does not come from the image that SFML cannot load for any reasons (and try gimp to reexport it with different options like interlacing for example)
I'm guessing (from the C:\Users\ part) you are on a Windows system.
While Windows should support slashes as path separator, the official path separator in Windows is a backslash. It needs to be escaped in C++, so your full path would be:
"C:\\Users\\Mattse\\source\\repos\\Chess\\Chess\\images\\figures.png"
Problem fixed, it was in fact the case that I forgot to delete one library in the linker->input settings. I only have the ones with a -d ending now, even the ones which have NO -d equivalent got deleted.
List of linked libraries:
sfml-audio-d.lib
sfml-graphics-d.lib
sfml-main-d.lib
sfml-network-d.lib
sfml-network-s-d.lib
sfml-system-d.lib
sfml-system-s-d.lib
sfml-window-d.lib
sfml-window-s-d.lib
So finally I’m not able to help myself out by researching anymore. Hopefully you can help me.
I recently decided to learn C++ in the context of my bachelor-thesis: My first aim is to read the pixel-values of a tiff-image with the libtiff-library. Yet, every call of a function of the library seems to break parts of my program.
Here’s the simple “HelloWorld”-Program, it works as it should:
#include "tiffio.h"
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
// TIFF* tif = TIFFOpen("path to .tif", "r");
return 0;
}
When I uncomment the second line in main(), the code still does compile without errors (except the warning that ‘tif’ isn’t used) and I can start the program as usual. Yet nothing gets printed into the console anymore. Not “Hello” nor any errors.
Any suggestions where the error could be? The code should be alright, I guess I messed something up during the setup of the library or so. Here’s what I did:
I managed to set up eclipse (Mars, 32bit) for C++ and MinGW (32bit) on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
I created a new C++-project with the mentioned code and did the following adjustments in the project-properties:
Project->Properties->C/C++ General->Paths and Symbols->Library
Paths-> Added “D:/… /tiff-4.0.4/libtiff/.libs”
Project->Properties->C/C++ Build->MinGW C++
Linker->Miscellaneous->Set Linkerflags to “-static-libgcc
-static-libstdc++”
Project->Properties->C/C++ Build->MinGW C++ Linker->Libraries-> Set
(-L) to “D:/… /tiff-4.0.4/libtiff/.libs” and (-l) to “libtiff”
I know the .tif is a valid file as I implemented parts of my program in C#, using the LibTiff.NET library by BitMiracle.
Thank you in advance!
Edit 1: The same error occures, even if TIFF* tif = TIFFOpen("path to .tif", "r"); is never called but written down in a dead part of the code. Debugging does not work either, the program seems to be terminated (exit value 0) before a single line is executed, without any error-reports.
I had the same issue and managed to get rid of it.
I set up eclipse (Mars) for C++ and MinGW on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
After the build, I got two directories containing files:
include
tiff.h
tiffconf.h
tiffio.h
tiffio.hxx
tiffvers.h
lib
libtiff.a
libtiff.dll.a
libtiff.la
libtiffxx.a
libtiffxx.dll.a
libtiffxx.la
I also linked all include files and only the libtiff.a to my project and that solved it, ie, the other lines are now executed.
I hope, I helped with this post.
I am using following code for background subtraction. I am giving it path of video, video runs successfully but at the end it gives Debug Assertion Failed error.
I am using following code in Microsoft Visual Studio to solve a problem of Computer Vision with opencv.
#include<opencv2/opencv.hpp>
#include<iostream>
#include<string>
#include<vector>
#include "opencv2/video/background_segm.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat frame;
Mat back;
Mat fore;
VideoCapture cap;
cap.open("H:/competition.avi");
BackgroundSubtractorMOG2 bg(100,16,true);
bg.set("nmixtures",3);
vector<vector<Point> > contours;
namedWindow("Frame");
namedWindow("Background");
for(;;)
{
cap >> frame;
if(!frame.empty())
{
bg.operator ()(frame,fore);
bg.getBackgroundImage(back);
erode(fore,fore,Mat());
dilate(fore,fore,Mat());
findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
drawContours(frame,contours,-1,Scalar(0,0,255),2);
imshow("Frame",frame);
imshow("Background",back);
if(waitKey(30) >= 0) break;
}
else
break;
}
return 0;
}
I just came across this issue and after serious web trawling, found the answer, at least it worked in my case...
you need to go to your visual studio project settings, to c/c++, to code generation and change the runtime library to Multi-threaded Debug DLL (/MDd).
It seems this error is from a single threaded DLL trying to run in a multi thread project, or something of that nature.
good luck!
For unknown reasons, some versions of opencv (2.x at least) have a CMake variable "BUILD_WITH_STATIC_CRT" that by default gets set to on, thus causing issues like that. Disable that flag, then the solution should get generated with /MDd defined.
Secondarily, open your exe file in dependency walker. Look for multiple versions of MS C++ runtime libraries. For example, you may have a version of QT built against msvcp110.dll (visual studio 2012) but your current project uses msvcp120.dll (visual studio 2013).
Alright. First thing first: Hit Retry, assuming that you are debugging (F5), and have not launched (Run) the program by hitting (Ctrl+F5). As soon as you'd hit Retry, you will see call stack in debugger.
The call stack will give you possible hint where that invalid/double free/delete is happening. That would be your starting point to analyse the issue. See if some memory is double freed, allocated using different heap (for example, with malloc, and being deleted). Or, if memory allocated by VC9 (for example), is being freed by a DLL written in VC8.
I had the same error,
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c Line 1424
Expression:_pFirstBlock == pHead
when using debug mode on vs12 when testing opencv code for augmented reality, for reference the code I used was from here.
The Solution that worked for me: The problem went away for me after I updated the visual studio settings for release mode, even though I was only using debug. Other opencv code runs no problem in debug mode, so I had not bothered to fully configured the release settings.
Anyway specifically in release the parts I had to update were in Properties -> C++ -> Additional Include Directories; and Properties -> Linker -> Input -> Additional Dependencies. After that the code ran error free in debug mode and release mode. If you don't know what settings to use, they are listed in the setup instruction pages on the opencv website, vs12 instructions are here
I meet the same problem.
I find the resolution through this URL.
Debug Assertion Failed Expression: _pFirstBlock == pHead using OpenCV and C++ trying to call SurfFeatureDetector
The reason for this error is the configuration problem,vs2012 is matched with vc11 folder.
This may help you.