Ask Your Question
0

newbie: lookin for process realtime camera capture image processing

asked 2014-11-10 04:35:23 -0600

DGambhir gravatar image

updated 2014-11-10 05:37:21 -0600

thdrksdfthmn gravatar image

Plz let me know the error in this code: Its not working when I capture image from camera but works when a still image from disk is taken

#include<stdio.h>
#include<cv.h>
#include<highgui.h>
//Defining Method
IplImage* doCanny(IplImage*,double,double,double);
IplImage* doPyrDown(IplImage*,int);
int main(){

// open next line if image is from the system
// IplImage* img=cvLoadImage("lena512color.tiff",0); // 1 for color 0 for gray scale
// open next 2 lines of code if image is live webcam
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);  // 0 is i.d of camera connected to your system
IplImage* img = cvQueryFrame(capture); //Create image from capture
cvNamedWindow("In_image",CV_WINDOW_AUTOSIZE); // same as figure command in matlab
cvNamedWindow("Smooth_out_image",CV_WINDOW_AUTOSIZE);
cvNamedWindow("CannyEdge_in_image",CV_WINDOW_AUTOSIZE);
cvNamedWindow("CannyEdge_out_image",CV_WINDOW_AUTOSIZE);
//create a window to show input image
cvShowImage("In_image",img);
//create a image to hold the output image
IplImage* out = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
//Do smoothing
cvSmooth(img,out,CV_GAUSSIAN,11,11);
//show the smoothed output image
cvShowImage("Smooth_out_image",out);
// Do Edge Detection
out = doCanny(out,10,100,3);
//Display
cvShowImage("CannyEdge_out_image",out);
// Do Edge Detection
out = doCanny(img,10,100,3);
// Display
cvShowImage("CannyEdge_in_image",out);
// Downsample Image
out = doPyrDown(img,IPL_GAUSSIAN_5x5);
cvShowImage("Pyramid_Downsampled_by_2",out);
//Release allocated memory
cvReleaseCapture(&capture); //Release capture
cvReleaseImage(&out);
//clean up windows
cvWaitKey(0);
cvDestroyAllWindows();
}
//Canny Edge Detection Method
IplImage* doCanny(IplImage* in,double lowThresh,double highThresh,double aperture){
    if(in->nChannels!=1)
        return(0); // for gray scale image only
    IplImage* out = cvCreateImage(CvSize(cvGetSize(in)),IPL_DEPTH_8U,1);
    cvCanny(in,out,lowThresh,highThresh,aperture);
    return(out);
}
// Pyramid image Down Method
IplImage* doPyrDown(IplImage* in,int filter=IPL_GAUSSIAN_5x5){
    //Make Sure image size divisible by 2
    assert(in->width%2 == 0 && in->height%2 == 0);
    IplImage* out = cvCreateImage(cvSize(in->width/2,in->height/2),in->depth,in->nChannels);
    cvPyrDown(in,out,filter);
    return(out);
}
edit retag flag offensive close merge delete

Comments

3

The c-code is outdated for several years now and should not be used anymore. Please check the c++-Version that uses cv::Mat.

FooBar gravatar imageFooBar ( 2014-11-10 05:34:33 -0600 )edit
1

Please avoid not-smart pointers and please avoid returning a pointer from a function, it very easily to get in undefined behaviour. You can also pass to C++.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-11-10 05:41:06 -0600 )edit
1

(I know this is not an answer helping with your problem, but:) I would strongly recommend to use the new C++ interface - especially when you are a newby! The new Interface does not have IplImage and cvXYZ, but cv::XYZ !

PhilLab gravatar imagePhilLab ( 2014-11-10 11:20:45 -0600 )edit

Where is the variable CV_CAP_ANY defined? This is the number of the webcam. It can range from 0 to... Try with a value of 0, if it doesn't work, try with 1, etc.

Doombot gravatar imageDoombot ( 2014-11-10 11:29:54 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-11-10 08:37:21 -0600

berak gravatar image

first of all, you should no more use the venerable c-api in 2014.

opencv switched to a c++ api in 2010 already, and so should you.

you will get rid of the serious memleaks in your code above, also it will also be only half of it to look at and maintain:

#include <iostream>
using namespace std;

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;

int main()
{
    VideoCapture cap(0); // get 1st cam
    if( ! cap.isOpened() )
    {
        cerr << "no capture." << endl;
        return -1;
    }
    Mat frame;
    if ( ! cap.read(frame) )
    {
        cerr << "no image." << endl;
        return -1;
    }
    imshow("In_image",frame);

    // Do smoothing
    Mat blurred; // leave empty, the api will take care of the allocation.
    blur(frame, blurred, Size(11,11));
    imshow("Smooth_out_image",blurred);

    // pyramid
    Mat pyr;
    pyrDown(frame,pyr);
    imshow("Pyramid downsampled",pyr);

    // Do Edge Detection
    Mat gray, can;
    cvtColor(frame, gray, CV_BGR2GRAY);
    Canny(gray, can, 10, 100, 3);
    imshow("CannyEdge_out_image", can);

    waitKey();
    return 0;
}
edit flag offensive delete link more

Comments

Thanks Berak, I understood the problem now Do you let me know the book as beginner because the I following seems to be very old

DGambhir gravatar imageDGambhir ( 2014-11-10 23:59:45 -0600 )edit

Question Tools

Stats

Asked: 2014-11-10 04:35:23 -0600

Seen: 1,323 times

Last updated: Nov 10 '14