Use OpenCV C++ commands within OpenFrameworks Xcode OSX Project - c++

I would like to use OpenFrameworks for an OSX application I am building. However, I need to include some existing code that uses C++ OpenCV commands, e.g. cv::imread().
The Xcode linker throws the error Undefined symbols for architecture i386: "cv::imread(std::string const&, int). At first I tried to use the existing OpenCV code that is in ofxOpenCv, then fell back to including the OpenCV framework as I had in previous non-OpenFrameworks projects. Neither approach solved the linking problem.
As far as I can tell, the problem is that OpenCV is compiled with the libc++ compiler, while OpenFrameworks is compiled with the libstdc++ compiler.
This presentation shows how to use C++ OpenCV commands within OpenFrameworks, but it is under Windows and not a detailed account.
This SO Question implies that OpenCV can be recompiled under libstdc++, but the solution given is for iOS and referenced make file does not exist for OSX/linux.
Is it at all possible to use OpenFrameworks with the OpenCV C++ commands under OSX?

Please use Kyle McDonald's ofxCv addon, it's a much nicer interface to opencv from openframeworks. It includes Utilities to convert from cv::Mat of ofImage and back for example or helpers to draw cv::Mat straight in your of application:
#include "testApp.h"
using namespace ofxCv;
using namespace cv;
Mat img;
void testApp::setup() {
img = imread("yourImage.png");//make sure the path is correct
}
void testApp::update() {
}
void testApp::draw() {
drawMat(img,0, 0);
}
Also, your Undefined symbols for architecture i386: error means you probably linked against the x64(64-bit) opencv .dylib files, not the i386(32b-bit) ones.

Related

UE5 OpenCV: Can’t grab VideoCapture

I’m trying to read camera input using the built-in OpenCV library in UE5 but no matter what I do, I can’t seem to make cv::VideoCapture.read() return anything. cv::VideoCapture.grab() also returns false every time.
It works fine on the same machine with regular C++ with OpenCV 4.6.0 and the VideoCapture is definitely open and the camera turns on as expected.
Is there something about Unreal’s built-in implementation I need to know about? (Other than it used OpenCV 4.5.5). I can’t seem to find any info online about this.
This is what my header files look like:
#pragma once
#include "PreOpenCVHeaders.h"
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/imgproc.hpp>
#include "PostOpenCVHeaders.h"
#include "CameraReader.generated.h"
And my Plugin’s Build file:
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"OpenCVHelper",
"OpenCV",
// ... add other public dependencies that you statically link with here ...
}
);
Using Windows 11 with Kinect 2.0.
I thought maybe I was missing some OpenCV DLLs since I can only find a custom Unreal version of the OpenCV World DLL, so I tried overriding the default OpenCV plugin and adding the FFMPEG and MSMF DLLs myself but that didn’t change anything.
Edit: After many attempted building OpenCV from source, I only managed to get GStreamer and FFMPEG to work.
The OpenCV library that comes with UE5 is not built with FFmpeg or with the Media Foundation library, so it cannot read video from a file or from a camera.
You can try to build your own version of OpenCV with FFmpeg or Media Foundation support and use that instead of the one that comes with UE5.

c++ openCV 3.1 - can't use Mat or most of the openCV things like in the tutorials

