Undefined Reference to cv::Tracker::update - c++

I am trying to use the tracking API of OpenCV. I did make of OpenCV by following the instructions here:https://github.com/itseez/opencv_contrib/. On building I had to turn off a few parameters in CMake gui. After make, I ran the following code using Tracking API:
#include <opencv2/core/utility.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <tracker.hpp>
#include <iostream>
#include <cstring>
using namespace std;
using namespace cv;
static Mat image;
static Rect2d boundingBox;
static bool paused;
static bool selectObject = false;
static bool startSelection = false;
static void onMouse( int event, int x, int y, int, void* )
{
if( !selectObject )
{
switch ( event )
{
case EVENT_LBUTTONDOWN:
//set origin of the bounding box
startSelection = true;
boundingBox.x = x;
boundingBox.y = y;
boundingBox.width = boundingBox.height = 0;
break;
case EVENT_LBUTTONUP:
//sei with and height of the bounding box
boundingBox.width = std::abs( x - boundingBox.x );
boundingBox.height = std::abs( y - boundingBox.y );
paused = false;
selectObject = true;
break;
case EVENT_MOUSEMOVE:
if( startSelection && !selectObject )
{
//draw the bounding box
Mat currentFrame;
image.copyTo( currentFrame );
rectangle( currentFrame, Point((int) boundingBox.x, (int)boundingBox.y ), Point( x, y ), Scalar( 255, 0, 0 ), 2, 1 );
imshow( "Tracking API", currentFrame );
}
break;
}
}
}
//
// Hot keys:
// q - quit the program
// p - pause video
//
int main( int argc, char** argv )
{
//open the capture
VideoCapture cap(0);
if( !cap.isOpened() )
{
return -1;
}
//
// "MIL", "BOOSTING", "MEDIANFLOW", "TLD"
//
string tracker_algorithm = "MIL";
if ( argc>1 ) tracker_algorithm = argv[1];
Mat frame;
paused = false;
namedWindow( "Tracking API", 0 );
setMouseCallback( "Tracking API", onMouse, 0 );
Ptr<Tracker> tracker = Tracker::create( tracker_algorithm );
if( tracker == NULL )
{
cout << "***Error in the instantiation of the tracker...***\n";
return -1;
}
//get the first frame
cap >> frame;
frame.copyTo( image );
imshow( "Tracking API", image );
bool initialized = false;
int frameCounter = 0;
for ( ;; )
{
char c = (char) waitKey( 2 );
if( c == 'q' || c == 27 )
break;
if( c == 'p' )
paused = !paused;
if ( !paused )
{
cap >> frame;
if(frame.empty())
{
break;
}
frame.copyTo( image );
if( selectObject )
{
if( !initialized )
{
//initializes the tracker
if( !tracker->init( frame, boundingBox ) )
{
cout << "***Could not initialize tracker...***\n";
return -1;
}
initialized = true;
}
else
{
//updates the tracker
if( tracker->update( frame, boundingBox ) )
{
rectangle( image, boundingBox, Scalar( 255, 0, 0 ), 2, 1 );
}
}
}
imshow( "Tracking API", image );
frameCounter++;
}
}
return 0;
}
However I am getting an error that the class Tracker's functions are not defined, on linking. Here is part of my build log:
g++.exe -LC:\opencv\min_bin\install\x64\mingw\lib -o bin\Debug\main_project.exe obj\Debug\t1.o C:\opencv\min_bin\install\x64\mingw\lib\libopencv_bgsegm300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_bioinspired300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_calib3d300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_ccalib300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_core300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_dnn300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_dpm300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_face300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_features2d300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_flann300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_fuzzy300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_hal300.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_highgui300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_imgcodecs300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_imgproc300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_ml300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_objdetect300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_photo300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_plot300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_reg300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_rgbd300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_saliency300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_shape300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_stereo300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_stitching300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_structured_light300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_superres300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_surface_matching300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_text300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_ts300.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_video300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_videoio300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_videostab300.dll.a C:\opencv\min_bin\install\x64\mingw\lib\libopencv_xobjdetect300.dll.a
obj\Debug\t1.o: In function main':
E:/main_proj/main_project/t1.cpp:79: undefined reference tocv::Tracker::create(cv::String const&)'
E:/main_proj/main_project/t1.cpp:116: undefined reference to cv::Tracker::init(cv::Mat const&, cv::Rect_<double> const&)'
E:/main_proj/main_project/t1.cpp:126: undefined reference tocv::Tracker::update(cv::Mat const&, cv::Rect_&)'
I suppose it is some problem that arose during make of OpenCV. Can Somebody suggest a solution?

