#include <cv.h>
#include <cvcam.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cvcam.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"highgui.lib")
CvRect g_rect; //全局变量
bool g_isdown;
void my_callback(int event, int x, int y, int flags, void* param); //声明鼠标事件的回调函数
void high_light(IplImage *image,CvRect rect); //将image图像的rect部分做加亮处理
void main()
{
IplImage *orginal_image=cvLoadImage("1.jpg");
assert(orginal_image!=NULL); //载入图像
IplImage *temp_image=cvCreateImage(cvGetSize(orginal_image),orginal_image->depth,orginal_image->nChannels);
assert(temp_image!=NULL); //创建临时图像
cvNamedWindow("Show"); //创建窗口并指定其回调函数
cvSetMouseCallback("Show",my_callback,(void*)temp_image);
while (1)
{
cvCopyImage(orginal_image,temp_image);
if (true==g_isdown)
{
high_light(temp_image,g_rect);
}
else
{
cvCopyImage(orginal_image,temp_image);
high_light(temp_image,g_rect);
}
cvShowImage("Show",temp_image);
if (27==cvWaitKey(30))
{
break;
}
}
}
void my_callback(int event, int x, int y, int flags, void* param)
{
IplImage *image=(IplImage*)(param);
switch (event)
{
case CV_EVENT_LBUTTONDOWN :{
g_isdown=true;
g_rect=cvRect(x,y,0,0);
}
break;
case CV_EVENT_MOUSEMOVE:{
if (true==g_isdown)
{
g_rect.width=x-g_rect.x;
g_rect.height=y-g_rect.y;
}
}
break;
case CV_EVENT_LBUTTONUP :{
g_isdown=false;
if (g_rect.width<0)
{
g_rect.x=g_rect.x+g_rect.width;
g_rect.width*=-1;
}
if (g_rect.height<0)
{
g_rect.y=g_rect.y+g_rect.height;
g_rect.height*=-1;
}
high_light(image,g_rect);
// printf("/n %d %d %d %d/n",g_rect.x,g_rect.y,g_rect.width,g_rect.height);
}
break;
}
}
//将图像进行加亮处理
void high_light(IplImage *image,CvRect rect)
{
assert(image!=NULL);
for (int row=rect.y;row<rect.y+rect.height;row++)
{
uchar *ptr=(uchar*)(image->imageData+row*image->widthStep);
for (int col=rect.x;col<rect.x+rect.width;col++)
{
ptr[col*3+1]=150;
}
}
}