Best
I'm trying to create a project in C++, which relays most of the time on the
open-cv library.
Therefore, I've installed open-cv3.1.0 on my windows machine and connected the
library and include map/files to my netbeans c++ project.
Succes
Overall I think that I've managed to do this correctly because
I don't receive any errors when I compile/run the application with the next piece of code in it.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
++ I can read an images in and visualize it :)
CvMat *img = cvLoadImageM(location.c_str(),CV_LOAD_IMAGE_GRAYSCALE);
cvNamedWindow( "My Window", 1 );
cvShowImage( "My Window", img );
cvWaitKey();
Problem
But as soon as I take a random tutorial. E.g.
// create a big 8Mb matrix
Mat A(1000, 1000, CV_64F);
http://docs.opencv.org/3.0-last-rst/modules/core/doc/intro.html
Or even
Mat A;
Then I get immediately an compile error :
g++ -o dist/Debug/Cygwin-Windows/ai4 build/Debug/Cygwin-Windows/PrincipalComponentAnalysis.o build/Debug/Cygwin-Windows/ReadInImage.o build/Debug/Cygwin-Windows/main.o -L../../../netbeans/OpenCV/opencv/build/x64/vc14/bin -lopencv_world310 -lopencv_world310d
build/Debug/Cygwin-Windows/PrincipalComponentAnalysis.o: In function `cv::Mat::~Mat()':
/cygdrive/d/fun/ai/ai4/../../../netbeans/OpenCV/opencv/build/include/opencv2/core/mat.inl.hpp:571: undefined reference to `cv::fastFree(void*)'
...
The only things which doesn't give me an error are :
CvMat A;
CvMat *B;
IplImage *C;
IplImage D;
Which s***t because most (all) the tutorials, and stackoverflow issues are using the Mat A or cv::Mat A version.
Question
Thus the question : Did you've ever had the same kind of problem or issue? and are you willing to help me? + How can I solve it.
edit : Configuration steps - OpenCV 3.1
Tools --> Options --> c/c++ --> Code Assistance :
D:\netbeans\openCV\opencv\build\include
D:\netbeans\openCV\opencv\build\include\opencv2
R-click Project --> Properties --> build --> C++ Compiler --> include directories
../../../netbeans/OpenCV/opencv/build/include
../../../netbeans/OpenCV/opencv/build/include/opencv2
R-click Project --> Properties --> build --> linker --> Additional Library Directories
../../../netbeans/OpenCV/opencv/build/x64/vc14/bin
R-click Project --> Properties --> build --> linker --> Libraries --> Add Labrary
opencv_world310
opencv_world310d
Solution
Download CMake
Download OpenCV
Download MinGW <-- Use this as compiler instead of cygwin
Install MinGW
Follow this link --> http://eyalarubas.com/opencv-installation-on-windows-netbeans-mingw.html
you are compiling with cygwin and linking to the VC14 libs.
That's your error. VC14 is for MSVC14 (Visual Studio Compiler), you will need to compile OpenCV yourself to use cygwin. OpenCV does not come with other pre-built libraries.
There are multiple tutorials online of how to do it but effectively you need to use cmake in order to compile OpenCV for your particular tool chain.
You have problem while calling the destructor of cv::Mat. I faced this problem onced. It was related to mismatch between the library and the program. For example 64 and 32 ,debug and release or mt and md something like these. So, the first thing that you have to do is to make sure that everything are matched between OpenCV built and your calling program.

How to build and use a C++ static library for ios application

