I am quite new to c++ compilation. I am trying to work on a simple problem using opencv where I read a video file and display it.
My code looks like:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "opencv2/opencv.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <ctype.h>
#include <unistd.h>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace cv;
IplImage* image = 0;
IplImage* prev_image = 0;
int show = 1;
int main( int argc, char** argv )
{
int frameNum = 0;
char* video = argv[1];
VideoCapture capture(video);
if( !capture.isOpened() ) {
printf( "Could not initialize capturing..\n" );
return -1;
}
if( show == 1 )
cvNamedWindow( "Video", 0 );
while( true ) {
IplImage* frame = 0;
int i, j, c;
// get a new frame
//frame = cvQueryFrame( capture );
Mat mat_img;
capture >> mat_img;
IplImage frame1 = mat_img.operator IplImage();
frame = &frame1;
if( !frame )
break;
if( !image ) {
image = cvCreateImage( cvSize(frame->width,frame->height), 8, 3 );
image->origin = frame->origin;
}
cvCopy( frame, image, 0 );
if( show == 1 ) {
cvShowImage( "Video", image);
c = cvWaitKey(3);
if((char)c == 27) break;
}
std::cerr << "The " << frameNum << "-th frame" << std::endl;
frameNum++;
}
if( show == 1 )
cvDestroyWindow("Video");
return 0;
}
Then I compile it like:
g++ test.cpp -o Video -pipe -D __STDC_CONSTANT_MACROS -D STD=std -Wall -I. -I/usr/local/ -O3 -DNDEBUG -ggdb -L/usr/local/ -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc -lavformat -lavdevice -lavutil -lavcodec -lswscale
Compilation works fine and no errors returned.
However, when I was running it, I got:
(Video:5651): GLib-GObject-CRITICAL **: g_object_set: assertion 'G_IS_OBJECT (object)' failed
Could not initialize capturing..
Some other information:
1. I test my opencv and ffmpeg by running simple examples, which work well.
2. I can stream frames from my camera and display it using opencv.
Anyone has idea of what causes this?
Any idea is appreciated.
Related
I'm using visual studio 2019 with OpenCV 4.4.0
every thing was great but when i want to start face detection the cascade classifiar doesn't load the haarcascade
you also have to know that i installed openCV in the c partition and this is a simple code
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <opencv2\opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <Windows.h>
#include <vector>
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cam(0);
Mat img;
CascadeClassifier detector;
vector<Rect> faces;
Point p[2];
bool cap = false;
if (!detector.load("c:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml"))
{
cout << "Image Detector Doesn't work\n";
return EXIT_FAILURE;
}
if (!cam.isOpened())
{
cout << "Can't Open Camera\n";
return EXIT_FAILURE;
}
while (!cap)
{
cam.read(img);
imshow("Cam", img);
waitKey(0);
if (GetAsyncKeyState(VK_ESCAPE))
cap = true;
}
destroyWindow("Cam");
cout << "Detecting Face...\n";
detector.detectMultiScale(img, faces);
for (int i = 0; i < faces.size(); i++)
{
p[0] = Point(faces[i].x,faces[i].y);
p[1] = Point(faces[i].x + faces[i].height,faces[i].y + faces[i].width);
rectangle(img,p[0],p[1],Scalar(0,0,255),3);
}
imwrite("Result.jpg",img);
return EXIT_SUCCESS;
}
this code doesn't load the haarcascade and it returns "can't load" in the cmd
so i really need help with and thanks for all
\ is used as escape sequence in C++ string literals.
Therefore, you should use \\ to put a character \ in them.
if (!dec.load("c:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml"))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am getting the following error when trying to compile my code:
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
I am using the following command:
g++ detectTemplatePoints.cpp -o SURF_TemplatePoints `pkg-config --cflags --libs opencv`
From what I can find online this seems to happen when you do not have a main entry point included but I do have that, my code below:
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace cv;
void readme();
int main (int argc, char* argv[]) {
if( argc != 2 ) {
readme(); return -1;
}
Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
if( !img_1.data ) {
std::cout<< " --(!) Error reading images " << std::endl; return -1;
}
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_1;
detector.detect( img_1, keypoints_1 );
Mat img_keypoints_1;
drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
imshow("Keypoints 1", img_keypoints_1 );
waitKey(0);
return 0;
}
void readme() {
std::cout << " Usage: ./detectTemplatePoints <img1>" << std::endl;
}
What is causing this error?
As the error message says: you have no main function. They have to have one of the following signatures:
int main()
or
int main(int, char**)
First of all, you should use g++ detectTemplatePoints.cpp -o SURF_TemplatePoints $(pkg-config --cflags --libs opencv ) in order to link opencv libraries.
Also, you haven't shown us the libraries you are including.
I can get it compiled so:
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>
using namespace std;
using namespace cv;
void readme();
int main (int argc, char** argv) {
if( argc != 2 ) {
readme(); return -1;
}
Mat img_1 = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
if( !img_1.data ) {
std::cout<< " --(!) Error reading images " << std::endl; return -1;
}
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_1;
detector.detect( img_1, keypoints_1 );
Mat img_keypoints_1;
drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
imshow("Keypoints 1", img_keypoints_1 );
waitKey(0);
return 0;
}
void readme() {
std::cout << " Usage: ./detectTemplatePoints <img1>" << std::endl;
}
I am using this piece of code to grab frames off a video :
#include <stdio.h>
#include <stdlib.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main (int argc, char** argv)
{
//initializing capture from file
CvCapture * capture = cvCaptureFromAVI ("/home/<some_file>.avi");
//Capturing a frame
IplImage* img = 0;
if(!cvGrabFrame(capture)) //capture a frame
{
cout << Could not grab a frame\n\7";
exit(0);
}
img=cvRetrieveFrame(capture); //retrieve the captured frame
//free resources
cvReleaseCapture(&capture);
}
Which is returning :
Could not grab a frame
Additional details :
I had used code to save webcam video feed to the file from which i want to grab frames .
I used this code :
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
int main( int argc, char** argv ) {
CvCapture* capture;
capture = cvCreateCameraCapture(0);
assert( capture != NULL );
IplImage* bgr_frame = cvQueryFrame( capture );
CvSize size = cvSize(
(int)cvGetCaptureProperty( capture,
CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty( capture,
CV_CAP_PROP_FRAME_HEIGHT)
);
cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );
CvVideoWriter *writer = cvCreateVideoWriter( "/Users/user/Desktop/OpenCV_trial/OpenCV_trial/vidtry.AVI",
CV_FOURCC('D','I','V','X'),
30,
size
);
while( (bgr_frame = cvQueryFrame( capture )) != NULL )
{
cvWriteFrame(writer, bgr_frame );
cvShowImage( "Webcam", bgr_frame );
char c = cvWaitKey( 33 );
if( c == 27 ) break;
}
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
cvDestroyWindow( "Webcam" );
return( 0 );
}
Does anyone know where I might be going wrong ? I am running OpenCV-2.4.3 on a Beagleboard -xM with Ubuntu Quantal.
I am not quite sure what your exactly question is, but if you want to grab frames from a video, you should at least have a loop.
A reason for your error could be, that your video file is not available. Have you tried another one? The full path of the file? Or put the file directly into your working directory and check it.
Another reason could be a problem with the first frame (this sometimes happens). So try to remove your exit and enclose your code with a loop over all frames.
Here is an example that shows the given video file (Consider to use the C++-interface):
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main (int argc, char** argv)
{
//initializing capture from file
Mat img;
VideoCapture capture("a.avi");
if(!capture.isOpened())
{
cout<<"Could not open video!\n";
return 1;
}
while(true)
{
//Capturing a frame
capture >> img;
if(!img.empty())
{
imshow("Video",img);
}
else
{
cout <<"Could not grab a frame\n";
break;
}
if(waitKey(5) >= 0)
break;
}
return 0;
}
This program runs on my PC if the file "a.avi" is in the current working directory of the program.
See the ticked answer below :)
Error 1 error C2065: 'capture' : undeclared identifier
Using VS2013 Express with OpenCV
Older code examples have worked, but I cant get this one to:
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace cv;
int main()
{
Mat frame = cvQueryFrame(capture);
imshow("Video", frame);
}
I had to change "opencv2/core/core.hpp"
To #include <opencv2\core\core.hpp>, and It got that bit.
but I've tried including highgui but I cant get "capture" to work?
Any ideas?
x64 on Debug, and using x64 libs...
that capture part is a leftover from the old c-api.
try this instead:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
int main()
{
VideoCapture cap(0);
while( cap.isOpened() )
{
Mat frame;
if ( ! cap.read(frame) )
break;
imshow("lalala",frame);
int k = waitKey(10);
if ( k==27 )
break;
}
return 0;
}
Of course how will it work when you haven't declared the variable capture? Probably you want to do something like this:
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace cv;
int main()
{
CvCapture* capture = cvCreateFileCapture("path to video file");
Mat frame = cvQueryFrame(capture);
imshow("Video", frame);
waitKey();
cvReleaseCapture(&capture);
}
I'm using OpenCV master branch (3.0.0. dev) with CUDA on Ubuntu 12.04, and trying to compile the following opencv with gpu code:
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"
using namespace cv;
int main (int argc, char* argv[])
{
try
{
cv::Mat src_host = cv::imread("file.png", CV_LOAD_IMAGE_GRAYSCALE);
cv::gpu::GpuMat dst, src;
src.upload(src_host);
cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);
cv::Mat result_host = dst;
cv::imshow("Result", result_host);
cv::waitKey();
}
catch(const cv::Exception& ex)
{
std::cout << "Error: " << ex.what() << std::endl;
}
return 0;
}
The compiling command is:
g++ testgpu.cpp -o test `pkg-config --cflags --libs opencv` -lopencv_gpu
It has the following compiling errors:
testgpu.cpp: In function ‘int main(int, char**)’:
testgpu.cpp:13:51: error: ‘CV_LOAD_IMAGE_GRAYSCALE’ was not declared in this scope
cv::Mat src_host = cv::imread("file.png", CV_LOAD_IMAGE_GRAYSCALE);
^
testgpu.cpp:17:52: error: ‘CV_THRESH_BINARY’ was not declared in this scope
cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY);
^
testgpu.cpp:19:31: error: conversion from ‘cv::gpu::GpuMat’ to non-scalar type ‘cv::Mat’ requested
cv::Mat result_host = dst;
^
It is something wrong with the installation of OpenCV, or the API change in Opencv 3.0.0?
The gpu module was redesigned in OpenCV 3.0. It was splitted onto several modules, it was renamed to cuda and gpu:: namespace was renamed to cuda::. The correct code for OpenCV 3.0:
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/cudaarithm.hpp"
using namespace cv;
int main (int argc, char* argv[])
{
try
{
cv::Mat src_host = cv::imread("file.png", cv::IMREAD_GRAYSCALE);
cv::cuda::GpuMat dst, src;
src.upload(src_host);
cv::cuda::threshold(src, dst, 128.0, 255.0, cv::THRESH_BINARY);
cv::Mat result_host(dst);
cv::imshow("Result", result_host);
cv::waitKey();
}
catch(const cv::Exception& ex)
{
std::cout << "Error: " << ex.what() << std::endl;
}
return 0;
}
Ah, they've been playing with the constants in master. Expect the CV_* prefix removed almost anywhere ( except the types, CV_8U and such are still alive).
So it's cv::THRESH_BINARY, cv::LOAD_IMAGE_GRAYSCALE, but .... cv::COLOR_BGR2GRAY (you didn't use it now, but i'll spare you the searching ;) )
Sorry, I've no experience with GPU stuff, so i can't solve the last riddle there.