openCV的图形程序的基本介绍

陈清 (大卫)qchen@discover.uottawa.ca
渥太华大学信息技术工程学院
2007-1-15
内容一览
1 介绍
2 openCV中的图片数据结构
3 图形的基本操作
4 使用视频
5 参考资料

1 介绍

2 openCV中的图片数据结构

在openCV中加载并显示苹果图片。

 

#include "cv.h" //main OpenCV functions
#include "highgui.h" //OpenCV GUI functions¯include <stdio.h>
int main()
{
/* declare a new IplImage pointer, the basic
image data structure in OpenCV */
IplImage* newImg;
/* load an image named "apple.bmp", 1 means
this is a color image */
newImg = cvLoadImage("apple.bmp",1);
//create a new window
cvNamedWindow("Window", 1);
//display the image in the window
cvShowImage("Window", newImg);
//wait for key to close the window
cvWaitKey(0);
cvDestroyWindow( "Window" ); //destroy the window
cvReleaseImage( &newImg ); //release the memory for the image
return 0;
}


 IplImage是基本的图片数据结构

3图形的基本操作

Threshold阈值

#include "cv.h"
#include "highgui.h"
#include "math.h"
int main()
{
IplImage* src;
IplImage* colorThresh;
IplImage* gray;
IplImage* grayThresh;
int threshold = 120, maxValue = 255;
int thresholdType = CV_THRESH_BINARY;
src = cvLoadImage("apple.bmp", 1);
colorThresh = cvCloneImage( src );
gray = cvCreateImage( cvSize(src->width, src->height), IPL_DEPTH_8U, 1 );
cvCvtColor( src, gray, CV_BGR2GRAY );
grayThresh = cvCloneImage( gray );
cvNamedWindow( "src", 1 ); cvShowImage( "src", src );
cvNamedWindow( "gray", 1 ); cvShowImage( "gray", gray );
cvThreshold(src, colorThresh, threshold, maxValue, thresholdType);
cvThreshold(gray, grayThresh, threshold, maxValue, thresholdType);
cvNamedWindow( "colorThresh", 1 ); cvShowImage( "colorThresh", colorThresh );
cvNamedWindow( "grayThresh", 1 ); cvShowImage( "grayThresh", grayThresh );
cvWaitKey(0);
cvDestroyWindow( "src" );
cvDestroyWindow( "colorThresh" );
cvDestroyWindow( "gray" );
cvDestroyWindow( "grayThresh" );
cvReleaseImage( &src );
cvReleaseImage( &colorThresh );
cvReleaseImage( &gray );
cvReleaseImage( &grayThresh );
return 0;
}

Canny边缘检测

#include "cv.h"
#include "highgui.h"
int main()
{
IplImage* newImg; // original image
IplImage* grayImg; // gray image for the conversion of the original image
IplImage* cannyImg; // gray image for the canny edge detection
//load original image
newImg = cvLoadImage("apple.bmp",1);
//create a single channel 1 byte image (i.e. gray-level image)
grayImg = cvCreateImage( cvSize(newImg->width, newImg->height), IPL_DEPTH_8U, 1 );
//convert original color image (3 channel rgb color image) to gray-level image
cvCvtColor( newImg, grayImg, CV_BGR2GRAY );
cannyImg = cvCreateImage(cvGetSize(newImg), IPL_DEPTH_8U, 1);
// canny edge detection
cvCanny(grayImg, cannyImg, 50, 150, 3);
cvNamedWindow("src", 1);
cvNamedWindow("canny",1);
cvShowImage( "src", newImg );
cvShowImage( "canny", cannyImg );
cvWaitKey(0);
cvDestroyWindow( "src" );
cvDestroyWindow( "canny" );
cvReleaseImage( &newImg );
cvReleaseImage( &grayImg );
cvReleaseImage( &cannyImg );
return 0;
}


