2

我是新手

void save(int event,int x,int y,int flags,void* firstImage){
    IplImage* pic = (IplImage*)firstImage;
    if(event==CV_EVENT_LBUTTONDOWN){
        printf("%d--%d\n",x,y);
    }
    return( x, y);
}

void main(){
    IplImage* firstImage = cvLoadImage("first.jpg");    
    cvNamedWindow("First");
    cvSetMouseCallback("First",save,(void*)firstImage);
    cvShowImage("First",firstImage);
    CvRect pixel = cvRect(x,y,350,350);
    cvSetImageROI (first,pixel);
    cvWaitKey(0);
    cvReleaseImage(&firstImage);        
}

我如何获得坐标形式“保存函数”以在 ROI 中使用 set x,y

4

1 回答 1

0

您需要使用回调函数,因为您不知道鼠标单击事件何时会发生。有多种获取值的方法,最简单的一种是使用全局变量(如图所示)。更好但更难的方法是将指针作为参数传递给将由您的“保存”函数(未显示)而不是“void * firstImage”修改的cvPoint。

应该工作的最简单的解决方案是定义一个全局变量并轮询它直到它发生变化。让我建议这个:

int GlobalX = -1; //These two variables will hold new information
int GlobalY = -1;

void save(int event,int x,int y,int flags,void* firstImage){
    IplImage* pic = (IplImage*)firstImage;
    if(event==CV_EVENT_LBUTTONDOWN){
        printf("%d--%d\n",x,y);
        GlobalX = x; // Pass the coordinates to a scope visible to the main thread
        GlobalY = y;
    }
}

void main(){
    IplImage* firstImage = cvLoadImage("first.jpg");    
    cvNamedWindow("First");
    cvSetMouseCallback("First",save,(void*)firstImage);
    cvShowImage("First",firstImage);
    while (GlobalX == -1 && GlobalY == -1) cvWaitKey(100); // Wait until the user has clicked on the picture
    cvDestroyWindow("First"); // make sure that GlobalX and GlobalY will not be modified by the user anymore - important
    CvRect pixel = cvRect(GlobalX,GlobalY,350,350); // Use the coordinates.
    cvSetImageROI (first,pixel);
    cvWaitKey(0);
    cvReleaseImage(&firstImage);        
}
于 2013-06-27T14:31:19.003 回答