I am compiling an example for OpenCV with the following code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
The compilation code is:
g++ -I/usr/local/include/opencv2 `pkg-config --cflags --libs opencv` -L /usr/local/share/OpenCV/3rdparty/lib/ opencv.cpp -o opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_features2d -lopencv_imgcodecs
If I omit one of the libraries I add additionally, I get linking errors. When trying to run the program, I get an"Invalid machine code" error. How can that be solved?
Related
I'm trying to open a video file in Opencv C++, but it doesn't work in a weird way.
I'm on Fedora, and opencv is installed through dnf's opencv-devel package.
Here is the test code I'm using :
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio/videoio.hpp>
int main(int argc, char *argv[])
{
std::string imagePath = "/home/me/picture.png";
cv::Mat image = cv::imread(imagePath);
std::cout << image << std::endl;
std::string videoPath = "/home/me/video.mp4";
cv::VideoCapture cap{ videoPath };
if(!cap.isOpened())
{
std::cout << "Error while opening video" << std::endl;
return -1;
}
cv::Mat startFrame;
cap >> startFrame;
std::cout << startFrame;
cap.release();
return 0;
}
Then, I Compile it with the libraries:
g++ -c -Wall -Wno-unknown-pragmas -I/usr/include/opencv4/ -o dist/obj/main.o src/main.cpp
g++ -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio dist/obj/main.o -o dist/bin/main
And when I run it, the image is read fine, but the video shows an error.
However, when I run it as sudo, the video reads just fine. I searched around a lot and couldn't find someone with a similar problem, so I'm posting this here.
Also, the python version works fine, so it shouldn'ttm be a codec problem.
I'm a complete beginner with cpp and opencv. I would like to compile and run this simple Program that lets me display an image. This works if I run the code inside XCode but if I try it using clang in the console it throws me a
linker command failed with exit code 1 (use -v to see invocation)
I had add these "linker flags" in Xcode to get opencv to run so I assume I need to somehow install them globally too. HHow would I achieve this?
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
if (argc != 2) {
cout << "Bitte zu ladendes Bild als Parameter angeben." << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
if (!image.data) {
cout << "Bild " << argv[1] << " wurde nicht gefunden." << std::endl ;
return -1;
}
namedWindow("OpenCV Window", WINDOW_AUTOSIZE);
imshow("OpenCV Window", image);
cout << "Image width: " << image.cols << ", image height: " <<
image.rows << endl;
waitKey(0);
return 0;
}
These are the Linker Flags inside Xcode:
-lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videoio -lopencv_videostab
I finally got OpenCV to work but I encountered a problem where my program does not proceed (no outcome). The outcome of the program should be an image in a new window. Snippet:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Point is, program builds successfully but when I debug it, nothing happens. I tried to Start Without Debugging (ctrl+F5, I am using MSVisual2012) and what popped up is:
How can I fix this? Is it related to bad library linking?
The output is "Usage: display_image ImageToLoadAndDisplay", which means the program didn't get the filename. You can:
image = imread("C:\myfile.jpg", IMREAD_COLOR); (and remove the previous lines)
open terminal, cd to the executable dir and run yourprogram.exe myfile.jpg
set command arguments in visual studio.
I have written a small program to prepare an image which is later processed by a FPGA-Board. I need to have it run on any windows machine, even if opencv is not installed. I have read a lot of tutorials and answers on stack overflow, but none of that seems to work for me. I compiled opencv myself using Cmake and MinGW to build the static libraries (.a files). How do I get those in my project, which ones do I need and how do I link them? It seems like everytime I try to link the libraries I used before (when using dlls) seems to have some kind of dependency to other ones.
Thanks in advance,
Brillow
The error message is:
g++ "-LE:\\opencv\\staticlibs\\lib" -static-libgcc -static-libstdc++ -mwindows -o img_prep.exe main.o -lIlmImf -ljasper -lpng -ltiff -ljpeg -lz -lopencv_highgui249 -lopencv_imgproc249 -lopencv_core249
E:\opencv\staticlibs\lib/libopencv_highgui249.a(grfmt_jpeg.cpp.obj):grfmt_jpeg.cpp:(.text.unlikely._ZN2cvL16my_jpeg_load_dhtEP22jpeg_decompress_structPhPP9JHUFF_TBLS5_.constprop.52+0xce): undefined reference to `jpeg_alloc_huff_table'
E:\opencv\staticlibs\lib/libopencv_highgui249.a(grfmt_jpeg.cpp.obj):grfmt_jpeg.cpp:(.text$_ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0xe2): undefined reference to `jpeg_CreateCompress'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: E:\opencv\staticlibs\lib/libopencv_highgui249.a(grfmt_jpeg.cpp.obj): bad reloc address 0xe2 in section `.text$_ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
I have linked the following libraries in the MinGW linker:
Here's my code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
if (argc != 2) {
cout << "Usage: img_prep InputImage" << endl;
return -1;
}
Mat img, img_gray;
Size size(800,600);
img = imread(argv[1], CV_LOAD_IMAGE_COLOR);
if (!img.data) {
cout << "Could not read the image!" << endl;
return -1;
}
cvtColor(img, img_gray, CV_RGB2GRAY);
resize(img_gray, img, size);
cout << "Image successfully converted!" << endl;
ofstream output;
output.open("output.img", ios::binary);
output.write((char *) img.data, img.rows * img.cols);
output.close();
imwrite("output.bmp", img);
cout << "Image saved!" << endl;
//Ergebnis anzeigen:
namedWindow("Display", WINDOW_AUTOSIZE);
imshow("Display", img);
waitKey(0);
return 0;
}
I have a problem under Qt, Actually I want to use opencv under it (ubuntu) and there is a crash.
If I compile under the terminal :
g++ pkg-config --cflags opencv example.cc -o output_file pkg-config --libs opencv
All is all right but under QT there is a crashed problem and I just read this message error :
Starting /home/quentin/build-test_opencv-Desktop_Qt_5_2_1_GCC_64bit-Release/test_opencv...
The program has unexpectedly finished.
/home/quentin/build-test_opencv-Desktop_Qt_5_2_1_GCC_64bit-Release/test_opencv crashed
This is my .pro :
QT += core
QT -= gui
TARGET = test_opencv
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
CONFIG += link_pkgconfig
PKGCONFIG += opencv
SOURCES += main.cpp
INCLUDEPATH += -I /usr/local/include/opencv
LIBS += `pkg-config opencv --libs`
and this is my main.cpp :
#include <QCoreApplication>
#include <iostream>
#include "cv.h"
#include "highgui.h"
using namespace std;
int main(int argc, char *argv[])
{
IplImage* img = cvLoadImage( "lena.png" );
cout << "Image WIDTH = " << img->width << endl;
cout << "Image HEIGHT = " << img->height << endl;
cvReleaseImage( &img );
return 0;
}
Most likely cvLoadImage fails and returns nullptr. You never bother checking for that.
What version of openCV do you use? What berak means is that IplImage is not used in the newer versions of openCV. Use Mat instead of IplImage. Try this code and tell me what happens:
#include <QCoreApplication>
#include <iostream>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
int main(int argc, char *argv[])
{
Mat* img = new cv::Mat(cv::imread("lena.png",CV_LOAD_IMAGE_COLOR));
if(img == NULL){
perror("Could not load image");
}
std::cout << "Image WIDTH = " << img->cols << std::endl;
std::cout << "Image HEIGHT = " << img->rows << std::endl;
img->release();
return 0;
}
This will work in opencv 2.4.X. Also make sure your image is in the same folder as your program. Please tell me of any error.