opencv基础小程序大集合
例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);
//注意指針變量定要先初始化才能使用,否則崩潰
//灰度轉(zhuǎn)換時通道一定?要設(shè)置正確
?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?通過每個獨(dú)立階段釋放內(nèi)存來簡化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?結(jié)構(gòu)初始化后,從攝像設(shè)備讀入圖像并保存(或者從視頻無區(qū)別)
#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"?);
}
總結(jié)
以上是生活随笔為你收集整理的opencv基础小程序大集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 整理:深度学习 vs 机器学习 vs 模
- 下一篇: Deep Learning(深度学习)学