When linking with -L (path to directory with library) option like
-LC:\opencv\min_bin\install\x64\mingw\lib
you should use -l as well to link to specific library.
it seems that -lopencv_tracking300 library is not found.

I rebuilt the entire set of modules and now it works. I am still not sure what exactly went missing earlier. You need to experiment quite a few times before you can arrive at a successful build. Thanks a lot for the suggestions- #berak and #John

Related

Eye Blinking Detection

Some warnings appear in terminal during running:
OpenCV Error: Assertion failed(s>=0) in setSize, file /home/me/opencv2.4/modules/core/src/matrix.cpp, line 116
The program compiled without error and executes, the problem is the eye ROI size changes when user moves closer/farther away from webcam, due to the changing of size, the warning appears. I managed to solve these warnings by setting the eye ROI size equal to my eye template size. However, it ends up the program fails to classify user's eyes open/close because the minVal obtained is 0. The method used is OpenCV Template Matching. Alternatively, I fix my distance from webcam and fix the eye template size could avoid the warning. Every time warning appears, the program fails to classify open/close eyes. The program doesn't work effectively because sometimes it mistakenly classifies the open eyes as closed and vice versa.
Questions:
Is there any alternative to identify open and close eyes other than template matching?
Any ideas how to improve the program in classification of blinking?
Any working example that you know in opencv C/C++ API can classify open and close eyes and count accurately the blinking times?
static CvMemStorage* storage = 0;
// Create a new Haar classifier
static CvHaarClassifierCascade* cascade = 0;
// Function prototype for detecting and drawing an object from an image
bool detect_and_draw( IplImage* image ,CvHaarClassifierCascade* cascade);
const char *cascade_name[1]={"eyes.xml"};
cv::Mat roiImg;
int threshold_value = 200;
int threshold_type = 3;;
int const max_value = 255;
int const max_type = 4;
int const max_BINARY_value = 255;
int hough_thr = 35;
cv::Mat src_gray, dst;
using namespace cv;
Mat img1; Mat img2; Mat templ; Mat result;
const char* image_window = "Source Image";
const char* result_window = "Result window";
int match_method=0;
int max_Trackbar = 5;
int eye_open=0;
int eye_close=0;
//Matching with 2 images ,eye closed or open
void MatchingMethod(cv::Mat templ,int id )
{
/// Source image to display
cv::Mat img_display;
roiImg.copyTo( img_display );
/// Create the result matrix
int result_cols = roiImg.cols - templ.cols + 1;
int result_rows = roiImg.rows - templ.rows + 1;
result.create( result_cols, result_rows, CV_32FC1 );
/// Do the Matching and Normalize
cv::matchTemplate( roiImg, templ, result, match_method );
cv::normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
/// Localizing the best match with minMaxLoc
double minVal; double maxVal; Point minLoc; Point maxLoc;
cv::Point matchLoc;
cv::minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
///Justing checkin the match template value reaching the threashold
if(id == 0 && (minVal < 0))
{
eye_open=eye_open+1;
if(eye_open == 1)
{
std::cout<<"Eye Open"<<std::endl;
eye_open=0;
eye_close=0;
}
}
else if(id == 1 && (minVal < 0))
eye_close=eye_close+1;
if(eye_close == 1)
{
std::cout<<"Eye Closed"<<std::endl;
eye_close=0;
system("python send_arduino.py");
}
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if( match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )
{ matchLoc = minLoc; }
else
{ matchLoc = maxLoc; }
/// Show me what you got
cv::rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
cv::rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
cv::imshow( image_window, img_display );
cv::imshow( result_window, result );
return;
}
void detect_blink(cv::Mat roi)
{
try
{
MatchingMethod(img1,0);
MatchingMethod(img2,1);
}
catch( cv::Exception& e )
{
std::cout<<"An exception occued"<<std::endl;
}
}
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{
if(argc <= 1)
{
std::cout<<"\n Help "<<std::endl;
std::cout<<"\n ------------------------------------\n"<<std::endl;
std::cout<<"./blink_detect open_eye.jpg close_eye.jpg\n"<<std::endl;
std::cout<<"Eg :: ./blink_detect 2.jpg 3.jpg\n"<<std::endl;
std::cout<<"\n ------------------------------------\n"<<std::endl;
exit(0);
}
// Structure for getting video from camera or avi
CvCapture* capture = 0;
// Images to capture the frame from video or camera or from file
IplImage *frame, *frame_copy = 0;
// Used for calculations
int optlen = strlen("--cascade=");
// Input file name for avi or image file.
const char* input_name;
img1 = imread( argv[1], 1 );
img2 = imread( argv[2], 1 );
// Load the HaarClassifierCascade
/// Create windows
cv::namedWindow( image_window, CV_WINDOW_AUTOSIZE );
cv::namedWindow( result_window, CV_WINDOW_AUTOSIZE );
// Allocate the memory storage
storage = cvCreateMemStorage(0);
capture = cvCaptureFromCAM( 0);
// Create a new named window with title: result
cvNamedWindow( "original_frame", 1 );
// If loaded succesfully, then:
if( capture )
{
// Capture from the camera.
for(;;)
{
// Capture the frame and load it in IplImage
if( !cvGrabFrame( capture ))
break;
frame = cvRetrieveFrame( capture );
// If the frame does not exist, quit the loop
if( !frame )
break;
// Allocate framecopy as the same size of the frame
if( !frame_copy )
frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
IPL_DEPTH_8U, frame->nChannels );
// Check the origin of image. If top left, copy the image frame to frame_copy.
if( frame->origin == IPL_ORIGIN_TL )
cvCopy( frame, frame_copy, 0 );
// Else flip and copy the image
for(int i=0;i<1;i++)
{
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name[i], 0, 0, 0 );
// Check whether the cascade has loaded successfully. Else report and error and quit
if( !cascade )
{
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
return -1;
}
// Call the function to detect and draw the face
if(detect_and_draw(frame_copy,cascade))
{
std::cout<<"Detected"<<std::endl;
}
}
// Wait for a while before proceeding to the next frame
if( cvWaitKey( 1 ) >= 0 )
break;
}
// Release the images, and capture memory
cvReleaseHaarClassifierCascade(&cascade);
cvReleaseImage( &frame_copy );
cvReleaseCapture( &capture );
cvReleaseMemStorage(&storage);
}
return 0;
}
// Function to detect and draw any faces that is present in an image
bool detect_and_draw( IplImage* img,CvHaarClassifierCascade* cascade )
{
int scale = 1;
// Create a new image based on the input image
IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
// Create two points to represent the face locations
CvPoint pt1, pt2;
int i;
// Clear the memory storage which was used before
cvClearMemStorage( storage );
// Find whether the cascade is loaded, to find the faces. If yes, then:
if( cascade )
{
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
1.1, 8, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
// Loop the number of faces found.
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
// Create a new rectangle for drawing the face
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
// Draw the rectangle in the input image
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
cv::Mat image(img);
cv::Rect rect;
rect = cv::Rect(pt1.x,pt1.y,(pt2.x-pt1.x),(pt2.y-pt1.y));
roiImg = image(rect);
cv::imshow("roi",roiImg);
///Send to arduino
detect_blink(roiImg);
}
}
cvShowImage( "original_frame", img );
if(i > 0)
return 1;
else
return 0;
cvReleaseImage( &temp );
}
Reference:
Website referred

