OpenCv练习
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> //OpenCV核心库头文件 #include <opencv2\core\core.hpp> //OpenCV图形处理头文件 #include <opencv2\highgui\highgui.hpp> #include "opencv2\opencv.hpp" //OpenCV核心动态链接库,和core.hpp头文件对应,d代表调试版本 #pragma comment(lib,"opencv_core249d.lib") //OpenCV图形处理动态链接库,和highgui.hpp头文件对应,d代表调试版本 #pragma comment(lib,"opencv_highgui249d.lib") #pragma comment(lib,"opencv_video249d.lib") #pragma comment(lib,"opencv_imgproc249d.lib") int test_0() { //窗口名称 std::string windowName = "HelloWorld"; //图像名称 std::string imgFile = "opencv-logo.png"; //读入图像 cv::Mat image = cv::imread(imgFile); //如果无法读取图形 if(!image.data) { std::cout << "无法打开图像文件" <<std::endl; system("PAUSE");//暂停窗口 return -1; } //创建一个新窗口 cv::namedWindow(windowName); //将图像显示都新创建的窗口中 cv::imshow(windowName,image); //等待,直到用户按任意键时退出 cv::waitKey(0); } void saturate_sv(IplImage* img) { for ( int y = 0; y < img->height; y++ ){ uchar* ptr = (uchar*)(img->imageData + y * img->widthStep); for ( int x = 0; x < img->width; x++ ){ ptr[3*x + 1] = 255; ptr[3*x + 2] = 255; } } } void test_1() { IplImage *img = cvLoadImage("opencv-logo.png"); saturate_sv(img); cvNamedWindow("Example",CV_WINDOW_AUTOSIZE); cvShowImage("Example", img); cvWaitKey(0);//让程序暂停 cvReleaseImage(&img); cvDestroyWindow("Example"); } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// //播放视频 void test_2() { cvNamedWindow("demo2",CV_WINDOW_AUTOSIZE); CvCapture* capture = cvCreateFileCapture("01-上一次课程复习_.avi"); IplImage* frame; while(1){ frame = cvQueryFrame( capture ); if ( !frame ) break; cvShowImage("demo2",frame); char c = cvWaitKey(33); if( c == 27 ) break;// esc ASCII } cvWaitKey(0);//让程序暂停 cvReleaseCapture( &capture ); cvDestroyWindow("demo2"); } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// int g_slider_position = 0; CvCapture *g_capture = NULL; void onTrackbarSlider(int pos) { cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos); } // 显示进度条 void test_03() { cvNamedWindow("demo3",CV_WINDOW_AUTOSIZE); g_capture = cvCreateFileCapture("01-上一次课程复习_.avi"); int frames = (int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT); if ( frames != 0 ){ cvCreateTrackbar("position","demo3",&g_slider_position,frames,onTrackbarSlider); } IplImage* frame; while(1){ frame = cvQueryFrame( g_capture ); if ( !frame ) break; cvShowImage("demo3",frame); char c = cvWaitKey(33); if( c == 27 ) break;// esc ASCII } cvWaitKey(0);//让程序暂停 cvReleaseCapture( &g_capture ); cvDestroyWindow("demo3"); } ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// //载入一幅图像进行平滑处理 void test_04(IplImage* image) { // create some windows to show the input and output image in cvNamedWindow("demo4-in"); cvNamedWindow("demo4-out"); // create a window to show our input image cvShowImage("demo4-in",image); // create an image to hold the smoothed output IplImage* out = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,3); // do the smoothing cvSmooth(image, out, CV_GAUSSIAN, 3, 3); cvShowImage("demo4-out",out); // be tidy cvReleaseImage(&out); cvWaitKey(0);//让程序暂停 cvDestroyWindow("demo4-in"); cvDestroyWindow("demo4-out"); } // 创建一副宽度和高度尺寸为输入的一半的图像 IplImage* doPryDown(IplImage* in, int filter = CV_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); return out; } // Canny 边缘检测将输出写入一个单通道(灰度级)图像 //边缘检测器产生一个与尺寸输入大小相同,但是只有一个通道的图像 IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double 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; } // 写入AVI视频文件 void test_05() { CvCapture* capture = 0; capture = cvCreateFileCapture("01-上一次课程复习_.avi"); IplImage* bgr_frame = cvQueryFrame(capture); double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); CvSize size = cvSize((int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT)); CvVideoWriter* writer = cvCreateVideoWriter("hello.avi", CV_FOURCC('M','J','P','G'), fps, size); IplImage* logpolar_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); std::cout << "while" << std::endl; while( (bgr_frame = cvQueryFrame(capture)) != NULL ){ std::cout << "enter while" << std::endl; CvPoint2D32f temp; temp.x = bgr_frame->width/2; temp.y = bgr_frame->height/2; cvLogPolar(bgr_frame, logpolar_frame, temp, 40, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS); cvWriteFrame( writer, logpolar_frame ); } std::cout << "endl" << std::endl; cvReleaseVideoWriter(&writer); cvReleaseImage(&logpolar_frame); cvReleaseCapture(&capture); } // 矩阵和图像类型 void test_06() { float vals[] = { 0.866600, -0.522200, 0.522200, -0.866600 }; CvMat rotmat; cvInitMatHeader( &rotmat, 2, 2, CV_32FC1, vals ); CvMat* mat_ptr = cvCreateMat(2, 2, CV_32FC1); float elem = 7.7; *((float*)CV_MAT_ELEM_PTR(*mat_ptr,2,2)) = elem; } int _tmain(int argc, _TCHAR* argv[]) { //IplImage *img = cvLoadImage("opencv-logo.png"); //IplImage *img1 = doPryDown(img); //IplImage *img2 = doPryDown(img1); //IplImage *img3 = doCanny(img2, 10, 100, 3); test_1(); return 0; }