#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 ...
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)
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..
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
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..
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.
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..
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 ?
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)
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, you are not allowed to do it in c. end of discussion.