#include <windows.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#define ON_CLICK 1
#define NOT_CLICK 0
int THICKNESS=2,mouseState;
CvMat *im;
CvPoint lastPoint;
CvScalar COLOR=cvScalar(0,0,0);
void drawPoint(CvMat *im,int x,int y,int radius,CvScalar color,int thickness,int line_type)
{
CvPoint center;
center.x=x;
center.y=y;
cvCircle(im,center,radius,color,thickness,line_type,0);
cvShowImage("image",im);
lastPoint.x=x;
lastPoint.y=y;
}
void drawLine(CvMat *im,int px,int py,int x,int y,CvScalar color,int thickness,int line_type)
{
CvPoint p;
p.x=x;
p.y=y;
cvLine(im,lastPoint,p,color,thickness,line_type,0);
cvShowImage("image",im);
lastPoint.x=x;
lastPoint.y=y;
}
void paint(int event,int x,int y,int flags,void *param)
{
if (event==CV_EVENT_LBUTTONDOWN)
{
mouseState=ON_CLICK;
drawPoint(im,x,y,THICKNESS,COLOR,-1,8);
}
else if (event==CV_EVENT_LBUTTONUP)
{
mouseState=NOT_CLICK;
drawLine(im,lastPoint.x,lastPoint.y,x,y,COLOR,THICKNESS,8);
}
else if (mouseState && event==CV_EVENT_MOUSEMOVE)
drawLine(im,lastPoint.x,lastPoint.y,x,y,COLOR,THICKNESS,8);
}
void clearScreen()
{
for (int i=0;i<im->height;i++)
for (int j=0;j<im->width;j++)
cvSet2D(im,i,j,cvScalar(255, 255, 255));
cvShowImage("image",im);
}
int main()
{
im=cvCreateMat(300,400,CV_32F);
clearScreen();
cvShowImage("image",im);
cvSetMouseCallback("image",paint,NULL);
int key=cvWaitKey(0);
while (key!=27)
{
switch(toupper(key))
{
case 'S':
cvSaveImage("myImage.png",im,0);
MessageBoxA(GetForegroundWindow(),"保存图片\"myImage.png\"成功!","保存图片",1);
break;
case 'C':
clearScreen();
break;
}
key=cvWaitKey(0);
}
cvDestroyAllWindows();
cvReleaseMat(&im);
return 0;
}
OpenCV 画图程序
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 如opencv开发前的准备工作中所说,此系列文章是在学习Practical Python and OpenCV(点...
- 本篇笔记主要记录Opencv里的画图功能,主要是下面几个API: cv2.line() 直线 cv2.rectan...