我从2008年开始在Mac笔记本电脑上运行OpenCV应用程序(拥有2.4 ghz intel core 2 duo处理器)。我在一个较新的Mac上运行相同的应用程序,它工作非常顺利,但更改为旧的Mac,相机正在丢帧,运行得不是很好。有没有办法通过减少图像等来修复代码中的这个问题?我的应用程序阈值多个颜色并跟踪它们然后从一个位置绘制线到另一个位置。
IplImage* GetThresholdedImage1(IplImage* img, int color)
{
// Convert the image into an HSV image
IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
cvCvtColor(img, imgHSV, CV_BGR2HSV);
//yellow
// Create new image to hold thresholded image
IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);
if(color==1)
cvInRangeS(imgHSV, cvScalar(10, 100, 100), cvScalar(20, 255, 255), imgThreshed);
cvReleaseImage(&imgHSV);
return imgThreshed;
}
int main(int argc,const char * argv []) {
CvCapture*capture=0;
capture=cvCaptureFromCAM( 1);
if(!capture)
{
printf("Could not initialize capturing...\n");
return -1;
}
cvNamedWindow("video");
IplImage* imgScribble = NULL;
while(true)
{ IplImage *frame=0;
frame=cvQueryFrame(capture);
if( !frame ) break;
cvErode(frame, frame, 0, 2); // ADD this line
cvSmooth( frame, frame, CV_GAUSSIAN, 9, 9 );
// If this is the first frame, we need to initialize it
if(imgScribble == NULL)
{
imgScribble = cvCreateImage(cvGetSize(frame),8, 3);
}
IplImage* imgYellowThresh1 = GetThresholdedImage1(frame,1);
CvMoments *moments_yellow = (CvMoments*)malloc(sizeof(CvMoments));
cvMoments(imgYellowThresh1,moments_yellow, 1);
double moment10 = cvGetSpatialMoment(moments_yellow, 1, 0);
double moment01 = cvGetSpatialMoment(moments_yellow, 0, 1);
double area = cvGetCentralMoment(moments_yellow, 0, 0);
static int posX = 0;
static int posY = 0;
int lastX = posX;
int lastY = posY;
posX = moment10/area;
posY = moment01/area;
if(lastX>0 && lastY>0 && posX>0 && posY>0)
{
thickness=cvRandInt(&rng)%8;
cvLine(imgScribble, cvPoint(posX, posY), cvPoint(lastX, lastY), CV_RGB(255,100,0),thickness,CV_AA);
}