I know how to build a object C static library using iOS->Framework&Library->Cocoa Touch Static Library in xcode 4.6, and it is straightforward with the help of this tutorial Creating a Static Library in iOS Tutorial. One thing I am not sure, however, is how to build and use a pure C++ static library for io application. For building a C++ static library, I am also use iOS->Framework&Library->Cocoa Touch Static Library guideline, and the difference is that I delete all the .h and .m files when creating the static library project and then put all the C++ static library head files and implementation files in the project. A very simple example is as follows:
hello.h
#include <iostream>
void say_hello();
hello.cpp
#include "hello.h"
void say_hello()
{
std::cout<<"hello"<<std::endl;
}
It seems working, and I can build hello.a static library for iPhone 6.1 Simulator. The next step is to build an application that will invoke the static library. I build a simple iOS application->Single View Application for iPhone 6.1 Simulator, and then try to invoke the hello.a static library in ViewController.mm file (change ViewController.m to ViewController.mm so that it can invoke C++ function) simply with the following code:
say_hello();
However, I received one warning and two error messages:
Warning:
ld: warning: ignoring file hello.a, file was built for archive which is not the architecture being linked (i386):
Error 1:
hello.a
Undefined symbols for architecture i386:
"say_hello()", referenced from:
-[ViewController viewDidLoad] in ViewController.o
Error 2:
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I then have several questions related to this experiment:
Is it the right way to create a pure C++ static library?
Is there something wrong with the way how I invoke the C++ static
library?
In my example, when invoking the static library, how could I solve the link errors?
Many thanks.
This will do,
1)Create c++ library using same way, iOS->Framework&Library->Cocoa Touch Static Library in Xcode 6.
TestCPlusPlus.h
int sub(int a, int b);
TestCPlusPlus.cpp
int sub(int a, int b)
{
return a - b;
}
2) Build the static library keeping configuration iOS Device, then iPhone 6(Basically simulator.)
3) then Expand Products in File Browser view. Select your .a file, say libTestStaticLibrary.a , then Right Button > Show in Finder. Move up in folders. You should be able to see two folers Debug-iphoneos and Debug-iphonesimulator
4) now
open Terminal go till this Products path then type
lipo -create Debug-iphoneos/libTestStaticLibrary.a Debug-iphonesimulator/libTestStaticLibrary.a -output libTestStaticLibrary.a
5) Now open your project which uses this library, you need to drag and drop the static library files as well as the header files which have function declaration of static library functions.
6) Now, create Cocoa touch class file which will act as adaptor between static library and obejective -c files. Change the extension to .mm
MyCustomAdaptor.h
#interface MyCustomAdaptor : NSObject
-(int)getSub:(int ) a SecondParam:(int) b;
#end
MyCustomAdaptor.mm
#import "TestCPlusPlus.h"
#implementation MyCustomAdaptor
-(int)getSub:(int ) a SecondParam:(int) b
{
int c = sub(a,b);
return c;
}
#end
7) now use this MyCustomAdaptor in any of the objective c- file.
Please notice that your .a is build with i386 or armv7?
Generally, you should build both versions and combine them into one.
like this:
lipo -create -output libopencore-amrwb.a libopencore-amrwb-armv7.a libopencore-amrwb-i386.a
I'm currently doing the same as you. I've had the same problem you describe here, actually the same two errors.
When you build your library you have to have in mind where're you going to use it, iOS device or simulator.
This is important because you have to build for the different cases, this is very simple, when you build you library just check the "Select Scheme".
For Real device use:
Just to test in the simulator use:
After building just drag-drop the files created to the project you want to use the library and you're good to go!

How to implement Tesseract to run with project in Visual Studio 2010

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?

Having trouble using SDL framework on XCode

I am having trouble using the SDL framework in my xcode project, my main (all there is in the project at the moment) currently looks like this:
#include <SDL/SDL.h>
#include <iostream>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
And the error I am receiving when building is:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
(maybe you meant: __Z8SDL_mainiPPKc)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It does not give me an error about the framework not being found. and I have looked around Stackoverflow for this problem and I did find one person who had their framework in the wrong place on their OS. But I have tried all places (/library/frameworks && /~username~/library/frameworks && /system/library/frameworks) and still no luck.
Additional info:
I did however notice after some searching on the internet that the official website http://www.libsdl.org/download-1.2.php
Does not have a OSX version of the Development library. While many tutorials on how to use SDL on OSX say these are required.
Also I am adding my library via Xcode itself ,not through drag'ndrop. Xcode seems to reckognize it.
Would be much appreciated if anyone could help, going crazy over this error.
UPDATE:
Still no luck, I have followed every step provided by solutions here below. perhaps this screenshot is of any help.
The main() function is corrected, but didn't help. I tried changing the path to the header files, im lost on this one. Does anyone perhaps have any alternatives to building this in xcode?
UPDATE2:
The suggestion worked, however now it is giving me this warning, which won't let me run the application.
Fixed! I removed the path in the build settings. Strange how I still don't know what went wrong, either way. thanks a lot for the help! made my day!
I see three possible problems. The first problem is how you're including SDL.h in your code. You should include SDL.h with the following code:
#include "SDL.h"
The second possible problem is you haven't added the files SDLMain.h and SDLMain.m to your project. Those files are necessary to compile SDL code on Mac OS X. The files are in the devel-lite folder on the SDL disk image.
The third possible problem is that your project doesn't link to the Cocoa framework. The Mac version of SDL uses Cocoa so you need the Cocoa framework in your project.
The following article walks you through the setup of an Xcode 4 project for SDL:
Using SDL with Xcode 4
UPDATE
I noticed a possible problem in how you defined the main() function. You have a space between char and * and another space between * and argv, which could be causing the error. The main() function in my SDL code is defined in the following way:
int main(int argc, char *argv[])