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.
Related
So I have OpenCV 3.1.0 set up on my computer running on Ubuntu 16.04 and I'm trying to run some very simple code to try and test OpenCV and whilst it recognises the opencv #include files, I continue to have compiler errors.
Here is my C++ code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(int argc, char** argv) {
Mat color = imread("lena.jpg");
Mat gray = imread("lena.jpg",0);
imwrite("lenaGray.jpg", gray);
int myRow = color.cols-1;
int myCol = color.rows-1;
Vec3b pixel = color.at<Vec3b>(myRow, myCol);
cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," << (int)pixel[1] << "," << (int)pixel[2] << ")" << endl;
imshow("Lena BGR", color);
imshow("Lena Gray", gray);
waitKey(0);
return 0;
}
And I attempt to compile it on the Linux terminal as such:
g++ `pkg-config opencv --cflags --libs` main.cpp -o main
And this is the error that's returned:
/usr/bin/ld: cannot find -lippicv
collect2: error: ld returned 1 exit status
As the title said, I am a complete beginner at the Linux operating system whilst I am relatively competent at using the OpenCV library. I simply want to be able to use the OpenCV library and I am unable to with this error and I am very frustrated with it.
Thanks in advance for any help!
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?
I recently installed opencv on Ubuntu 12.04.5 from the repository using this command.
sudo apt-get install libopencv-dev python-opencv
When I try to run the following code to confirm that it works properly I get an Illegal instruction (it compiled fine).
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include<iostream>
using namespace std;
int main(){
cv::Mat img;
img = cv::imread("RO.ppm");
cout << img.size() << endl;
return 0;
}
I compiled using this command (due to undefined reference errors).
g++ -o test test.cpp $(pkg-config opencv --cflags --libs)
Update: Commenting out the cout line does not change the result and I've triple checked that RO.ppm exists in this directory (even if it didn't imread doesn't throw an error with illegal or not found input in my experience). I guess my question is two-fold what causes illegal instruction errors and how do I fix it?
You can't cout cv::Size directly without overloading '<<' operator for cv::Size. Instead you can get rows and columns from cv::Size and multiply them in order to get total size of the image:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
int main(){
cv::Mat img;
img = cv::imread("RO.ppm");
cv::Size img_size = img.size();
int cols = img_size.width;
int rows = img_size.height;
cout << "image size: " << rows*cols << endl;
return 0;
}
See this similar post for usage of cv::Size.
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.