例1显示照片
#include "stdafx.h"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
const char* imagename = "woheadai.jpg";
Mat img = imread(imagename);
if(img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}
imshow("image", img);
waitKey();
return 0;}
从文件中读入一幅图像,将之反色,然后显示出来
//
// hello-world.cpp
//
//
#include <stdafx.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
IplImage* img = 0;
int height,width,step,channels;
uchar *data;
int i,j,k;
const char* imagename = argc > 1 ? argv[1] : "ha.jpg";
/* if(argc<2){
printf("Usage: main <image-file-name>\n\7");
exit(0);
}
*/
// load an image
img=cvLoadImage(imagename);
if(!img){
printf("Could not load image file: %s\n",argv[1]);
exit(0);
}
// get the image data
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
printf("Processing a %dx%d image with %d channels\n",height,width,channels);
// create a window
cvNamedWindow("ao", 0);
cvMoveWindow("ao", 100, 100);
// invert the image
for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
data[i*step+j*channels+k]=255-data[i*step+j*channels+k];
// show the image
cvShowImage("ao", img );
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&img );
return 0;
}
单张RGB图片变成灰度图并保存
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
int main(int argc, char* argv[])
{
IplImage *image;
IplImage *result;
image=cvLoadImage("lena.jpg",-1);
//注意指针变量定要先初始化才能使用,否则崩溃
//灰度转换时通道一定¬要设置正确
int channel=1; //image->nChannels;
int depth=image->depth;
CvSize sz;
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");
return 0;
}
例2显示视频
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
void main( )
{
cvNamedWindow("example2",0);
CvCapture *capture=cvCreateFileCapture("111.mp4");
IplImage *frame;
while(1)
{
frame=cvQueryFrame(capture);
if(!frame){break;}
cvShowImage("example2",frame);
char c=cvWaitKey(33);
if(c==27){break;}
}
cvReleaseCapture(&capture);
cvDestroyWindow("example2");
}
例3 滚动条显示视频
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int g_slider_position = 0;
CvCapture* g_capture = NULL;
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);
}
int main(int argc, char** argv)
{
cvNamedWindow( "Example3", CV_WINDOW_AUTOSIZE );
g_capture = cvCreateFileCapture( "1111.mp4");
int frames = (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);
char c=cvWaitKey(33);
if(c==27){break;}
} // While loop (as in Example 2) capture & show video…-
cvReleaseCapture(&g_capture);
cvDestroyWindow("example2");// Release memory and destroy window…-
return(0);
}
例4图像平滑处理
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
void example2_4( IplImage* image )
{
// Create some windows to show the input
// and output images in.
cvNamedWindow( "Example4-in" );
cvNamedWindow( "Example4-out" );
// Create a window to show our input image
//
cvShowImage( "Example4-in", image );
//Create an image to hold the smoothed output
//cvGetSize(image)
IplImage* out=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,3);
// Do the smoothing
//
cvSmooth( image, out, CV_GAUSSIAN, 3, 3 );//CV_GAUSSIAN
// Show the smoothed image in the output window
//
cvShowImage( "Example4-out", out );
// Be tidy
//
cvReleaseImage( &out );
// Wait for the user to hit a key, then clean up the windows
//
cvWaitKey( 0 );
cvDestroyWindow( "Example4-in" );
cvDestroyWindow( "Example4-out" );
}
int main(void)
{
IplImage* img = cvLoadImage("woheadai.jpg");
example2_4(img);
return 1;
}
例5 使用cvPyDown()使图片长宽各缩小一半
#include "stdafx.h"
#include "highgui.h"
#include"cv.h"
IplImage * doPyrDown(IplImage * in)// int filter = 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 );
};
int main(int argc,char ** argv)
{
IplImage * img = cvLoadImage("woheadai.jpg");
doPyrDown( img );
cvReleaseImage(&img);
return 0;
}
例6 canny 边缘检测将输出写入一个单通道(灰度级)图像
#include "stdafx.h"
#include "highgui.h"
#include"cv.h"
IplImage * doCanny(IplImage * in,double lowThresh, double highThresh,int aperture )
{
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("11.jpg", out);
cvReleaseImage(&out);
cvDestroyWindow("hb");
return( out );
};
int main(int argc,char ** argv)
{
IplImage * img = cvLoadImage( "11.jpg", 0);
doCanny( img,1,2.4,3);
cvReleaseImage(&img);
return 0;
}
例7 图像处理中两次缩放和canny 边缘检测例 8 通过每个独立阶段释放内存来简化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, double lowThresh, double highThresh, int aperture )
{
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 );
};
int main(int argc,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);
return 0;
}
例9 capture 结构初始化后,从摄像设备读入图像并保存(或者从视频无区别)
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "conio.h"
int main( ) {
CvCapture *capture = cvCaptureFromCAM( CV_CAP_ANY );
IplImage *image = NULL; //*dst = NULL;
image = cvQueryFrame( capture );
CvSize size = cvGetSize( image );
//dst = cvCreateImage( size, image->depth, 1 );
double fps = 10;
CvVideoWriter* writer = cvCreateVideoWriter( "Video from CAMERA.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" );
}