I have a simple GTK+ v3 GUI application, and I am making use of the OpenCV library so that I have a simple function for taking pictures from the one webcam connected to my computer. The code is included at the bottom of this post.
I'm able to successfully acquire image data and render it on screen, but when I include this code in my GTK+ v3 project, I get a startup error like so:
(result:2944): Gtk-ERROR **: GTK+ 2.x symbols detected.
Using GTK+ 2.x and GTK+3 in the same process is not supported.
Trace/breakpoint trap.
So, this makes sense so far. One of the OpenCV libraries apparently makes use of Gtk+ v2. It turns out that if I remove libopencv_highgui from my list of libraries to link against, I won't have this issue. However, the functions used to acquire image data from the webcam is included in that library for some reason.
Are there other functions accessble via the C or C++ APIs for OpenCV that don't require me to make use of libopencv_highgui and allow me to take a snapshot from a webcam with ease?
The other alternative seem to be re-writing my project as a Gtk+ v2 application, which wouldn't be so bad, seeing as I haven't gone too far into it.
Is there a hidden option C out there? (Pardon the pun ;) ).
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <errno.h>
using namespace std;
using namespace cv;
#define PROJECT_NAME "CAMERA_MODULE" // Include before liblog
#include "../../lib/liblog/inc/log.h"
int cameraAcquireImage(void) {
CvCapture* capture = 0;
Mat frame, frameCopy, image;
//0=default, -1=any camera, 1..99=your camera
capture = cvCaptureFromCAM(CV_CAP_ANY);
if(!capture) {
logError("No camera interface detected");
return (-EIO);
}
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
if(capture) {
logError("Capture in progress");
for( ; ;) {
IplImage* iplImg = cvQueryFrame(capture);
frame = iplImg;
if(frame.empty()) {
break;
}
if(iplImg->origin == IPL_ORIGIN_TL) {
frame.copyTo(frameCopy);
} else {
flip(frame, frameCopy, 0);
}
cvShowImage( "result", iplImg );
if( waitKey( 10 ) >= 0 ) {
break;
}
}
}
cvReleaseCapture( &capture );
cvDestroyWindow("result");
return 0;
}
I can think of the following solutions:
Downgrade to GTK 2 - pretty simple.
Since VideoCapture is one the very few modules that depend on
highgui, use something else for video capture (Video4Linux perhaps)
and then use OpenCV modules which do not depend on highgui.
Build OpenCV with GTK 3 support (WITH_GTK=ON WITH_GTK3=ON).
Use Qt instead of GTK if you can.
Related
I am using console linux and I have a camera capture application. I need to capture an image without GUI(The camera should start and capture some images, save it to disk and close). The following code works well on my laptop but doesn't start on console. Any suggestions?
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
Mat frame;
namedWindow("feed",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("feed", frame);
imwrite("/home/zaif/output.png", frame);
if(waitKey(1) >= 0) break;
}
return 0;
}
After the release of OpenCV 2.4.6 there were bug fixes for video capture on Linux. Go straight to 2.4.6.2 and you should get the fixes. Specifically, this revision is probably the relevant fix for you, although there were a number of other revisions pertaining to video capture on android that might effect Linux compilation too.
I am trying to write a very basic video capturing program using opencv, but despite all my efforts, nothing gets written. I am fairly sure that i am following all the tutorials one can find on the subject.
Here is the code i am using:
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <videoInput.h>
static const cv::Size _SIZE(640, 480);
static const int FPS = 20;
int webcam = 0;
std::auto_ptr<videoInput> VI(NULL);
std::auto_ptr<cv::VideoWriter> outputVideo(NULL);
cv::Mat frame(_SIZE.height, _SIZE.width, CV_8UC3);
// get device list create videoInput
videoInput::listDevices();
VI.reset(new videoInput())
// choose first device
VI->setupDevice(webcam, _SIZE.width, _SIZE.height);
// always check
if(!VI->isDeviceSetup(webcam))
return -1; //-->
// set device frame rate
VI->setIdealFramerate(webcam, FPS);
// create named window and image
cv::namedWindow("CAM");
do
if (VI->isFrameNew(webcam))
{
VI->getPixels(webcam, (unsigned char*)frame.data, false, true); // получение пикселей в BGR
if (outputVideo.get())
{
(*outputVideo).write( frame);
}
char c = cv::waitKey(5);
if (!IsWindowVisible((HWND)cvGetWindowHandle("CAM")) || c==27)
{
exit(0);
}
cv::imshow("CAM", frame);
} while(1);
I have tried various extension and various Fourcc values. Usually, on any extension except avi, writer is created but does nothing. on the contrary, avi files writers simply fail to be created.
I have read that probably codecs are missing, but what does that mean - what exactly do i need to put where for them to be found by opencv?
All in all, i am very confused. This is tutorial code, it should work.
Recently installed OpenCV 2.4.3 to try to do some video capturing and object distinction. But sadly, every attempt to capture video through web-camera results in memory access violation.
I'm using Visual Studio 2010 (Win 7 x86), and web-camera "A4 Tech USB2.0". First I thought that maybe problem is with camera itself, but then i tried using videoInput.h lib to get any response from camera, still no result. (Other apps like Skype see it (and make it work) no-problem).
Here is a code (almost by the book):
<pre>
#include "cv.h"
#include "highgui.h"
#include "stdlib.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
CvCapture* capture = cvCreateCameraCapture(CV_CAP_ANY); //cvCaptureFromCAM( 0 );
assert( capture );
double width = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
double height = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
printf("[i] %.0f x %.0f\n", width, height );
IplImage* frame=0;
cvNamedWindow("capture", CV_WINDOW_AUTOSIZE);
printf("[i] press Esc for quit!\n\n");
if(capture != NULL)
{
while(true)
{
frame = cvQueryFrame( capture ); //it crashes here all the time
cvShowImage("capture", frame);
char c = cvWaitKey(35);
if (c == 27)
{
break;
}
}
}
cvReleaseCapture( &capture );
cvDestroyWindow("capture");
return 0;
}
</pre>
Read other topics with the same issue and tried to account some problems:
(add interval between captures cvWaitKey(35),
add check for if capture device really exists if (capture != NULL)
) but still can`t understand why this keep happening.
UPDATE: What i basically get in the end is console with generated atributes of the OpenCVwindow and window itself(gray background it seems). And memory access violation error.
After some thought and testing, found the resolution.
It seems, that DirectVobSub filter on DirectShow somehow prevented me from accessing my camera programmly (both through OpenCV and VideoInput libs). It created a new instance of itself every time I ran my program and accessed my camera, which led to Access Memory Violation). After uninstalling it from my PC, the code started to work.
Good day everyone! So currently I'm working on a project with video processing, so I decided to give a try to OpenCV. As I'm new to it, I decided to find few sample codes and test them out. First one, is C OpenCV and looks like this:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
int main( void ) {
CvCapture* capture = 0;
IplImage *frame = 0;
if (!(capture = cvCaptureFromCAM(0)))
printf("Cannot initialize camera\n");
cvNamedWindow("Capture", CV_WINDOW_AUTOSIZE);
while (1) {
frame = cvQueryFrame(capture);
if (!frame)
break;
IplImage *temp = cvCreateImage(cvSize(frame->width/2, frame->height/2), frame->depth, frame->nChannels); // A new Image half size
cvResize(frame, temp, CV_INTER_CUBIC); // Resize
cvSaveImage("test.jpg", temp, 0); // Save this image
cvShowImage("Capture", frame); // Display the frame
cvReleaseImage(&temp);
if (cvWaitKey(5000) == 27) // Escape key and wait, 5 sec per capture
break;
}
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
return 0;
}
So, this one works perfectly well and stores image to hard drive nicely. But problems begin with next sample, which uses C++ OpenCV:
#include "opencv2/opencv.hpp"
#include <string>
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
//namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_RGB2XYZ);
imshow("edges", edges);
//imshow("edges2", frame);
//imwrite("test1.jpg", frame);
if(waitKey(1000) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
So, yeah, generally, in terms of showing video (image frames) there is practically no changes, but when it comes to using im** functions, some problems arise.
Using cvSaveImage() works out nicely, but the moment I try to use imwrite(), unhandled exception arises in regards of 'access violation reading location'. Same goes for imread(), when I'm trying to load image.
So, the thing I wanted to ask, is it possible to use most of the functionality with C OpenCV? Or is it necessary to use C++ OpenCV. If yes, is there any solution for the problem I described earlier.
Also as stated here, images initially are in BGR-format, so conversion needed. But doing BGR2XYZ conversion seems to invert colors, while RGB2XYZ preserve them. Examples:
images
Or is it necessary to use C++ OpenCV?
No, there is no necessity whatsoever. You can use any interface you like and you think you are good with it (OpenCV offers C, C++, Python interfaces).
For your problem about imwrite() and imread() :
For color images the order channel is normally Blue, Green, Red , this
is what imshow() , imread() and imwrite() expect
Quoted from there
I have searched a lot about my simple problem but I didn't find solution. When I run my code black console shows me the camera frame size but in the window video is not showing, it shows a solid gray screen. But if I play a video from HDD then it works fine.
Please help me some one.
This is my code
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
int main(int argc, char** argv){
CvCapture *capture;
IplImage* img=0;
cvNamedWindow("Window");
capture = cvCreateCameraCapture( -1);
//capture = cvCaptureFromAVI("1.mp4");
//capture = cvCaptureFromCAM(-1);
int ext=0;
assert( capture );
if(capture==NULL){
cout<<"Cam Not Found!!!"<<endl;
getchar();
return -5;
}
while ( true ){
img = cvQueryFrame( capture );
cvSaveImage("1.jpg",img);
if (!img){
printf("Image not Found\n");
break;
}
cvShowImage("Window", img);
cvWaitKey(50);
}
cvReleaseImage(&img);
cvDestroyWindow("Window");
cvReleaseCapture(&capture);
return 0;
}
I use opencv 2.2 and Visual studio 2010
One thing is obviouslly wrong, you need to change the order of the calls to:
cvShowImage("Window", img);
cv::waitKey(20);
Second, it's essential that you check the success of cvQueryFrame():
img = cvQueryFrame( capture );
if (!img)
{
// print something
break;
}
EDIT:
By the way, I just noticed you are mixing the C interface of OpenCV with the C++ interface. Don't do that! Replace cv::waitKey(50); by cvWaitKey(50);.
For debugging purposes, if cvQueryFrame() succeeds I suggest you store one frame to the disk with cvSaveImage(), and if that image is OK it means the capture procedure is actually working perfectly and the problem is somewhere else.
I jast switch the openCV version 2.2 to 2.1 and its work perfectly.......
I am using OpenCV version 3.1, I got the same problem, I re-built openCV 3.1 and re-checked Environment Variables, so my problem resolved. You can back-up built-opencv and extract if you need. Sorry for my bad english :)