Ask Your Question
0

Execution exiting after running the code.

asked 2014-04-23 04:50:10 -0600

Vivek Goel gravatar image

updated 2014-04-23 06:28:53 -0600

I am try to run the code but msv execution is exiting and sometimes it shows breakpoint error. P.S. code is copied.

#include "stdafx.h"
#include "opencv/cvaux.h"
#include "opencv/highgui.h"
#include "opencv/cxcore.h"
#include <stdio.h>

int main(int argc, char* argv[])
{
    CvCapture* camera = cvCreateCameraCapture(-1); // Use the default camera

    IplImage* frame = 0;
    CvMemStorage* storage = cvCreateMemStorage(0); //needed for Hough circles

    // capturing some extra frames seems to help stability
    frame = cvQueryFrame(camera);
    frame = cvQueryFrame(camera);
    frame = cvQueryFrame(camera);

    // with default driver, PSEye is 640 x 480
    CvSize size = cvSize(640, 480);
    IplImage *  hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3);
    IplImage*  thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1);
    IplImage*  thresholded2 = cvCreateImage(size, IPL_DEPTH_8U, 1);

    CvScalar hsv_min = cvScalar(0, 50, 170, 0);
    CvScalar hsv_max = cvScalar(10, 180, 256, 0);
    CvScalar hsv_min2 = cvScalar(170, 50, 170, 0);
    CvScalar hsv_max2 = cvScalar(256, 180, 256, 0);

    //do {
    frame = cvQueryFrame(camera);
    if (frame != NULL) {
        printf("got frame\n\r");
        // color detection using HSV
        cvCvtColor(frame, hsv_frame, CV_BGR2HSV);
        // to handle color wrap-around, two halves are detected and combined
        cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded);
        cvInRangeS(hsv_frame, hsv_min2, hsv_max2, thresholded2);
        cvOr(thresholded, thresholded2, thresholded);

        cvSaveImage("thresholded.jpg", thresholded);

        // hough detector works better with some smoothing of the image
        cvSmooth(thresholded, thresholded, CV_GAUSSIAN, 9, 9);
        CvSeq* circles = cvHoughCircles(thresholded, storage, CV_HOUGH_GRADIENT, 2, thresholded->height / 4, 100, 40, 20, 200);

        for (int i = 0; i < circles->total; i++)
        {
            float* p = (float*)cvGetSeqElem(circles, i);
            printf("Ball! x=%f y=%f r=%f\n\r", p[0], p[1], p[2]);
            cvCircle(frame, cvPoint(cvRound(p[0]), cvRound(p[1])),
                3, CV_RGB(0, 255, 0), -1, 8, 0);
            cvCircle(frame, cvPoint(cvRound(p[0]), cvRound(p[1])),
                cvRound(p[2]), CV_RGB(255, 0, 0), 3, 8, 0);
        }

        cvSaveImage("frame.jpg", frame);
    }
    else {
        printf("Null frame\n\r");
    }
    //} while (true);
    cvReleaseCapture(&camera);
    return 0;
}

and errors are..

The thread 0x1cbc has exited with code 0 (0x0).
The thread 0xe84 has exited with code 0 (0x0).
The thread 0x1ec4 has exited with code 0 (0x0).
The thread 0x1590 has exited with code 0 (0x0).
The thread 0x11f8 has exited with code 0 (0x0).
The thread 0x1e04 has exited with code 0 (0x0).
The thread 0x1404 has exited with code 0 (0x0).
The program '[32] Myhello2.exe' has exited with code 0 (0x0).
edit retag flag offensive close merge delete

Comments

4

People should learn to read their own code ... it returns code 0. This is not an error but an identification you added for telling the code ran to the end. There is literally a

  return 0;

command at the end of your code. The number is just the thread identification on startup of all your executions.

Also please quit using the old C-api, and switch to the newer C++-interface. Again an example of simple copy paste problems ...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-23 06:12:38 -0600 )edit

I find it a good idea to enclose the code in a try...catch block and catch Exception&amp; e. That can save you some headaches in the long run.

unxnut gravatar imageunxnut ( 2014-04-23 06:51:30 -0600 )edit

help me, how to fix it. it will be very thankfull.

Vivek Goel gravatar imageVivek Goel ( 2014-04-24 04:55:52 -0600 )edit
1

