我从 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);
}