data[i*step+j*channels+k]=255-data[i*step+j*channels+k];
//showtheimage
cvShowImage("ao",img);
//waitforakey
cvWaitKey(0);
//releasetheimage
cvReleaseImage(&img);
return0;
}
单张RGB图片变成灰度图并保存
#include"stdafx.h"
#include"cv.h"
#include"highgui.h"
intmain(intargc,char*argv[])
{
IplImage*image;
IplImage*result;
image=cvLoadImage("lena.jpg",-1);
//注|意指针变量定要先初始化才能使用,否则崩溃
//灰度转换时通道一定¬要设置正确
intchannel=1;//image->nChannels;
intdepth=image->depth;
CvSizesz;
sz.width=image->width;
sz.height=image->height;
result=cvCreateImage(sz,depth,channel);
cvCvtColor(image,result,CV_BGR2GRAY);
cvNamedWindow("original",1);
cvShowImage("original",image);
cvNamedWindow("gray",1);
cvShowImage("gray",result);
cvSaveImage("hlena.jpg",result);
cvWaitKey(0);
cvReleaseImage(&image);
cvReleaseImage(&result);
cvDestroyWindow("original");
cvDestroyWindow("gray");
return0;
}
例2-2显示视频
#include"stdafx.h"
#include
#include
#include
voidmain()
{
cvNamedWindow("example2",0);
CvCapture*capture=cvCreateFileCapture("晚ª¨ª秋?
电Ì?
影®¡ã<晚ª¨ª秋?
>同ª?
名?
主¡Â题¬a曲¨²--音°?
悦?
台¬¡..mp4");
IplImage*frame;
while
(1)
{
frame=cvQueryFrame(capture);
if(!
frame){break;}
cvShowImage("example2",frame);
charc=cvWaitKey(33);
if(c==27){break;}
}
cvReleaseCapture(&capture);
cvDestroyWindow("example2");
}
例2-3滚动条显示视频
#include"stdafx.h"
#include
#include
#include
intg_slider_position=0;
CvCapture*g_capture=NULL;
voidonTrackbarSlide(intpos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);
}
intmain(intargc,char**argv)
{
cvNamedWindow("Example3",CV_WINDOW_AUTOSIZE);
g_capture=cvCreateFileCapture("晚ª¨ª秋?
电Ì?
影®¡ã<晚ª¨ª秋?
>同ª?
名?
主¡Â题¬a曲¨²--音°?
悦?
台¬¡§.mp4");
intframes=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);
if(frames!
=0)
{
cvCreateTrackbar("Position","Example3",&g_slider_position,frames,onTrackbarSlide);
}
IplImage*frame;
while
(1)
{
frame=cvQueryFrame(g_capture);
if(!
frame){break;}
cvShowImage("example2",frame);
charc=cvWaitKey(33);
if(c==27){break;}
}//Whileloop(asinExample2)capture&showvideo…-
cvReleaseCapture(&g_capture);
cvDestroyWindow("example2");//Releasememoryanddestroywindow…-
return(0);
}
例2—4图像平滑处理
#include"stdafx.h"
#include"cv.h"
#include"highgui.h"
voidexample2_4(IplImage*image)
{
//Createsomewindowstoshowtheinput
//andoutputimagesin.
cvNamedWindow("Example4-in");
cvNamedWindow("Example4-out");
//Createawindowtoshowourinputimage
//
cvShowImage("Example4-in",image);
//Createanimagetoholdthesmoothedoutput
//cvGetSize(image)当Ì¡À前¡ã图ª?
像?
结¨¢构1的Ì?
大䨮小?
各¡Â通ª¡§道̨¤每?
个?
像?
素?
点Ì?
的Ì?
数ºy据Y类¤¨¤型¨ª,通ª¡§道̨¤的Ì?
总Á¨¹数ºy
IplImage*out=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,3);
//Dothesmoothing
//
cvSmooth(image,out,CV_GAUSSIAN,3,3);//CV_GAUSSIAN对?
图ª?
像?
进?
行D核?
大䨮小?
为aparam1ר¢param2的Ì?
高?
斯1卷¨ª积y。
¡ê
//Showthesmoothedimageintheoutputwindow
//
cvShowImage("Example4-out",out);
//Betidy
//
cvReleaseImage(&out);
//Waitfortheusertohitakey,thencleanupthewindows
//
cvWaitKey(0);
cvDestroyWindow("Example4-in");
cvDestroyWindow("Example4-out");
}
intmain(void)
{
IplImage*img=cvLoadImage("woheadai.jpg");
example2_4(img);
return1;
}
例2-5使用cvPyDown()使图片长宽各缩小一半
#include"stdafx.h"
#include"highgui.h"
#include"cv.h"
IplImage*doPyrDown(IplImage*in)//intfilter=IPL_GAUSSIAN_5X5
{
assert(in->width%2==0&&in->height%2==0);
IplImage*out=cvCreateImage(
cvSize(in->width/2,in->height/2),
in->depth,
in->nChannels
);
cvPyrDown(in,out,CV_GAUSSIAN_5x5);
cvNamedWindow("tu1",1);
cvShowImage("tu1",out);
cvWaitKey(0);
cvSaveImage("wheadai.jpg",out);
cvReleaseImage(&out);
cvDestroyWindow("tu1");
return(out);
};
intmain(intargc,char**argv)
{
IplImage*img=cvLoadImage("woheadai.jpg");
doPyrDown(img);
cvReleaseImage(&img);
return0;
}
例2-6canny边缘检测将输出写入一个单通道(灰度级)图像
#include"stdafx.h"
#include"highgui.h"
#include"cv.h"
IplImage*doCanny(IplImage*in,doublelowThresh,doublehighThresh,intaperture)
{
if(in->nChannels!
=1)
return(0);
IplImage*out=cvCreateImage(cvSize(in->width,in->height),IPL_DEPTH_8U,1);
cvCanny(in,out,lowThresh,highThresh,aperture);
cvNamedWindow("hb",1);
cvShowImage("hb",out);
cvWaitKey(0);
cvSaveImage("奥ã?
黛¨¬丽¤?
·¡è赫?
本À?
.jpg",out);
cvReleaseImage(&out);
cvDestroyWindow("hb");
return(out);
};
intmain(intargc,char**argv)
{
IplImage*img=cvLoadImage("奥ã?
黛¨¬丽¤?
·¡è赫?
本À?
(194).jpg",0);
doCanny(img,1,2.4,3);
cvReleaseImage(&img);
return0;
}
例2-7图像处理中两次缩放和canny边缘检测例2-8通过每个独立阶段释放内存来简化2-7中图像处理
#include"stdafx.h"
#include"highgui.h"
#include"cv.h"
IplImage*out;
IplImage*doPyrDown(IplImage*in)
{
assert(in->width%2==0&&in->height%2==0);
out=cvCreateImage(cvSize(in->width/2,in->height/2),in->depth,in->nChannels);
cvPyrDown(in,out,CV_GAUSSIAN_5x5);
return(out);
};
IplImage*doCanny(IplImage*in,doublelowThresh,doublehighThresh,intaperture)
{
if(in->nChannels!
=1)
return(0);
IplImage*out=cvCreateImage(cvSize(in->width,in->height),IPL_DEPTH_8U,1);
cvCanny(in,out,lowThresh,highThresh,aperture);
return(out);
};
intmain(intargc,char**argv)
{
IplImage*img=cvLoadImage("ha.jpg",0);
out=doPyrDown(img);
out=doPyrDown(out);
out=doCanny(out,1,3,3);
cvNamedWindow("kk",1);
cvShowImage("kk",out);
cvWaitKey(0);
cvSaveImage("haha.jpg",out);
cvReleaseImage(&out);
cvDestroyWindow("kk");
cvReleaseImage(&img);
return0;
}
例2-9capture结构初始化后,从摄像设备读入图像并保存(或者从视频无区别)
#include"stdafx.h"
#include"cv.h"
#include"highgui.h"
#include"conio.h"
intmain(){
CvCapture*capture=cvCaptureFromCAM(CV_CAP_ANY);
IplImage*image=NULL;//*dst=NULL;
image=cvQueryFrame(capture);
CvSizesize=cvGetSize(image);
//dst=cvCreateImage(size,image->depth,1);
doublefps=10;
CvVideoWriter*writer=cvCreateVideoWriter("VideofromCAMERA.avi",CV_FOURCC('X','V','I','D'),fps,size,1);
cvNamedWindow("ha",1);
while((image=cvQueryFrame(capture))!
=NULL)
{
printf("Enteredn");
//cvCvtColor(image,dst,CV_RGB2GRAY);
cvShowImage("ha",image);
cvWriteFrame(writer,image);
if((cvWaitKey(100))==27)
break;
}
cvReleaseVideoWriter(&writer);
cvReleaseCapture(&capture);
//cvReleaseImage(&dst);
cvDestroyWindow("ha");
}
例2-10彩色视频变灰度视频
//Convertavideotograyscale
//argv[1]:
inputvideofile
//argv[2]:
nameofnewoutputfile
//
#include"stdafx.h"
#include"cv.h"
#include"highgui.h"
intmain(intargc,char*argv[])
{
CvCapture*capture=0;
capture=cvCreateFileCapture("wanqiu.avi");
if(!
capture)
{
return-1;
}
IplImage*bgr_frame=cvQueryFrame(capture);//Initthevideoread
doublefps=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
CvSizesize=cvSize(
(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT)
);
CvVideoWriter*writer=cvCreateVideoWriter("hah.avi",CV_FOURCC('M','J','P','G'),fps,size);
IplImage*logpolar_frame=cvCreateImage(size,IPL_DEPTH_8U,3);
while((bgr_frame=cvQueryFrame(capture))!
=NULL)
{
cvLogP