Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我想在窗口中保留一个填充的矩形,但失败了。请告诉我为什么。_C++_C_Opencv_Callback - Fatal编程技术网

C++ 我想在窗口中保留一个填充的矩形,但失败了。请告诉我为什么。

C++ 我想在窗口中保留一个填充的矩形,但失败了。请告诉我为什么。,c++,c,opencv,callback,C++,C,Opencv,Callback,代码可以构建并运行,但它不能像我希望的那样运行。我想用鼠标在窗口中画一个矩形。当按下按钮时,它开始绘图。释放按钮时,最后一个矩形应填充并保留在窗口中 我仔细检查了代码,我真的不知道为什么它不能以那种方式工作。请告诉我。 PS:我通过小小的修改成功地实现了功能。我只想知道这个程序的问题是什么。 #include "stdafx.h" #include "cv.h" #include "highgui.h" #include <iostream> using namespace std

代码可以构建并运行,但它不能像我希望的那样运行。我想用鼠标在窗口中画一个矩形。当按下按钮时,它开始绘图。释放按钮时,最后一个矩形应填充并保留在窗口中

我仔细检查了代码,我真的不知道为什么它不能以那种方式工作。请告诉我。 PS:我通过小小的修改成功地实现了功能。我只想知道这个程序的问题是什么。

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <iostream>

using namespace std;

CvRect rect;
bool g_press = false;

void my_mouse_callback(int event, int x, int y, int flags, void* param);

int main(){
 IplImage *img = cvCreateImage(cvSize(500, 500), 8, 3);
 cvZero(img);
 cvAddS(img, cvScalarAll(255), img);
 IplImage *temp = cvCloneImage(img);
 cvNamedWindow("FUN");
 cvSetMouseCallback("FUN", my_mouse_callback, (void*) temp);
 cout<<1<<endl;

//if the mouse is pressed, use the statement in while to draw. If the mouse is released, use the callback to draw.
//
 while(1){
 if(g_press) {
 temp = cvCloneImage(img);
 cvRectangle(temp, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), CV_RGB(50,50,50));
 }
 cvShowImage("FUN", temp);
 if(cvWaitKey(15) == 27) break;
 }
 cvReleaseImage(&img);
 cvDestroyWindow("FUN");
}

//callback function for mouse

void my_mouse_callback(int event, int x, int y, int flags, void* param){
 IplImage* img = (IplImage*) param;
 switch(event){
 case CV_EVENT_MOUSEMOVE:
 {
 if(g_press == true){
 rect.width = x - rect.x;
 rect.height = y - rect.y;
 }
 }break;
 case CV_EVENT_LBUTTONDOWN:
 {
 g_press = true;
 rect.x = x;
 rect.y = y;
 rect.height = rect.width = 0;
 }break;
 case CV_EVENT_LBUTTONUP:
 {
 g_press = false;
 if(rect.width < 0){
 rect.x += rect.width;
 rect.width *= -1;
 }
 if(rect.height <0){
 rect.y += rect.height;
 rect.height *= -1;
 }

////By this statement, I want to fill the final rectangle and hold it in the window. I examined it for several times and I did not find mistakes. I just want to know the reason.
 cvRectangle(img, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), CV_RGB(50,50,50), CV_FILLED);
 }break;
 }
}
#包括“stdafx.h”
#包括“cv.h”
#包括“highgui.h”
#包括
使用名称空间std;
CvRect-rect;
bool g_press=false;
void my_mouse_回调(int事件、int x、int y、int标志、void*param);
int main(){
IplImage*img=cvCreateImage(cvSize(500500),8,3);
cvZero(img);
cvAddS(img,cvScalarAll(255),img);
IplImage*temp=cvCloneImage(img);
cvNamedWindow(“乐趣”);
cvSetMouseCallback(“乐趣”,我的鼠标回调,(void*)temp);

cout以下是我在Mac OS X上观察到的情况:绘制矩形后,它一直可见,直到我再次在窗口内用鼠标单击为止。原因是:

if(g_press) {
    temp = cvCloneImage(img);
    cvRectangle(temp, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), CV_RGB(50,50,50));
 }
每次单击时,您都会克隆原始图像(清晰/空),这会导致您松开绘制的矩形

如果这是你所期望的,它在我的系统上工作

编辑:

我对您的代码进行了更全面的分析,问题很明显:您在
my_mouse\u callback()
中所做的任何绘图都会被我前面提到的
if
撤消

如果您密切关注,您会注意到应用程序的执行是这样的:
my\u mouse\u callback()
在出现鼠标事件时被调用,然后它会更改
g\u press
的值,用正确的参数配置
rect
结构,最后在图像上绘制一个矩形

紧接着,
while
中的
if
条件被触发,
temp=cvCloneImage(img);
覆盖回调:OOPSIE!生成的图形,然后绘制另一个未设置为CV_填充的矩形。换句话说,您在窗口上看到的矩形是由以下原因造成的:

if(g_press) {
    temp = cvCloneImage(img);
    cvRectangle(temp, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), CV_RGB(50,50,50));
}
而不是回调!因此,总结一下明显性,如果您想填充矩形,只需将最后一次调用更改为:

cvRectangle(temp, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), CV_RGB(50,50,50), CV_FILLED);

请缩进你的代码。从标题中删除很多thx。谢谢你的回复。它按照你说的那样工作。但这不是我想要的。我想要的是,当我释放按钮时,最后一个矩形被填充。我使用回调中最后几行的代码来实现它,它似乎不起作用。然后我测试它。它确实起作用,但不起作用矩形没有被填充。所以我很困惑。更新了我的答案。当你检查完所有答案后,请点击帮助你解决问题的答案旁边的复选框,选择它作为你问题的正式答案。这样你也在帮助别人。祝你好运。谢谢你的详细分析。但是,在按钮释放回调中,我首先将全局变量g_press设置为false。因此,if中的语句无法操作。因此,我认为在按钮释放的情况下,不会撤消对回调中temp的操作。此外,回调函数中的img变量是一个局部变量。但它代表esents for temp,我将其传递给回调。感谢您的仔细分析。如果您能帮助我,我将不胜感激。我已经这样做了。请花时间测试我向您提出的建议,您将看到它是有效的。更进一步,并注释掉
cvRectangle()
从回调调用。我的分析可能不准确,但我的测试将告诉您实际发生了什么,以及如何解决问题。