1.5.2【图像腐蚀】
#include
#include
//#include
using namespace cv;//cvmingmingkongjian
int main()
{
Mat srcImage=imread("1.jpg");
imshow("{original]",srcImage);
Mat element=getStructuringElement(MORPH_RECT,Size(15,15));
Mat dstImage;
erode(srcImage,dstImage,element);
imshow("[errosion]",dstImage);
waitKey(0);
return 0;
}
【图像模糊】
#include"opencv2/highgui/highgui.hpp"
#include"opencv2/imgproc/imgproc.hpp"
using namespace cv;
int main()
{
Mat srcImage=imread("1.jpg");//zairu
imshow("[junzhilvbo-original]",srcImage);//xianshiyuantu
Mat dstImage;
blur(srcImage,dstImage,Size(7,7));
imshow("[blur]",dstImage);
waitKey(0);
}
【canny边缘检测】
#include"opencv2/opencv.hpp"
#include"opencv2/imgproc/imgproc.hpp"
using namespace cv;
int main()
{
Mat srcImage=imread("1.jpg");//zairu
imshow("[original-canny]",srcImage);//xianshiyuantu
Mat edge,grayImage;
cvtColor(srcImage,grayImage,CV_BGR2GRAY);
blur(grayImage,edge,Size(3,3));
imshow("[gray]",grayImage);
Canny(edge,edge,3,9,3);
imshow("[Canny]",edge);
waitKey(0);
return 0;
}
【1.6.1】读取并播放视频
#include
using namespace cv;
int main()
{
VideoCapture capture("1.avi");
while(1)
{
Mat frame;
capture>>frame;
if (frame.empty())
{
break;
}
imshow("duqushipin",frame);
waitKey(3);
}
return 0;
}
【书上摄像头】
#include
int main(int argc,char*argv[])
{
cvNamedWindow("avi");
CvCapture* capture=cvCreateCameraCapture(-1);//-1 代表随机选取摄像头
IplImage* frame;
while(1)
{
frame=cvQueryFrame(capture);
if(!frame)
{
break;
}
cvShowImage("avi",frame);
char c=cvWaitKey(33);//控制帧率
if(c==27)
{
break;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("avi");//释放窗口
return 0;
}
【视频教程中的打开视频】
#include
int main(int argc,char*argv[])
{
cvNamedWindow("avi");
CvCapture* capture=cvCreateFileCapture("D:\\opencvtest-demo\\1.avi");
IplImage* frame;
while(1)
{
frame=cvQueryFrame(capture);
if(!frame)
{
break;
}
cvShowImage("avi",frame);
char c=cvWaitKey(33);//控制帧率
if(c==27)
{
break;
}
}
cvReleaseCapture(&capture);
cvDestroyWindow("avi");//释放窗口
return 0;
}
【创建滚动条+指针位置】
#include "cv.h"
#include "highgui.h"
int g_slider_pos=0;
void on_changed(int pos)
{
printf("g_slider_pos is %d\n",g_slider_pos);
}
int main(int argc,char*argv[])
{
cvNamedWindow("show");
cvCreateTrackbar(
"position",
"show",
&g_slider_pos,
400,
on_changed
);
cvShowImage("show",NULL);
cvWaitKey(0);
return 0;
}
【从滚动条开始播放】
【让滚动条随着视频移动】
//#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include
using namespace cv;
IplImage* frame;
int c;
int g_slider_pos=0;
CvCapture*g_capture=NULL;
int frame_count=0;
void onTrackbarSlider(int pos)
{
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,
pos
);
frame_count=pos;
}
int main(int argc,char*argv[])
{
cvNamedWindow("show");
g_capture=cvCreateFileCapture("D:\\opencvtest-demo\\1.avi");
int frames=(int)cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if(frame>0);
{
cvCreateTrackbar(
"position",
"show",
&g_slider_pos,
frames,
onTrackbarSlider
);
while(1)
{
frame=cvQueryFrame(g_capture);
if(!frame)
{
break;
}
cvShowImage("show",frame);
char c=cvWaitKey(33);
if(c==27)
{
break;
}
}
cvSetTrackbarPos("Position","show",frame_count);
frame_count++;
}
cvShowImage("show",NULL);
cvWaitKey(0);
return 0;
}
【写视频文件】
cvSize size= cvSize(480,360);
double fps=15;
CvVideoWriter = cvCreateVideoWriter("");
int image_id=0;
char filename[256];
sprintf(filename,"");
IplImage* src=cvLoadImage(filename);
if(!src)
{
return 0;
}
IplImage*src_resize=cvCreateImage(size,8,3);
cvNamedWindow("avi");
while(src)
{
cvShowImage("avi",src_resizze);
cvWaitKey(1);
cvResize(src,src_resize);
cvWriteFrame(writer,src_resize);
cvReaseImage(&src);
image_id++;
printf(filename,"");
src=cvLoadImage(filename);
}
cvReaseVideoWriter(&writer);
cvReleaseImage(&src_resize);
return 0;
}
【显示图像】
#include"stdafx.h"
#include"cv.h"
#include"cxcore.h"
#include"highgui.h"
int main(int argc,char*argv[])
{
IplImage* src=cvLoadImage("D:\\opencvtest-demo\\1.jpg",CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR);
cvNamedWindow("showImage");
cvShowImage("showImage",src);
cvWaitKey(0);
cvReleaseImage(&src);
cvDestroyWindow("showImage");
return 0;
}
【写视频文件】
#include"stdafx.h"
#include"cv.h"
#include"highgui.h"
#include"cxcore.h"
#include
#include
int main(int argc,char*argv[])
{
CvSize size=cvSize(600,400);
double fps=15;
CvVideoWriter*writer=cvCreateVideoWriter("D:\\opencvtest-demo\\out.avi",CV_FOURCC('X','V','I','D'),fps,size);
int image_id=1;
char filename[256];
sprintf(filename,"D:\\opencvtest-demo\\火柴\\%d.jpg",image_id);
IplImage*src=cvLoadImage(filename);//先加载进来一帧,判断是否加载成功
if(!src)
{
return 0;
}
IplImage*src_resize=cvCreateImage(size,8,3);
cvNamedWindow("avi");
while(src)
{
cvShowImage("avi",src_resize);
cvWaitKey(50);
cvResize(src,src_resize);
cvWriteFrame(writer,src_resize);
cvReleaseImage(&src);//之前图像释放
image_id++;
sprintf(filename,"D:\\opencvtest-demo\\火柴\\%d.jpg",image_id);
src=cvLoadImage(filename);
}
cvReleaseVideoWriter(&writer);
cvReleaseImage(&src_resize);
return 0;
}
【矩阵通道与维度】
#include"stdafx.h"
#include
//#include"cxtypes.h"
#include"highgui.h"
#include"cxcore.h"
#include"cv.h"
int main(int argc,char*argv[])
{
float data[18]=
{
30,40,60,50,40,44,
67,88,55,33,22,97,
59,69,32,46,25,45,
};
CvMat mat;
cvInitMatHeader(&mat,3,1,CV_32FC3,data);//根据通道数,mat后面3,6变3,3;本例讲的从1-3通道,换系数
int nChannels=3;;
mat.data.ptr;
int y,x;
for(y=0;y {
float*p_float=(float*)(mat.data.ptr+y*mat.step);//偏移地址是一行的字节数,访问第几行,用y乘以几;通道数变化不改变y偏移方法
for(x=0;x { float value[3];
value[0]=*(p_float+x*nChannels);
value[1]=*(p_float+x*nChannels+1);
value[2]=*(p_float+x*nChannels+2);
printf("(%f %f %f)",value[0],value[1],value[2]);
}
}
return 0;
}
【第十集 结构体详解 BGR通道分别显示】
#include
#include"highgui.h"
int main(int argc,char*argv[])
{
IplImage* img=cvLoadImage("D:\\opencvtest-demo\\1.jpg");
IplImage* sub_img=cvLoadImage("D:\\opencvtest-demo\\2.jpg");
CvRect rect;
rect.x=107;
rect.y=13;
rect.width=sub_img->width;
rect.height=sub_img->height;
cvSetImageROI(img,rect);
cvCopy(sub_img,img);//也可以自己用双重循环实现
cvResetImageROI(img);//取消ROI区域
/*IplImage* r_img,*b_img,*g_img;
b_img=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
g_img=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
r_img=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);//一个通道所以写1
int y,x;//访问图像矩阵
for(y=0;y
height;y++)
{
unsigned char* prow=(unsigned char*)(img->imageData+y*img->widthStep);
unsigned char* brow=(unsigned char*)(b_img->imageData+y*b_img->widthStep);
unsigned char* grow=(unsigned char*)(g_img->imageData+y*g_img->widthStep);
unsigned char* rrow=(unsigned char*)(r_img->imageData+y*r_img->widthStep);
for(x=0;xwidth;x++)
{
prow[1*x+0]=0;
prow[1*x+1]=255;
prow[1*x+2]=0;
//第y行地x像素的B通道
//prow[3*x+1]=255;//第y行地x像素的G通道
//prow[3*x+2]=//第y行地x像素的R通道
brow[x]=prow[3*x+0];//三通道对单通道的赋值
grow[x]=prow[3*x+1];
rrow[x]=prow[3*x+2];
}
}
cvNamedWindow("b");
cvNamedWindow("g");
cvNamedWindow("r");
cvShowImage("b",r_img);
cvShowImage("g",g_img);
cvShowImage("r",b_img);
//img->origin=1;
//img->depth
//printf("nChannels is %d\n",img->nChannels);//打印图像的通道 */
cvNamedWindow("1");
cvShowImage("1",img);
cvWaitKey(0);
return 0;
}
【第11集 灰度直方图 】
#include"stdafx.h"
#include"cv.h"
#include"cxcore.h"
#include"highgui.h"
int main(int argc,char*argv[])
{
IplImage* src=cvLoadImage("D:\\opencvtest-demo\\4.jpg",CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR);
cvNamedWindow("showImage");
cvShowImage("showImage",src);
cvWaitKey(0);
int dims=1;
int size=256;
float range[]={0,255};
float* ranges[]={range};
cvCreateHist(dims,&size,CV_HIST_ARRAY,ranges,1);
cvClearHist(hist);
IplImage* imgRed=cvCreateImage(cvGetSize(src),8,1);//3通道
IplImage* imgGreen=cvCreateImage(cvGetSize(src),8,1);
IplImage* imgBlue=cvCreateImage(cvGetSize(src),8,1);
cvSplit(src,imgBlue,imgGreen,imgRed,NULL);//分解成4个通道
cvCalcHist(&imgBlue,hist,0,0);
//..
cvClearHist(hist);
cvCalcHist(&imgGreen,hist,0,0);
//..
cvClearHist(hist);
cvCalcHist(&imgRed,hist,0,0);
//..
cvClearHist(hist);
IplImage* DrawHistogram(CvHistogram* hist,float )
{
float hisMax=0;
cvGetMinMaxHistValue(hist,0,&histMax,0,0);//计算直方图最大值
IplImage* imgHist=cvCreateImage(cvSize(256*))
cvZero(imgHist);//因为随机值,先置0
for(int i=0;i<255;i++)
{
float histValue+cvQueryHistValue_1D(hist,i);//获取当前bin的值
float histValue+cvQueryHistValue_1D(hist,i+1);//获取下一个bin的值
}
}
return 0;
}
【转换成灰度图像并存储】
#include "highgui.h"
#include
#include
int main(int argc, char* argv[])
{
IplImage* src=cvLoadImage("D:\\opencvtest-demo\\1.jpg");
if( !src->imageData ) // 检查是否正确载入图像
return -1;
cvNamedWindow("image" ); //创建窗口
cvShowImage("image", src); //显示图像
IplImage* gray_image = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);//创建目标图像
cvCvtColor(src,gray_image,CV_BGR2GRAY);//cvCvtColor(src,des,CV_BGR2GRAY),从RGB到灰度图像
cvNamedWindow("gray_image");//创建显示目标的窗口
cvShowImage("gray_image",gray_image);//显示灰度图像
cvWaitKey(0);
cvSaveImage("D:\\opencvtest-demo\\gray_image.jpg",gray_image); //保存图像
cvReleaseImage(&src); //释放空间
cvReleaseImage(&gray_image);
cvDestroyAllWindows();
return 0;
}
【11集 灰度直方图】【未能实现】
#include"stdafx.h"
#include"cv.h"
#include"cxcore.h"
#include"highgui.h"
#include
/*#define MAX_ASCII (127)
int* DrawHistogram(int *Count); */
int main(int argc,char*argv[])
{
//float scaleX=1, float scaleY=1;
CvHistogram* hist;
IplImage* src=cvLoadImage("D:\\opencvtest-demo\\4.jpg",CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR);
cvNamedWindow("showImage");
cvShowImage("showImage",src);
cvWaitKey(0);
IplImage* DrawHistogram(CvHistogram* hist,float scaleX=1, float scaleY=1);
{
float scaleX=1, float scaleY=1;
float histMax=0;
cvGetMinMaxHistValue(hist,0,&histMax,0);//计算直方图最大值
IplImage* imgHist=cvCreateImage(cvSize(256*scaleX,64*scaleY),8,1);//改变参数来改变显示图像的大小
cvZero(imgHist);//因为随机值,先置0
for(int i=0;i<255;i++)
{
float histValue=cvQueryHistValue_1D(hist,i);//获取当前bin的值
float nextValue=cvQueryHistValue_1D(hist,i+1);//获取下一个bin的值
CvPoint pt1=cvPoint( i*scaleX,64*scaleY);
CvPoint pt2=cvPoint((i+1)*scaleX,64*scaleY);
CvPoint pt3=cvPoint((i+1)*scaleX,64*scaleY-(nextValue/histMax)*64*scaleY);
CvPoint pt4=cvPoint( i*scaleX,(64-(nextValue/histMax)*64) *scaleY);
int numPts=5;
CvPoint pts[5];
pts[0]=pt1;
pts[1]=pt2;
pts[2]=pt3;
pts[3]=pt4;
pts[4]=pt1;
cvFillConvexPoly(imgHist,pts,numPts,cvScalar(255));
}
}
int dims=1;
int size=256;
float range[]={0,255};
float* ranges[]={range};
hist=cvCreateHist(dims,&size,CV_HIST_ARRAY,ranges,1);
cvClearHist(hist);
IplImage* imgRed=cvCreateImage(cvGetSize(src),8,1);//3通道
IplImage* imgGreen=cvCreateImage(cvGetSize(src),8,1);
IplImage* imgBlue=cvCreateImage(cvGetSize(src),8,1);
cvSplit(src,imgBlue,imgGreen,imgRed,NULL);//分解成4个通道
cvCalcHist(&imgBlue,hist,0,0);
IplImage* histBlue= DrawHistogram(hist);
cvClearHist(hist);
cvCalcHist(&imgGreen,hist,0,0);
IplImage* histGreen= DrawHistogram(hist);
cvClearHist(hist);
cvCalcHist(&imgRed,hist,0,0);
IplImage* histRed= DrawHistogram(hist);
cvClearHist(hist);
cvNamedWindow("B");
cvNamedWindow("G");
cvNamedWindow("R");
cvShowImage("B",histBlue);
cvShowImage("G",histGreen);
cvShowImage("R",histRed);
cvWaitKey(0);
return 0;
}
【12 RGB颜色空间】【未能实现】
//#include"stdafx.h"
//#include"cv.h"
#include"cxcore.h"
#include"highgui.h"
int main(int argc,char*argv[])
{
IplImage* src=cvLoadImage("D:\\opencvtest-demo\\1.jpg");
IplImage* dst=cvCreateImage(cvGetSize(src),8,3);
cvMerge(src,src,src,0,dst);
cvNamedWindow("src");
cvShowImage("src",src);
//cvWaitKey(0);
cvNamedWindow("dst");
cvShowImage("dst",dst);
//cvWaitKey(0);
/*cvReleaseImage(&src);
cvDestroyWindow("src");
cvReleaseImage(&dst);
cvDestroyWindow("dst");*/
return 0;
}
【14 颜色空间转换】
#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main(int argc,char*argv[])
{
IplImage* src=cvLoadImage("D:\\opencvtest-demo\\1.jgp");
cvNamedWindow("src");
cvShowImage("src",hrc);
/*
IplImage* src=cvLoadImage("D:\\opencvtest-demo\\1.jpg");
IplImage* img_hsv=cvCreateImage(cvGetSize(src),8,3);//char型,8位
IplImage* img_h=cvCreateImage(cvGetSize(src),8,1);
IplImage* img_s=cvCreateImage(cvGetSize(src),8,1);
IplImage* img_v=cvCreateImage(cvGetSize(src),8,1);
*/
IplImage*src_float=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,3);//char型转换成float
IplImage* src=cvLoadImage("D:\\opencvtest-demo\\1.jpg");
IplImage* img_hsv=cvCreateImage(cvGetSize(src),8,3);//char型,8位
IplImage* img_h=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* img_s=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* img_v=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
cvConvertScale(src,src_float,1.0,0);//最后那个0是偏移量
cvSplit(img_hsv,img_h,img_s,img_v,NULL);
IplImage* hsv=cvCreateImage(cvGetSize(src),8,3);//char型,8位
IplImage* h=cvCreateImage(cvGetSize(src),8,1);
IplImage* s=cvCreateImage(cvGetSize(src),8,1);
IplImage* v=cvCreateImage(cvGetSize(src),8,1);
cvConvertScale(img_h,h,(1.0/360.0)*255.0,0);
cvConvertScale(img_s,s,255.0,0);
cvConvertScale(img_v,v,1.0,0);
cvMerge(h,s,v,NULL,hsv);//合成
/*
cvConvertScale(img_h,h,(1.0));
cvCvtColor(src,img_hsv,CV_BGR2HSV);
cvSplit(img_hsv,img_h,img_s,img_v,NULL);
*/
cvNamedWindow("hsv");
cvShowImage("hsv",hsv);
for(int y=0;y
height;y++)
{ for(int x=0;xwidth;x++)
{ float value=cvGetReal2D(img_h,y,x);
printf("%f",value);
}
//printf("\n");
}
}