【练习4.1】图像转换、Canny检测、图像合并、在图像上输出文字

2014-07-17

第四章 细说HighGUI

练习题第1题

题目大致要求:

a:(1)从视频文件读入数据,(2)转换为灰度图,(3)做Canny边缘检测,显示在三个窗口

b:将a的三个视频显示在一个窗口中

c:在b步骤的不同部分写上合适的文字标签

代码:

  1 #include "stdafx.h"
  2 #include <windows.system.h>
  3 #include <iostream>
  4 #include <cv.h>
  5 #include <highgui.h>
  6 using namespace std;
  7 using namespace cv;
  8 
  9 int _tmain(int argc, _TCHAR* argv[])
 10 {
 11     const char * videoFileName = "D:\\Work\\Work_Programming\\Source\\Video\\Fork.avi";
 12 
 13     cvNamedWindow("原始视频", 0);
 14     cvNamedWindow("灰度视频", 0);
 15     cvNamedWindow("Canny视频", 0);
 16     cvNamedWindow("三合一视频", 0);
 17 
 18     CvCapture * videoCapture = cvCreateFileCapture(videoFileName);
 19     int imgWidth = (int)cvGetCaptureProperty(videoCapture, CV_CAP_PROP_FRAME_WIDTH);
 20     int imgHeight = (int)cvGetCaptureProperty(videoCapture, CV_CAP_PROP_FRAME_HEIGHT);
 21     IplImage * frame;
 22 
 23     IplImage * grayImage = cvCreateImage(cvSize(imgWidth, imgHeight), IPL_DEPTH_8U, 1);
 24     IplImage * cannyImage = cvCreateImage(cvSize(imgWidth, imgHeight), IPL_DEPTH_8U, 1);
 25     IplImage * tripleImage = cvCreateImage(cvSize(imgWidth * 3, imgHeight), IPL_DEPTH_8U, 3);
 26     IplImage *tempImage = cvCreateImage(cvSize(imgWidth, imgHeight), IPL_DEPTH_8U, 3);
 27 
 28     IplImage *subFrameImage = cvCreateImageHeader(cvSize(imgWidth, imgHeight), IPL_DEPTH_8U, 3);
 29     IplImage *subGrayImage = cvCreateImageHeader(cvSize(imgWidth, imgHeight), IPL_DEPTH_8U, 3);
 30     IplImage *subCannyImage = cvCreateImageHeader(cvSize(imgWidth, imgHeight), IPL_DEPTH_8U, 3);
 31 
 32     //--------------------c在图片上显示文字-----------------------    
 33     CvFont ft;
 34     cvInitFont(&ft, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 2, 8);
 35 
 36     while (true)
 37     {
 38         frame = cvQueryFrame(videoCapture);
 39 
 40         if (!frame)
 41         {
 42             break;
 43         }
 44 
 45         //--------------------a(1)小题-----------------------         
 46         cvShowImage("原始视频", frame);
 47 
 48         //--------------------a(2)小题-----------------------    
 49         cvCvtColor(frame, grayImage, CV_BGR2GRAY);
 50         cvShowImage("灰度视频", grayImage);
 51 
 52         //--------------------a(3)小题-----------------------
 53         if (grayImage->nChannels != 1)
 54         {
 55             return 0;
 56         }
 57 
 58         cvCanny(grayImage, cannyImage, 50, 100, 3);
 59         cvShowImage("Canny视频", cannyImage);
 60 
 61         //--------------------b:将三个视频在一个窗口中显式-----------------------
 62         //将原始图像复制到三合一窗口
 63         subFrameImage->origin = tripleImage->origin;
 64         subFrameImage->widthStep = tripleImage->widthStep;
 65         subFrameImage->imageData = tripleImage->imageData;
 66 
 67         cvPutText(frame, "Original", cvPoint(0, 20), &ft, cvScalar(255, 255, 255));
 68 
 69         cvCopy(frame, subFrameImage);
 70 
 71         //将灰度图复制到三合一窗口
 72         subGrayImage->origin = tripleImage->origin;
 73         subGrayImage->widthStep = tripleImage->widthStep;
 74         subGrayImage->imageData = tripleImage->imageData + (subFrameImage->width)*subFrameImage->nChannels;
 75         cvCvtColor(grayImage, tempImage, CV_GRAY2BGR);
 76 
 77         cvPutText(tempImage, "Gray", cvPoint(0, 20), &ft, cvScalar(255, 255, 255));
 78 
 79         cvCopy(tempImage, subGrayImage);
 80 
 81         //将Canny复制到三合一窗口
 82         subCannyImage->origin = tripleImage->origin;
 83         subCannyImage->widthStep = tripleImage->widthStep;
 84         subCannyImage->imageData = tripleImage->imageData + (subFrameImage->width * 2)*subFrameImage->nChannels;
 85         cvCvtColor(cannyImage, tempImage, CV_GRAY2BGR);
 86 
 87         cvPutText(tempImage, "Canny", cvPoint(0, 20), &ft, cvScalar(255, 255, 255));
 88 
 89         cvCopy(tempImage, subCannyImage);
 90 
 91         cvShowImage("三合一视频", tripleImage);
 92 
 93         char c = cvWaitKey(33);
 94         if (c == 27)
 95         {
 96             break;
 97         }
 98     }
 99 
100     cvReleaseImage(&grayImage);
101     cvReleaseImage(&cannyImage);
102     cvReleaseImage(&tripleImage);
103     cvReleaseImage(&tempImage);
104 
105     cvReleaseImageHeader(&subFrameImage);
106     cvReleaseImageHeader(&subGrayImage);
107     cvReleaseImageHeader(&subCannyImage);
108     cvReleaseCapture(&videoCapture);
109     cvDestroyWindow("原始视频");
110     cvDestroyWindow("灰度视频");
111     cvDestroyWindow("Canny视频");
112     cvDestroyWindow("三合一视频");
113 
114     //system("pause");
115 
116     return 0;
117 }

 

结果图像:

posted on 2014-07-18 17:32  毋忆典藏  阅读(915)  评论(0编辑  收藏  举报