There is NO error ... your code works perfectly...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-24 04:57:52 -0600 )edit

but it is exiting in my pc. without opening windows and all.

Vivek Goel gravatar imageVivek Goel ( 2014-04-24 05:00:53 -0600 )edit
1

That is quite simple, you are just savinf images, not showing them ... Do you see the output of the printf function in your console? Did you try executing it from console and not from your development area?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-24 05:08:47 -0600 )edit

its showing only a console for only 5 secs and exits.

Vivek Goel gravatar imageVivek Goel ( 2014-04-24 05:12:05 -0600 )edit
1

well that is because you run from your IDE. Please add the following line before the last return 0: system("pause");

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-24 05:15:14 -0600 )edit

now in console it's showing 'Null frame press any key to continue...' and than exits

Vivek Goel gravatar imageVivek Goel ( 2014-04-24 05:18:52 -0600 )edit
1

Seriously you should really read your code... Null frame means that your frame = cvQueryFrame(camera); did not work...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-24 05:22:51 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2014-04-25 04:57:47 -0600

updated 2014-05-02 02:28:25 -0600

This should be the equivalent C++ variant

#include "opencv2/opencv.hpp"

#include <stdio.h>

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
    VideoCapture camera (0); // Use the default camera
    Mat frame;

    // capturing some extra frames seems to help stability
    // do this for stability --> give number of iterations, depends on camera
    int n = 3;
    for(int i = 0; i < n; i++){
        camera >> frame;
    }

    // with default driver, PSEye is 640 x 480
    Mat hsv_frame (Size(640, 480), CV_8UC3);
    Mat thresholded (Size(640, 480), CV_8UC1);
    Mat thresholded2 (Size(640, 480), CV_8UC1);

    Scalar hsv_min (0, 50, 170); Scalar hsv_min2 (170, 50, 170);
    Scalar hsv_max (10, 180, 256); Scalar hsv_max2 (256, 180, 256);

    camera >> frame;
    if( !frame.empty() ){
        // color detection using HSV
        cvtColor(frame, hsv_frame, CV_BGR2HSV);
        // to handle color wrap-around, two halves are detected and combined
        inRange(hsv_frame, hsv_min, hsv_max, thresholded);
        inRange(hsv_frame, hsv_min2, hsv_max2, thresholded2);
        bitwise_or(thresholded, thresholded2, thresholded);

        imwrite("PATH/thresholded.jpg", thresholded);

        // hough detector works better with some smoothing of the image
        GaussianBlur(thresholded, thresholded, Size(9,9), 0, 0);
        vector<Vec3f> circles;
        HoughCircles(thresholded, circles, CV_HOUGH_GRADIENT, 2, thresholded.rows / 4, 100, 40, 20, 200);

        for (int i = 0; i < circles.size(); i++)
        {
            printf("Ball! x=%f y=%f r=%f\n\r", circles[i][0], circles[i][1], circles[i][2]);
            circle(frame, Point((int)circles[i][0], (int)circles[i][1]), 3, CV_RGB(0, 255, 0), -1, 8, 0);
            circle(frame, Point((int)circles[i][0], (int)circles[i][1]), (int)circles[i][2], CV_RGB(255, 0, 0), 3, 8, 0);
        }

        imwrite("PATH/frame.jpg", frame);
    }
    else {
        printf("No frame retrieved! \n");
    }

    return 0;
}
edit flag offensive delete link more

Comments

flood of errors..

Vivek Goel gravatar imageVivek Goel ( 2014-05-01 03:39:17 -0600 )edit

Please do suggest what is going wrong ... I literally translated to C++ code like you asked. However I did not check the performance and I do hope you changed stuff like PATH to your exact location...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-02 02:23:51 -0600 )edit

It seriously took me 2 minutes to remove the errors. You should be able yourself to do that in the future. Adding a int type declarement or a correct name replacement is not that hard. The above code now compiles and links perfectly on my system...

StevenPuttemans gravatar imageStevenPuttemans ( 2014-05-02 02:29:38 -0600 )edit
1

through your motivation i will definitely try it in future. i am getting some problem with my webcam, i will check it out soon and contact you later :)

Vivek Goel gravatar imageVivek Goel ( 2014-05-03 12:51:17 -0600 )edit

Question Tools

Stats

Asked: 2014-04-23 04:50:10 -0600

Seen: 1,209 times

Last updated: May 02 '14