轮廓检查

 

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main()
{
IplImage* newImg = NULL;
IplImage* grayImg = NULL;
IplImage* contourImg = NULL;
//parameters for the contour detection
CvMemStorage * storage = cvCreateMemStorage(0);
CvSeq * contour = 0;
int mode = CV_RETR_EXTERNAL;
mode = CV_RETR_CCOMP; //detect both outside and inside contour
cvNamedWindow("src", 1);
cvNamedWindow("contour",1);
//load original image
newImg = cvLoadImage("applebw.bmp",1);
//create a single channel 1 byte image (i.e. gray-level image)
grayImg = cvCreateImage( cvSize(newImg->width, newImg->height), IPL_DEPTH_8U, 1 );
//convert original color image (3 channel rgb color image) to gray-level image
cvCvtColor( newImg, grayImg, CV_BGR2GRAY );
cvShowImage( "src", newImg );
//make a copy of the original image to draw the detected contour
contourImg = cvCreateImage(cvGetSize(newImg), IPL_DEPTH_8U, 3);
contourImg=cvCloneImage( newImg );
//find the contour
cvFindContours(grayImg, storage, &contour, sizeof(CvContour), mode, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
//draw the contour
cvDrawContours(contourImg, contour, CV_RGB(0, 255, 0), CV_RGB(255, 0, 0), 2, 2, 8);
cvShowImage( "contour", contourImg );
cvWaitKey(0);
cvDestroyWindow( "src" ); cvDestroyWindow( "contour" );
cvReleaseImage( &newImg ); cvReleaseImage( &grayImg ); cvReleaseImage( &contourImg );
cvReleaseMemStorage(&storage);
return 0;
}

膨胀/腐蚀 (Dilate/Erode)

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main()
{
IplImage* newImg = NULL;
IplImage* dilateImg = NULL;
IplImage* erodeImg = NULL;
cvNamedWindow("src", 1);
cvNamedWindow("dilate",1);
cvNamedWindow("erode",1);
//load original image
newImg = cvLoadImage("apple.bmp",1);
cvShowImage( "src", newImg );
//make a copy of the original image
dilateImg=cvCloneImage( newImg );
erodeImg=cvCloneImage( newImg );
//dilate image
cvDilate(newImg,dilateImg,NULL,4);
//erode image
cvErode(newImg,erodeImg,NULL,4);
cvShowImage( "dilate", dilateImg );
cvShowImage( "erode", erodeImg );
cvWaitKey(0);
cvDestroyWindow( "src" ); cvDestroyWindow( "dilate" ); cvDestroyWindow( "erode" );
cvReleaseImage( &newImg ); cvReleaseImage( &dilateImg ); cvReleaseImage( &erodeImg );
return 0;
}

填充(Flood and Fill)

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
int main()
{
IplImage* newImg = NULL;
IplImage* ffImg = NULL;
//flood and fill parameters
int lo_diff, up_diff; //the low and up flood randge which can be adjusted
CvConnectedComp comp;
CvPoint floodSeed; //the original pixel where the flood begins
CvScalar floodColor;
lo_diff=8;
up_diff=8;
floodColor = CV_RGB( 255, 0, 0 ); //set the flood color to red
cvNamedWindow("src", 1);
cvNamedWindow("flood&fill",1);
//load original image
newImg = cvLoadImage("apple.bmp",1);
cvShowImage( "src", newImg );
//make a copy of the original image
ffImg=cvCloneImage( newImg );
floodSeed=cvPoint(60,60); //flooding start from pixel(60, 60)
//Flood and Fill from pixel(60, 60) with color red and the flood range of (-8, +8)
cvFloodFill( ffImg, floodSeed, floodColor, CV_RGB( lo_diff, lo_diff, lo_diff ),
CV_RGB( up_diff, up_diff, up_diff ), &comp, 8, NULL);
cvShowImage( "flood&fill", ffImg );
cvWaitKey(0);
cvDestroyWindow( "src" ); cvDestroyWindow( "flood&fill" );
cvReleaseImage( &newImg ); cvReleaseImage( &ffImg );
return 0;
}

旋转和缩放(Rotate and Scale)

#include "cv.h"
#include "highgui.h"
#include "math.h"
int main()
{
IplImage* src;
IplImage* dst;
int delta;
int angle;
src = cvLoadImage("apple.bmp", 1);
dst = cvCloneImage( src );
delta = 1; angle = 0;
cvNamedWindow( "src", 1 );
cvShowImage( "src", src );
for(;;)
{
float m[6];
double factor = (cos(angle*CV_PI/180.) + 1.1)*3;
CvMat M = cvMat( 2, 3, CV_32F, m );
int w = src->width;
int h = src->height;
m[0] = (float)(factor*cos(-angle*2*CV_PI/180.));
m[1] = (float)(factor*sin(-angle*2*CV_PI/180.));
m[2] = w*0.5f;
m[3] = -m[1];
m[4] = m[0];
m[5] = h*0.5f;
cvGetQuadrangleSubPix( src, dst, &M, 1, cvScalarAll(0));
cvNamedWindow( "dst", 1 ); cvShowImage( "dst", dst );
if( cvWaitKey(5) == 27 )
break;
angle = (angle + delta) % 360;
}
return 0;
}


4 使用视频

从文件中进行视频捕捉

CvCapture* cvCaptureFromFile( const char* filename );

从摄像头进行视频捕捉
CvCapture* cvCaptureFromCAM( int index );

范例:

// capture from video device #0
CvCapture* capture = cvCaptureFromCAM(0);

抓取帧:

cvGrabFrame( CvCapture* capture );

从cvGrabFrame:中获取图片

cvRetrieveFrame( CvCapture* capture );

范例:

IplImage* img = 0;
if(!cvGrabFrame(capture)){ // capture a frame
printf("Could not grab a frame\n\7");
exit(0); }
//retrieve the captured frame
img=cvRetrieveFrame(capture);

释放截图资源:

cvReleaseCapture(&capture);

更多参考,见face detection位于C:\Program Files\OpenCV\samples\c\facedetect.c(译注:苹果的直接在压缩文档中)

5 参考资料


(完)


 




 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值