学习OpenCV课后题4.3
2010-07-13 17:00
316 查看
#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;
}
}
}
#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;
}
}
}
相关文章推荐
- 学习OpenCV课后题3.3
- 学习OpenCV课后题4.2
- 学习opencv 课后8.5
- 学习OpenCV 第四章课后题第一题(合并图像、缩放、字体)
- 读取视频文件存盘(学习openCV课后题2.3)
- 学习OpenCV课后题3.8(b)
- 基于QT5的opencv学习之《学习opencv课后练习3-7》
- 读取摄像头并存盘(学习openCV课后题2.3改进)
- 学习OpenCV课后题3.4
- 学习OpenCV课后题4.4
- 学习opencv第二章课后题4,5
- 读取摄像头数据(学习openCV课后题 2.4)
- 学习OpenCV课后题3.6
- 学习OpenCV课后题4.5(有点糙)
- 学习opencv课后答案第三章
- 根据滚动条位置缩放图像(学习OpenCV课后题 2.5)
- 学习OpenCV课后题3.7
- 学习OpenCV课后题4.7
- 学习Opencv第3章课后习题
- 学习OpenCV第三章课后题(打卡)