Why does OpenCV code not work in my CodeBlocks IDE? - c++

The Background: I need to use OpenCV for various Image Processing projects. To get it to work on my system(32-bit Windows 7, CodeBlocks IDE, TDM-GCC 4.8.1) I downloaded the OpenCV 3.0 sources from the OpenCV website and built it using Cmake(the only significant thing I did in this step was disable ipp) and mingw compiler. I linked the include directory and libraries to the C::B environment.
The Problem: When I run the following sample code I get an error.
*
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat img;
img = imread("lena.jpg");
imshow("Original Image", img);
waitKey();
}
*
The error log being:
||=== Build: Debug in opencv (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function ZN2cv3Mat7releaseEv':|
F:\opencvbuild\include\opencv2\core\mat.inl.hpp|655|undefined reference to__atomic_fetch_add_4'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 9 second(s)) ===|
How do I get the library to work? Any possible hack or directly accessible binary files solution will do.

Related

Install opencv for C++ on Mac m1 chip properly

I've tried to install openCV for C++ using this tutorial : https://www.youtube.com/watch?v=KaTA-yK7dWA
and then try to set it up on Xcode using this video:
https://www.youtube.com/watch?v=2FYm3GOonhk&t=1266s
But when I ran the code:
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
///////////////// Webcam //////////////////////
int main() {
VideoCapture cap(1);
Mat img;
while (true) {
cap.read(img);
imshow("Image", img);
waitKey(1);
}
return 0;
}
I got the error:
ld: library not found for -lopencv_photo.4.5.5
clang: error: linker command failed with exit code 1 (use -v to see invocation)
don't know what to do, I'll be glad for help and guidance, it was much easier in python
Thanks in advance !
As the linker says - it cannot find the library lopencv_photo.4.5.5 to link against your program.
In Xcode the easiest option is to append the flag -lopencv_photo to Other Linker Flags in your Project Preferences.
Also - you wont be able to access a webcam on MacOS. There is no direct USB (serial) access to the video feed and it would require entitlements that requires a MacOS App project, not a command line tool.

Files/directories to include in Visual Studio C++ to use CUDA?

I want to use CUDA/GPU in OpenCV in Visual Studio. For example, cuda::GpuMat. I successfully build OpenCV with the extra modules with CUDA enabled
I tried the following code
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/photo/cuda.hpp>
#include <opencv2/photo.hpp>
using namespace std;
using namespace cv;
int main(){
string imageName("input.bmp");
//CPU version
Mat image = imread(imageName.c_str(), IMREAD_GRAYSCALE);
//CUDA version
cuda::GpuMat imageGPU;
cuda::GpuMat downloadGPU;
Mat buff;
imageGPU.upload(image);
downloadGPU.download(buff);
imwrite("gpu.bmp", buff);
return 0;
}
But I get an unhandled exception error.
I originally downloaded OpenCV in C:\Users\me\Downloads\opencv
I then downloaded and installed the latest OpenCV extra modules with CUDA on in
In Property Pages->C/C++->General->Additional Include Directories, I have:
C:\Users\me\Downloads\opencv\build\include\opencv
C:\Users\me\Downloads\opencv\build\include\opencv2
C:\Users\me\Downloads\opencv\build\include\
In Property Pages->Linker->General->Additional Library Directories, I have:
C:\Users\me\Downloads\opencv\build\x64\vc15\lib
and in Property Pages->Linker->Input->Additional Dependencies, I have:
opencv_world343d.lib
opencv_world343.lib
what else am I supposed to include so I can get GpuMat to work properly?
Most of the cases, yes, but you need to know which library you need to add, it may be cufft.lib, cublas.lib, cudnn.lib, etc. It depends of the function you use inside your code.
Opencv includes a cmake include file that would set all of this up for you if you use cmake to build you VS test project. This file will be in the root of the opencv install directory, i.e. after building opencv running cmake --install or the equivalent in VS. The file is OpenCVConfig.cmake, and it would be included in the CMakeLists.txt file for your project. Then you would call FindPackage(OpenCV), which would locate the OpenCV install, setup a few variables, which you would then use to link against your app.
I can post a sample CMakeList.txt file if you feel that would help.

Cannot compile using any external libraries (Codeblocks)

I cannot compile any code using external libraries on Code::Blocks.
I tried using the boost lambda example:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
I have a global environment variable set up for the library:
https://imgur.com/a/maiRC
...and I think I set up the build options right.
https://imgur.com/a/BP0Xk
But my build can't detect the header file:
||=== Build: Debug in boost test (compiler: GNU GCC Compiler) ===|
C:\Documents and Settings\Charlotte\My Documents\wxTest\boost test\boo.cpp|1|fatal error: boost/lambda/lambda.hpp: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Unfortunately the boost include path in your first image is cut off. But it looks like
C:\Boost\include\boost-1_62\boost
Right?
That’s most likely incorrect. If you include the lambda header like you did (which is also the usual way to include a boost Header)
#include <boost/lambda/lambda.hpp>
then there needs to exist a file called
C:\Boost\include\boost-1_62\boost\boost\lambda\lambda.hpp
That does not fit the normal Boost directory layout. There is one boost too many in there. Try setting your your global environment Boost path to:
C:\Boost\include\boost-1_62

LINK : fatal error LNK1104: cannot open file 'opencv_calib3d245d.lib' (OpenCV 2.4.9)

I'm using OpenCV 2.4.9 in Windows 7 with Visual Studio C++ 2010 Express.
I downloaded the OpenCV 2.4.9 and did the installation process according to this tutorial. Including set the environment variables.
http://opencv-srf.blogspot.com.br/2013/05/installing-configuring-opencv-with-vs.html
Except for the version of compiler, instead of "vc11" (like in the tutorial) I put "vc10".
Well, the rest I did exactly like in this tutorial.
I'm trying to run this simple code:
#include <stdio.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main ( int argc, char **argv ) {
Mat image = imread("9-002.tif", CV_LOAD_IMAGE_GRAYSCALE);
adaptiveThreshold(image, image, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 111, -20);
imwrite("rs_9-002.tif", image);
return 0;
}
And when I run the project I get this:
1>------ Build started: Project: Projeto de TESTES, Configuration: Debug Win32 ------
1> main.cpp
1>LINK : fatal error LNK1104: cannot open file 'opencv_calib3d245d.lib'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
the properties of my project are as follows:
I do not know how to fix this. Any idea what could be causing this error?
Looking at your Additional Dependencies you have listed libraries for opencv-2.4.5 not opencv-2.4.9. The libraries in your Additional Dependencies are named *245d.lib not *249d.lib. You will need to edit all the entries that have 245 in the library name and replace that with 249. Make sure you do that for all the configurations you use (Debug, Release ...).

Error with Poppler implementation with C++

I'm currently working on project which uses the poppler library. As It stands I have the following:
In the following folder (C:\Users\ ...\Anotation Extraction) I have all the files associated with a Codeblocks C++ project (including the main.cpp) as well as the current version of poppler and its associated documents in a folder called "poppler-0.22.2".
I also have a test pdf in this folder.
(System: Windows 7, 64 bit, Codeblocks using MinGW)
In my main.cpp file I have the following:
#include <iostream>
#include "poppler-0.22.2/cpp/poppler-document.h"
using namespace std;
int main()
{
const string dir = "C:\\Users\\...\\test.pdf";
poppler::document* doc;
doc = poppler::document::load_from_file(dir);
delete doc;
cout << dir << endl;
return 0;
}
However, when I try to build this code, the following errors pop up and I have absolutely no idea why.
obj\Debug\main.o||In function `main':|
C:\Users...\Anotation Extraction\main.cpp|11|undefined reference to `imp__ZN7poppler8document14load_from_fileERKSsS2_S2_'|
C:\Users...\Anotation Extraction\main.cpp|12|undefined reference to `imp__ZN7poppler8documentD1Ev'|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 1 seconds) ===|
Any help you guys could provide would be much appreciated.