Visual C++ express 2010 The procedure entry point ??1task_group_context#tbb##QAE#XZ could not be located in the dynamic link library tbb.dll

Can someone help me out with this error?
I tried researching on the internet and tried the different methods to resolve the problem (eg: uninstalling other versions of visual c++, adding codes, etc), but none of them seem to work :(
What I have done:
under c/c++-->general-->additional include directories-->C:\OpenCV2.3\build\include;C:\OpenCV2.3\build\include\opencv2;C:\OpenCV2.3\build\include\opencv;C:\OpenCV2.3\opencv\data\haarcascades
under linker-->general-->additional library directories-->C:\OpenCV2.3\build\x86\vc9\lib;C:\OpenCV2.3\opencv\data\haarcascades;%(AdditionalLibraryDirectories)
under linker-->input-->additional dependencies--> added opencv_core230.lib;opencv_highgui230.lib;opencv_objdetect230.lib
under debugging-->command arguments -->added --cascade="C:/Program Files/OpenCV/data/haarcascades/haarcascade_frontalface_alt.xml"testimg.jpg
The code:
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
// Create memory for calculations
static CvMemStorage* storage = 0;
// Create a new Haar classifier
static CvHaarClassifierCascade* cascade = 0;
// Function prototype for detecting and drawing an object from an image
void detect_and_draw( IplImage* image );
// Create a string that contains the cascade name
const char* cascade_name = "haarcascade_frontalface_alt.xml";
/* "haarcascade_profileface.xml";*/
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{
// Structure for getting video from camera or avi
CvCapture* capture = 0;
// Images to capture the frame from video or camera or from file
IplImage *frame, *frame_copy = 0;
/* IplImage* img = cvLoadImage( "testimg.jpg" );
cvNamedWindow( "MyJPG", CV_WINDOW_AUTOSIZE );
cvShowImage("MyJPG", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "MyJPG" );
*/
// Used for calculations
int optlen = strlen("--cascade=");
// Input file name for avi or image file.
const char* input_name;
// Check for the correct usage of the command line
if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
{
cascade_name = argv[1] + optlen;
input_name = argc > 2 ? argv[2] : 0;
}
else
{
fprintf( stderr,
"Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
return -1;
/*input_name = argc > 1 ? argv[1] : 0;*/
}
// Load the HaarClassifierCascade
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
// Check whether the cascade has loaded successfully. Else report and error and quit
if( !cascade )
{
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
return -1;
}
// Allocate the memory storage
storage = cvCreateMemStorage(0);
// Find whether to detect the object from file or from camera.
if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
else
capture = cvCaptureFromAVI( input_name );
// Create a new named window with title: result
cvNamedWindow( "result", 1 );
// Find if the capture is loaded successfully or not.
// If loaded succesfully, then:
if( capture )
{
// Capture from the camera.
for(;;)
{
// Capture the frame and load it in IplImage
if( !cvGrabFrame( capture ))
break;
frame = cvRetrieveFrame( capture );
// If the frame does not exist, quit the loop
if( !frame )
break;
// Allocate framecopy as the same size of the frame
if( !frame_copy )
frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
IPL_DEPTH_8U, frame->nChannels );
// Check the origin of image. If top left, copy the image frame to frame_copy.
if( frame->origin == IPL_ORIGIN_TL )
cvCopy( frame, frame_copy, 0 );
// Else flip and copy the image
else
cvFlip( frame, frame_copy, 0 );
// Call the function to detect and draw the face
detect_and_draw( frame_copy );
// Wait for a while before proceeding to the next frame
if( cvWaitKey( 10 ) >= 0 )
break;
}
// Release the images, and capture memory
cvReleaseImage( &frame_copy );
cvReleaseCapture( &capture );
}
// If the capture is not loaded succesfully, then:
else
{
// Assume the image to be lena.jpg, or the input_name specified
const char* filename = input_name ? input_name : (char*)"testimg.jpg";
// Load the image from that filename
IplImage* image = cvLoadImage( filename, 1 );
// If Image is loaded succesfully, then:
if( image )
{
// Detect and draw the face
detect_and_draw( image );
// Wait for user input
cvWaitKey(0);
// Release the image memory
cvReleaseImage( &image );
}
else
{
/* assume it is a text file containing the
list of the image filenames to be processed - one per line */
FILE* f = fopen( filename, "rt" );
if( f )
{
char buf[1000+1];
// Get the line from the file
while( fgets( buf, 1000, f ) )
{
// Remove the spaces if any, and clean up the name
int len = (int)strlen(buf);
while( len > 0 && isspace(buf[len-1]) )
len--;
buf[len] = '\0';
// Load the image from the filename present in the buffer
image = cvLoadImage( buf, 1 );
// If the image was loaded succesfully, then:
if( image )
{
// Detect and draw the face from the image
detect_and_draw( image );
// Wait for the user input, and release the memory
cvWaitKey(0);
cvReleaseImage( &image );
}
}
// Close the file
fclose(f);
}
}
}
// Destroy the window previously created with filename: "result"
cvDestroyWindow("result");
// return 0 to indicate successfull execution of the program
return 0;
}
// Function to detect and draw any faces that is present in an image
void detect_and_draw( IplImage* img )
{
int scale = 1;
// Create a new image based on the input image
IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
// Create two points to represent the face locations
CvPoint pt1, pt2;
int i;
// Clear the memory storage which was used before
cvClearMemStorage( storage );
// Find whether the cascade is loaded, to find the faces. If yes, then:
if( cascade )
{
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
// Loop the number of faces found.
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
// Create a new rectangle for drawing the face
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
// Draw the rectangle in the input image
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
}
}
// Show the image in the window named "result"
cvShowImage( "result", img );
// Release the temp image created.
cvReleaseImage( &temp );
}
The error message says, that a certain symbol could not be found in an external library, tbb.dll. The library (the dll) itself seems to be found.
The library is Threading building blocks and your error message hints to a mismatch between header and the used dll, for example because of multiple versions installed etc. You should find out, if the correct dll is used at runtime (check the modules window in VS, or use Dependency Walker for that).
To point the runtime application to the correct path of tbb.dll, you might put the directory in your PATH environment variable, or put the tbb.dll side by side to the Executable.

