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.
Related
This is my dir tree (root dir is untitled)
This is the content of CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
find_package (OpenCV 4.5.2 REQUIRED)
include_directories ("/usr/local/include/opencv4/")
add_executable(untitled main.cpp)
This is my code in main.cpp:
#include <iostream>
#include <string>
#include <sstream>
#include <opencv4/opencv2/core.hpp>
#include <opencv4/opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, const char** argv) {
Mat color = imread("./lenna.png");
Mat gray = imread("./lenna.png", 0);
imwrite("./lennaGray.png", 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("Lenna BGR", color);
imshow("Lenna Gray", gray);
waitKey(0);
return 0;
}
This is the entire error when I ran make command:
But if I ran my code in terminal by this command:
g++ main.cpp -o main -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -I/usr/local/include/opencv4 && ./main
then it worked.
I don't know what happened, I just learned OpenCV and I am using Ubuntu 20.04 and OpenCV-4.5.2.
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.
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 trying to use a cursor to seek a specific frame in stream.
main.cpp code follows:
#include <iostream>
#include <opencv2/opencv.hpp>
int main()
{
cv::VideoCapture cap;
if (!cap.open("8%d.jpg"))
return 1;
double index = cap.get(CV_CAP_PROP_POS_FRAMES);
index += 10.0;
std::cout << "Seek to index: " << std::endl;
if ( !(index < cap.get(CV_CAP_PROP_FRAME_COUNT)) )
return 1;
bool retval = cap.set(CV_CAP_PROP_POS_FRAMES, index);
assert(retval);
std::cout << cap.get(CV_CAP_PROP_POS_FRAMES) << std::endl;
cap.release();
return 0;
}
Command line on Ubuntu 14.4.3 LTS with OpenCV 2.4.11:
g++ main.cpp `pkg-config --cflags --libs opencv`
Command to check returning value:
echo "$#"
Output:
Seek to index: 10
-9.223377e+18
cv::VideoCapture::set function on an image sequence seems to break the stream, can you tell me why ?
Note: It works for a video as input but not an image sequence!
PS: I prefer asking before looking inside OpenCV-FFmpeg source code (it could take a while).
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;
}