Ask Your Question
-1

I am new to opencv. I tried my first code but certain error is hindering my procession to the completion of my project..

asked 2014-07-08 05:12:26 -0600

Bushra gravatar image

updated 2014-07-08 07:12:43 -0600

unxnut gravatar image
#include <iostream>

#include<opencv/cvaux.h>
#include<opencv/highgui.h>
#include<opencv/cxcore.h>

#include<stdio.h>
#include<stdlib.h>

// Need to include this for serial port communication
#include <Windows.h>

///////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
// Setup serial port connection and needed variables.
HANDLE hSerial = CreateFile(L"COM3", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

if (hSerial !=INVALID_HANDLE_VALUE)
{
    printf("Port opened! \n");

    DCB dcbSerialParams;
    GetCommState(hSerial,&dcbSerialParams);

    dcbSerialParams.BaudRate = CBR_9600;
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.Parity = NOPARITY;
    dcbSerialParams.StopBits = ONESTOPBIT;

    SetCommState(hSerial, &dcbSerialParams);
}
else
{
    if (GetLastError() == ERROR_FILE_NOT_FOUND)
    {
        printf("Serial port doesn't exist! \n");
    }

    printf("Error while setting up serial port! \n");
}

char outputChars[] = "c";
DWORD btsIO;

// Setup OpenCV variables and structures
CvSize size640x480 = cvSize(640, 480);          // use a 640 x 480 size for all windows, also make sure your webcam is set to 640x480 !!

CvCapture* p_capWebcam;                     // we will assign our web cam video stream to this later . . .

IplImage* p_imgOriginal;            // pointer to an image structure, this will be the input image from webcam
IplImage* p_imgProcessed;           // pointer to an image structure, this will be the processed image
IplImage* p_imgHSV;                 // pointer to an image structure, this will hold the image after the color has been changed from RGB to HSV
                                    // IPL is short for Intel Image Processing Library, this is the structure used in OpenCV 1.x to work with images

CvMemStorage* p_strStorage;         // necessary storage variable to pass into cvHoughCircles()

CvSeq* p_seqCircles;                // pointer to an OpenCV sequence, will be returned by cvHough Circles() and will contain all circles
                                    // call cvGetSeqElem(p_seqCircles, i) will return a 3 element array of the ith circle (see next variable)

float* p_fltXYRadius;               // pointer to a 3 element array of floats
                                    // [0] => x position of detected object
                                    // [1] => y position of detected object
                                    // [2] => radius of detected object

int i;                              // loop counter
char charCheckForEscKey;            // char for checking key press (Esc exits program)

p_capWebcam = cvCaptureFromCAM(0);  // 0 => use 1st webcam, may have to change to a different number if you have multiple cameras

if(p_capWebcam == NULL) {           // if capture was not successful . . .
    printf("error: capture is NULL \n");    // error message to standard out . . .
    getchar();                              // getchar() to pause for user see message . . .
    return(-1);                             // exit program
}

                                                    // declare 2 windows
cvNamedWindow("Original", CV_WINDOW_AUTOSIZE);      // original image from webcam
cvNamedWindow("Processed", CV_WINDOW_AUTOSIZE);     // the processed image we will use for detecting circles

p_imgProcessed = cvCreateImage(size640x480,         // 640 x 480 pixels (CvSize struct from earlier)
                               IPL_DEPTH_8U,        // 8-bit color depth
                               1);                  // 1 channel (grayscale), if this was a color image, use 3

p_imgHSV = cvCreateImage(size640x480, IPL_DEPTH_8U, 3); 

// Variables for Arduino Control
int servoPosition = 90;
int servoOrientation = 0;

// Main program loop
while(1) {                              // for each frame . . .
    p_imgOriginal = cvQueryFrame(p_capWebcam);      // get frame from webcam

    if(p_imgOriginal == NULL) {                 // if frame was not captured successfully . . .
        printf("error: frame is NULL \n");      // error message to std out
        getchar();
        break;
    }

    // Change the color model from RGB (BGR) to HSV. This makes it easier to choose a color based on Hue
    cvCvtColor(p_imgOriginal, p_imgHSV, CV_BGR2HSV);

    cvInRangeS(p_imgHSV,                // function input
               cvScalar(38,  60,  70),          // min filtering value (if color is greater than or equal to this ...
(more)
edit retag flag offensive close merge delete

Comments

1

that code looks horribly outdated. they changed to a c++ api in2010 already.

could you try a more modern, much simpler sample, like this first ?

berak gravatar imageberak ( 2014-07-08 05:21:35 -0600 )edit

quote: "also make sure your webcam is set to 640x480 !!" if that is your problem, again, the advice is to leave that old c-api code alone (you don't have to prealloc, and thus know the size of the grayscale image with the c++ api before)

berak gravatar imageberak ( 2014-07-08 05:37:02 -0600 )edit

sir, actually i m required to do in c-api , as per the demand of my project...and my webcam is set to 640x480.. please suggest me some solution with this..

Bushra gravatar imageBushra ( 2014-07-08 06:17:21 -0600 )edit

Bushra, you are not allowed to do it in c. end of discussion.

berak gravatar imageberak ( 2014-07-08 07:14:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-07-08 06:11:54 -0600

Hi there,

You are using threshold function "cvInRangeS" for an RGB image.It is actually apllied on HSV image. Convert your image using cvCvtColor(p_imgOriginal, p_imgOriginal, CV_BGR2HSV);

Then learn how you can extract the specified color region using the respective Hue Saturation Vales, This link could help you:Click here

Hope this helps you :)

edit flag offensive delete link more

Comments

I have updated my question..now please have a look at the main code.. the images are converted to hsv and then the cvCvtColor has been applied..inspite of this the same error exists..

please help!!!

Bushra gravatar imageBushra ( 2014-07-08 06:25:33 -0600 )edit

I am making a robot with arduino ..for object tracking purpose

Bushra gravatar imageBushra ( 2014-07-08 06:27:10 -0600 )edit

Post the error here

Abhishek Kumar Annamraju gravatar imageAbhishek Kumar Annamraju ( 2014-07-08 07:56:08 -0600 )edit

Unhandled exception at at 0x75141D4D in Threshold test.exe: Microsoft C++ exception: cv::Exception at memory location 0x0088BE5C.

Bushra gravatar imageBushra ( 2014-07-08 11:49:16 -0600 )edit

Make sure you have included all the dll files in your project. Run a simple opencv code to access your webcam. If all this goes fine try debugging the code line by line so that you can find where it is going wrong. It may be due to some coding problem.

Abhishek Kumar Annamraju gravatar imageAbhishek Kumar Annamraju ( 2014-07-09 00:12:19 -0600 )edit

Thank you for replying to my queries. The above errors have been solved. Error was in the line "cvReleaseMemStorage(&p_strStorage); // deallocate necessary storage variable" Now I am not releasing it. But the object is not being detected, rather its detecting multiple objects( even if there are no objects).. It will be very kind of you , if u could help me with this..

Bushra gravatar imageBushra ( 2014-07-10 01:59:17 -0600 )edit

Question Tools

Stats

Asked: 2014-07-08 05:12:26 -0600

Seen: 399 times

Last updated: Jul 08 '14