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).
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've got a path to my file defined this way:
const char* GROUND_TEXTURE_FILE = "objects/textures/grass.jpg";
And here is the function, which I use to load image:
bool loadTexImage2D(const string &fileName, GLenum target) {
...
// this will load image data to the currently bound image
// at first, we must convert fileName, for ascii, this method is fine?
wstring file(fileName.begin(), fileName.end());
if(ilLoadImage(file.c_str()) == IL_FALSE) { //here the program falls
What's wrong in my code? Why the program falls when ilLoadImage is called? I think, that file.c_str() should work fine as a wchar_t * type or not? Thanks for answer :)
As the author's said, you can do pretty anything without initializing the lib :D
#include <iostream>
#include <IL/il.h>
int main ()
{
std::string filename = "objects/textures/grass.jpg";
ilInit();
if (!ilLoadImage(filename.c_str())) {
std::cout << ilGetError() << std::endl;
return 1;
}
std::cout << ilGetInteger(IL_IMAGE_WIDTH) << std::endl;
std::cout << ilGetInteger(IL_IMAGE_HEIGHT) << std::endl;
return 0;
}
build:
g++ -Wall -pedantic --std=c++11 -g -o app main.cpp -lIL
I am using opencv library to obtain video from the built-in webcam. The following code works perfectly well when i put camera logic code into the main function, but it doesn't when i put it into separate thread. The task1() thread stops at cv::VideoCapture capture(0). Meanwhile both task2() and the main thread are executing correctly.
Could someone explain me why opencv logic doesn't work when put into separate thread?
My code:
#include <iostream>
#include <string.h>
#include <thread>
#include <unistd.h>
#include <opencv2/opencv.hpp>
using namespace std;
void task1 (){
cout<<"1st thread ";
cv::Mat frame;
cv::VideoCapture capture(0);
if ( capture.isOpened() == false )
{
cout<<"Failed to open camera";
}
cv::namedWindow("Test OpenCV",1);
while ( true ){
capture >> frame;
cv::imshow("Test OpenCV", frame );
int key = cv::waitKey(1);
if ( key == 27 )
break;
}
}
void task2 (){
int n = 0;
while (1){
cout<<"2nd thread "<<n<<"\n";
sleep(3);
n++;
}
}
int main(int argc, const char * argv[]) {
// insert code here...
cout << "Hello, World!\n";
thread t1(task1);
thread t2(task2);
//t1.join();
//t2.join();
int n = 0;
while (1){
cout<<"main thread "<<n<<"\n";
sleep(1);
n++;
}
return 0;
}
Your code runs as it should for me (without any modifications) and I get the live feed through the task1 thread (using OpenCV 2.4.5).
I added -std=gnu++0x flag for compiler support (otherwise g++ throws an error).
g++ -std=gnu++0x opencv_thread.cpp -o opencv_thread `pkg-config --cflags --libs opencv`
Check my console output here. I added a cout << "1st thread "<< endl; within the while loop in task1.
I think the issue might be specific to some opencv versions as I have seen similar issues in an older version (don't remember which one) and boost threads.
Can you give details about the version you used?. Also try it with 2.4.5.
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.
I'm trying to make this simple GraphicsMagick example as a node binding/addon. This code works as expected in OSX 10.6.7 with GraphicsMagick 1.3.15
#include <Magick++.h>
#include <iostream>
using namespace std;
int main(int argc,char **argv)
{
Magick::InitializeMagick(0);
Magick::Image image;
try {
image.read( "snow.jpg" );
image.scale("320");
image.write( "snow-scaled.jpg" );
}
catch( Magick::Exception &error_ ) {
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
cout << "Image scaled!" << endl;
return 0;
}
Compiling:
g++ scale.cpp `GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs`
Running:
./a.out
Image scaled!
But making this code a node binding (0.6.14) just freezes (see full gist):
void AsyncWork(uv_work_t* req) {
std::cout << "AsyncWork..." << std::endl;
Baton* baton = static_cast<Baton*>(req->data);
baton->result = 12345; // Just a test
Magick::Image image; // <--- Freezes here!
image.read("snow.jpg");
std::cout << "Scaling..." << std::endl;
image.scale("200");
std::cout << "Done!" << std::endl;
image.write("snow-scaled.jpg");
// and baton->error to true.
}
Output when calling it from javascript:
AsyncWork...
Any ideas what's wrong?
On a side note, this actually works when compiled/run under Ubuntu!
Have you tried to initialise with Magick::InitializeMagick(0); in AsyncWork? Asynch functions run on pool threads.
You could always just git the finished GM addon here.