Hi all,
I am trying to get the threshold image using opencv and to find the center of the object. My webcam works fine, but i get an error as 'select timeout'. Here is my code and output
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include<iostream>
using namespace cv;
using namespace std;
IplImage* GetThresholdedImage(IplImage* img);
void FollowBall(int x, int y, double area, int imgWidth);
int main()
{
CvCapture* capture = 0;
capture = cvCaptureFromCAM(0);
if(!capture)
{
printf("Could not intitialize capturing...\n");
return -1;
}
while(1)
{
IplImage* frame = 0;
frame = cvQueryFrame(capture);
if(!frame) break;
IplImage* imgYellowThresh = GetThresholdedImage(frame);
//Calculate Ball's position
CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
cvMoments(imgYellowThresh, moments, 1);
//actual moment values
double moment10 = cvGetSpatialMoment(moments, 1, 0);
double moment01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
//Hold Last Position and current position of ball
static int posX = 0;
static int posY = 0;
int lastX = posX;
int lastY = posY;
//actual new position
posX = moment10/area;
posY = moment01/area;
FollowBall(posX, posY, area, frame->width);
//wait for a keypress
int c = cvWaitKey(10);
if(c != -1)
break;
cvReleaseImage(&imgYellowThresh);
delete moments;
}
cvReleaseCapture(&capture);
return 0;
}
IplImage* GetThresholdedImage(IplImage* img)
{
//Convert the image into an HSV image
IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
cvCvtColor(img, imgHSV, CV_BGR2HSV);
IplImage* imgThresholded = cvCreateImage(cvGetSize(img), 8, 1);
cvInRangeS(imgHSV, cvScalar(102, 100, 40), cvScalar(180, 230, 240), imgThresholded);
IplImage* imgEroded = cvCreateImage(cvGetSize(img), 8, 1);
cvErode(imgThresholded, imgEroded, NULL, 1); //removes specks
cvReleaseImage(&imgHSV);
cvReleaseImage(&imgThresholded);
return imgEroded;
}
void FollowBall(int x, int y, double area, int imgWidth)
{
printf("Ball info: x=%d, y=%d, width=%d, area=%.2f\n", x, y,imgWidth, area);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
select timeout
returning eroded/nBall info: x=-2147483648, y=-2147483648, width=640, area=0.00
select timeout
returning eroded/nBall info: x=-2147483648, y=-2147483648, width=640, area=0.00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
any help is appreciated.
mhahn @