How to execute some shell script after opencv detect circle

How to execute some shell script (e.g 1.sh) after opencv detect circle?
I have used exec, it works but opencv program close after circle detected, and what I want is that the program didn't close until I press "q" key.
Here is my code:
#include<cv.h>
#include<highgui.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main( int argc, char **argv )
{
CvCapture *capture = 0;
IplImage *img = 0;
int key = 0;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN,1.0,1.0,0,1,CV_AA);
capture = cvCaptureFromCAM( 0 );
if ( !capture ) {
fprintf( stderr, "Cannot open initialize webcam!\n" );
return 1;
}
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
img = cvQueryFrame( capture );
if (!img)
exit(1);
IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
CvMemStorage* storage = cvCreateMemStorage(0);
while( key != 'q' ) {
img = cvQueryFrame( capture );
if( !img ) break;
cvCvtColor( img, gray, CV_BGR2GRAY );
cvSmooth( gray, gray, CV_GAUSSIAN, 5, 5 );
CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, >gray->height/40, 200, 100/*, 20, 100*/ );
int i;
for( i = 0; i < circles->total; i++ )
{
float* p = (float*)cvGetSeqElem( circles, i );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), >CV_RGB(50,255,30), 5, 8, 0 );
cvPutText(img, "CIRCLE",cvPoint(cvRound(p[0]+45),cvRound(p[1]+45)), &font, >CV_RGB(50,10,255));
if ( circles ) {
execl("./1.sh", (char *)0);
}
}
cvShowImage( "result", img );
cvShowImage("gray", gray);
key = cvWaitKey( 1 );
}
// cvReleaseMemStorage(storage);
// cvReleaseImage(gray);
cvDestroyAllWindows();
cvDestroyWindow( "result" );
cvReleaseCapture( &capture );
return 0;
}
I used codeblocks on ubuntu.
After exec*, none of the code (in that process) will be reached. You can fork, exec if you want the program to continue without waiting for the script to complete, otherwise add a wait. Alternatively, you could use system or popen.
Examples:
example function to fork a command and wait:
#include <unistd.h>
/*as a macro*/
#define FORK_EXEC_WAIT(a) ({int s,p;if((p=fork())==0) \
{execvp(a[0],a);}else{while(wait(&s)!= p);}})
/*as a function*/
void fork_exec_wait(char** a) {
int s,p;
if((p=fork())==0){
execvp(a[0],a);
}else{
while(wait(&s)!= p);
}
}
to fork a command and continue
#include <unistd.h>
/*as a macro*/
#define FORK_EXEC(a) ({if((fork())==0) execvp(a[0],a);})
/*as a function*/
void fork_exec(char** a) {
int s,p;
if((p=fork())==0)
execvp(a[0],a);
}
the system command is ~ fork-exec-wait of "sh -c command args"
#include <stdlib.h>
system("command args");
the popen command is similar without the sh -c and will give you the output as a stream (think pipes, fifo, etc)
#include <stdio.h>
FILE *fp;
fp = popen("command args", "r");
...
pclose(fp);

