下面是代碼,
/*
創建白色圖像,通過鼠標左鍵繪圖,右鍵實現橡皮擦功能
*/
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
bool LFlag = false;
bool RFlag = false;
Point point ;
void onMouseCallback(int event, int x , int y , int flags, void *param)
{
IplImage *img = (IplImage *)param;
Point point1, point2;
switch (event)
{
//鼠標左鍵按下獲取當前位置,設置標記
case CV_EVENT_LBUTTONDOWN:
LFlag = true;
point = cvPoint(x, y);
break;
case CV_EVENT_LBUTTONUP:
LFlag = false;
break;
case CV_EVENT_RBUTTONDOWN:
RFlag = true;
point = Point(x, y);
cvCircle(img,point,5,CV_RGB(255,255,255),-1);
break;
case CV_EVENT_RBUTTONUP:
RFlag = false;
break;
case CV_EVENT_MOUSEMOVE:
point = Point(x, y);
break;
}
if (LFlag)
{
cvSet2D(img,point.y,point.x,CV_RGB(255,0,0));
}else if (RFlag)
{
cvCircle(img, point, 5, CV_RGB(255,255,255),-1);
}
}
int main()
{
IplImage *pImage = cvCreateImage(cvSize(500,500), 8, 3);
cvRectangle(pImage, cvPoint(0,0), cvPoint(500,500), CV_RGB(255,255,255),-1);
IplImage *temp = cvCloneImage(pImage);
namedWindow("簡易畫板");
cvSetMouseCallback("簡易畫板", onMouseCallback, (void *)pImage);
while (1)
{
cvCopy(pImage, temp);
cvShowImage("簡易畫板", temp);
if(waitKey(1)==27)
{
break;
}
}
return 0;
}
數組越界了,下面那個函數應該是畫出來了但是沒有顯示,具體函數實現得查查文檔