Segmentation fault on library call

I'm using a physics library called Chipmunk for a game that I'm writing.
In my initialize function I initialize the global variable cpSpace space. Then in update I call cpSpaceStep(space, timestep). The prototype for this function is void cpSpaceStep(cpSpace *space, cpFloat dt);. I'm getting a segfault on this function call. I've marked those two function calls in the code below.
The full code is below:
#include "../include/SDL/SDL_image.h"
#include "../include/SDL/SDL.h"
#include "../include/Player.h"
#include "../include/Timer.h"
#include "../include/Block.h"
#include "../include/ImageLoader.h"
#include "../include/chipmunk/chipmunk.h"
#include <string>
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 60;
SDL_Event event;
SDL_Surface *screen = NULL;
SDL_Surface *player_img = NULL, *block_img = NULL;
Player *player;
Timer fps;
cpSpace *space;
bool quit = false;
void initialize();
void update();
int main( int argc, char* argv[] )
{
initialize();
update();
return 1;
}
void initialize()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
}
//Set the window caption
SDL_WM_SetCaption( "Move the Dot", NULL );
cpVect gravity = cpv(0, 100);
//******************cpSpacenew()*****************
//This is where space is init'ed
space = cpSpaceNew();
//***********************************************
}
void update()
{
//While the user hasn't quit
while( quit == false )
{
//Start the frame timer
fps.start();
while( SDL_PollEvent( &event ) )
{
//Handle events for the dot
player->handle_input( &event );
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
player->update();
cpFloat timeStep = 1.0/FRAMES_PER_SECOND;
//************************Segfault**********************************************
cpSpaceStep(space, timeStep);
//******************************************************************************
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
}
Where's your cpInitChipmunk() call? Without that, it's likely that cpSpaceNew() may well return NULL (or junk).
Easy enough to check. Immediately after the call to cpSpaceNew(), insert a:
printf ("%p\n", space);
(or something equivalent, to see what the value is.
You may also want to do that immediatley before trying to use it as well, just in case something corrupts it.
Could be because space is NULL when you call cpSpaceStep(). And in cpSpaceStep() function, an attempt is done to dereference the pointer which is NULL. Check whether space is properly initialized in initialize().

OpenCV - getting the slider to update its position during video playback

I've picked up 'Learning OpenCV' and have been trying some of the code examples/exercises. In this code snippet, I want to get the slider to update its position with each video frame change, but for some reason it won't work (the picture freezes with the following code):
#include "cv.h"
#include "highgui.h"
int g_slider_position = 0;
CvCapture* g_capture = NULL;
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES, pos);
}
int main(int argc, char** argv)
{
cvNamedWindow("The Tom 'n Jerry Show", CV_WINDOW_AUTOSIZE);
g_capture = cvCreateFileCapture(argv[1]);
int frames = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if (frames != 0)
{
cvCreateTrackbar(
"Position",
"The Tom 'n Jerry Show",
&g_slider_position,
frames,
onTrackbarSlide
);
}
IplImage* frame;
while (1)
{
frame = cvQueryFrame(g_capture);
if (!frame)
break;
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
cvShowImage("The Tom 'n Jerry Show", frame);
char c = cvWaitKey(33);
if (c == 27)
break;
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("The Tom 'n Jerry Show");
return 0;
}
Any idea how to get the slider and video to work as intended?
This is the actual working code
// PROGRAM TO ADD A UPDATING TRACKBAR TO A VIDEO
#include <cv.h>
#include <highgui.h>
int g_slider_position = 0;
CvCapture* video_capture = NULL;
void onTrackbarSlide(int current_frame)
{
current_frame = g_slider_position;
cvSetCaptureProperty(video_capture,CV_CAP_PROP_POS_FRAMES,current_frame);
}
int main( int argc, char** argv )
{
cvNamedWindow( "Video", CV_WINDOW_AUTOSIZE );
video_capture = cvCreateFileCapture( "Crowdy.avi");
int no_of_frames = (int) cvGetCaptureProperty(video_capture,CV_CAP_PROP_FRAME_COUNT);
if( no_of_frames!= 0 )
{
cvCreateTrackbar("Slider","Video",&g_slider_position,no_of_frames,onTrackbarSlide);
}
IplImage* frame;
while(1)
{
frame = cvQueryFrame( video_capture );
if( !frame ) break;
cvShowImage( "Video", frame );
cvSetTrackbarPos("Slider","Video",++g_slider_position);
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &video_capture );
cvDestroyWindow( "Video" );
return(0);
}
You are incrementing g_slider_position twice in the code, so it will increment beyond its limit (set in cvCreateTrackbar as frames). This is likely causing your picture to freeze.
To fix, change this
g_slider_position++;
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
to
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
Accounting for the edited code, I would check that OpenCV is properly reading the number of frames from your file. Look at Learning OpenCV's Chapter 2, example 2.3 for a method of generically retrieving the number of frames from your AVI (if that is what you are using).
In your code above, if the number of frames is 0, the trackbar is not created but the code still enters a loop that attempts to update the trackbar position (if it finds a frame). I would use this instead:
if (frames != 0)
{
cvCreateTrackbar(
"Position",
"The Tom 'n Jerry Show",
&g_slider_position,
frames,
onTrackbarSlide
);
}
else
{
exit(1);
}
This seems a bit complicated to me. I used the cvGetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES) call to retrieve the current frame and used this to update the slider.
The callback function is then used just to change the position within g_capture.
So the call back is:
//Call back for slider bar
void onTrackbarSlide(int pos) {
cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES, pos);
}
And the loop is:
IplImage* frame; //Frame grabbed from video
while(1) {
frame = cvQueryFrame( g_capture );
if (!frame ) break;
cvShowImage( "Example2", frame );
g_frame_count = (int) cvGetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES);
cvSetTrackbarPos("Position","Example2", g_frame_count);
char c = cvWaitKey(33);
if ( c == 27 ) break;
}
Where the g_ variables are global.
You can try the solution below.
change this
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES, pos);
}
to
void onTrackbarSlide( int pos )
{
if( pos > g_slider_position + 1 )
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,
pos);
}
and also change this
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
to
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
g_slider_position + 1
);
Hi I have simlar code and I did the following:
void onTrackbarSlide(int pos)
{
if(pos > g_lastPosition+1 || pos < g_lastPosition)
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);
g_lastPosition = pos;
}
.............
while(1)
{
frame = cvQueryFrame( g_capture );
if( !frame ) break;
cvShowImage( "Example3", frame );
cvSetTrackbarPos("Position", "Example3", g_slider_position+1);
char c = cvWaitKey(33);
if( c == 27 ) break;
}
So you can grab the slide bar to any direction , I hope this can help
OK I finally solved this problem of updating the slider
and also if you want to move the slider the video will update
there is no problem of picture freezing now
#include "stdafx.h"
#include<cv.h>
#include<cxcore.h>
#include<highgui.h>
int g_slider_position = 0;
CvCapture* g_capture = NULL;
int count=0; //initiate a global counter
void onTrackbarSlide( int pos )
{// if you are moving the slider for more than two frames then this loop will initiate to
// to update the video
if(pos>count+2 || pos<count-2){
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,
pos);}
count=pos;
}
int main(int argc, _TCHAR* argv[])
{
//int count=0;
cvNamedWindow("Example3",CV_WINDOW_AUTOSIZE);
g_capture=cvCreateFileCapture("video.avi");
int frames = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if(frames!= 0) {
cvCreateTrackbar(
"Position",
"Example3",
&g_slider_position,
frames,
onTrackbarSlide
);
}
IplImage* frame;
while (1)
{
count++; // the counter will move along with the frame
frame = cvQueryFrame( g_capture );
if (!frame) break;
cvShowImage ("Example3", frame);
cvSetTrackbarPos("Position", "Example3", g_slider_position+1);
char c = cvWaitKey(33);
if(c==27) break;
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("Example3");
return 0;
}
ok now what i have done is that i have created a global counter which will be updated alongside with the frames
now when we use the slider with the mouse to a different position then in onTrackbarSlider routine the if loop will be initiated and it will set the video to the new position