1. 程式人生 > >《學習OpenCV》 - OpenCV入門(二)

《學習OpenCV》 - OpenCV入門(二)

目錄

2-1:顯示圖片 

2-2:播放AVI視訊

2-3:視訊滾動條(測試未發現)

2-4:簡單變換

2-5:尺寸變換

2-6:邊緣檢測

2-7/2-8:多樣操作

2-9:攝像機讀入資料


2-1:顯示圖片 

//顯示影象

#include "pch.h"
#include <iostream>
#include"highgui.h"
#include <opencv2/opencv.hpp>
using namespace cv;

int main(int argc, char **argv)
{
    //argv[1]傳入的圖片設定:【專案】-【屬性】-【除錯】-【命令引數】-輸入檔案(影象)路徑
	IplImage* img = cvLoadImage("C:\\Users\\MR_Z\\Desktop\\1.jpg");    //影象檔案載入到記憶體
    
    //螢幕建立視窗,(視窗標題,視窗屬性)
	cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);    //根據實際大小進行拉伸或縮放

    //顯示圖片(視窗名,圖片指標)
	cvShowImage("Example1", img);   

    //程式暫停,等待使用者觸發案件(時間)毫秒單位
	cvWaitKey(0);

    //釋放影象分配的記憶體(影象地址)
	cvReleaseImage(&img);

    //銷燬視窗,釋放記憶體(視窗標題)
	cvDestroyWindow("Example1");

}

2-2:播放AVI視訊

//播放AVI視訊

#include "pch.h"
#include <iostream>
#include"highgui.h"
#include <opencv2/opencv.hpp>
using namespace cv;

int main(int argc, char **argv)
{

	//螢幕建立視窗,(視窗標題,視窗屬性)
	cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);    //根據實際大小進行拉伸或縮放

	//讀入AVI檔案
	CvCapture* capture = cvCreateFileCapture("C:\\Users\\MR_Z\\Desktop\\1.avi");

	IplImage* frame;
	while (1)
	{
		//在cvCapture 分配記憶體,獲取capture的幀影象
		frame = cvQueryFrame(capture);
		//播放結束
		if (!frame)  break;

		//顯示圖片(視窗名,圖片指標)
		cvShowImage("Example2", frame);

		//按鍵得到esc=27,結束
		char c = cvWaitKey(33);
		if (c == 27) break;
	}
	//釋放記憶體空間
	cvReleaseCapture(&capture);

	//銷燬視窗,釋放記憶體(視窗標題)
	cvDestroyWindow("Example2");
}

2-3:視訊滾動條(測試未發現)

//播放AVI視訊
#include"cv.h"
#include "pch.h"
#include <iostream>
#include"highgui.h"
#include <opencv2/opencv.hpp>
using namespace cv;
int g_slider_positon = 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("C:\\Users\\MR_Z\\Desktop\\1.avi");//獲取avi檔案
	int frames = (int)cvGetCaptureProperty(g_capture, CV_CAP_PROP_FRAME_COUNT);//以幀數的形式來讀取影象
	if (frames != 0)
	{
		cvCreateTrackbar("Position", "Examples3", &g_slider_positon, frames, onTrackbarSlide);
	}


	IplImage* frame;
	while (1)
	{
		frame = cvQueryFrame(g_capture);
		if (!frame)  break;

		cvShowImage("Example3", frame);

		char c = cvWaitKey(33);
		if (c == 27) break;
	}

	cvReleaseCapture(&g_capture);
	cvDestroyWindow("Example3");
	return 0;

}

2-4:簡單變換

/*
 *   1.建視窗                     cvNamedWindow(視窗標題,大小設定);
 *   2.顯示原始影象               cvShowImage(視窗標題, 影象指標);
 *   3.處理影象                  cvSmooth(image, out, CV_GAUSSIAN, 5, 5);
 *   4.顯示處理影象
 *   5.釋放影象、釋放視窗        cvReleaseImage(影象地址);cvDestroyWindow(視窗標題);
 */

#include "cv.h"
#include "highgui.h"
#include "pch.h"

#include <opencv2/opencv.hpp>
using namespace cv;

void example2_4(IplImage* image)
{

	//建立兩個視窗
	cvNamedWindow("Example2_4-in", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Example2_4-out", CV_WINDOW_AUTOSIZE);

	//顯示影象
	cvShowImage("Example2_4-in", image);

	// Create an image to hold the smoothed output
	//建立一個用來儲存的影象(cvSize結構體的大小,畫素點的資料型別,通道數(RGB))
	IplImage* out = cvCreateImage(cvGetSize(image),	IPL_DEPTH_8U, 3);

	////平滑操作(輸入影象,輸出影象,平滑方法,高斯卷積*2)
	cvSmooth(image, out, CV_GAUSSIAN, 5, 5);

	//顯示處理後圖像
	cvShowImage("Example2_4-out", out);

	// 釋放影象記憶體
	cvReleaseImage(&out);

	//等待鍵入退出
	cvWaitKey(0);
    //釋放視窗記憶體
	cvDestroyWindow("Example2_4-in");
	cvDestroyWindow("Example2_4-out");

}

int main(int argc, char** argv)
{
    //讀取影象
	IplImage* img = cvLoadImage("C:\\Users\\MR_Z\\Desktop\\1.jpg");
	example2_4(img);
}

2-5:尺寸變換

IplImage* doPyrDown(IplImage* in, int filter = IPL_GAUSSIAN_5x5)
{
    assert(in->width % 2 == 0 && in->height % 2 == 0);
    IplImage* out =cvCreatImage(cvSize(in->width / 2,in->height / 2), in->depth, in->nChannels);
    /**********************************/
    cvPryDown(in, out);        //尺寸變化命令
    /**********************************/
    return out;
}

2-6:邊緣檢測

IplImage* doCanny(IplImage* in, double lowThresh, double highThresh, double aperture)
{
    if(in->cvChannels != 1) return 0;//不是單通道返回
    IplImage* out = cvCreateImage(cvSize(cvGetSize(in)), IPL_DEPTH_8U,1);

    /**********************************/
    cvCanny(in, out, lowThresh, highThresh, aperture);
    /**********************************/

    return out;
};

2-7/2-8:多樣操作

IplImage* out;
out = doPyrDown(in, IPL_GAUSSIAN_5x5);    //縮放操作
out = doPyrDown(out, IPL_GAUSSIAN_5x5);
out = doCanny(out, 10, 100,3);            //邊緣檢測

cvReleaseImage(&out);

2-9:攝像機讀入資料


#include "cv.h"
#include "highgui.h"
#include "pch.h"
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char** argv) {
	cvNamedWindow("Example2_9", CV_WINDOW_AUTOSIZE);
	CvCapture* capture;
	if (argc == 1) {
		capture = cvCreateCameraCapture(0);	//捕獲攝像頭影象內容
	}
	else {
		capture = cvCreateFileCapture("C:\\Users\\MR_Z\\Desktop\\vtest.avi");//視訊檔案
	}
	assert(capture != NULL);//用於除錯

	IplImage* frame;
	while (1) {
		frame = cvQueryFrame(capture);
		if (!frame) break;
		cvShowImage("Example2_9", frame);
		char c = cvWaitKey(10);
		if (c == 27) break;
	}
	cvReleaseCapture(&capture);
	cvDestroyWindow("Example2_9");
}

2-10寫入AVI視訊檔案


#include "cv.h"
#include "highgui.h"
#include "pch.h"
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char* argv[])
{
	CvCapture* capture = 0;
	capture = cvCreateFileCapture(argv[1]);
	if (!capture)	//沒有視訊檔案則返回
	{
		return -1;
	}
	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(
		argv[2],
		CV_FOURCC('M', 'J', 'P', 'G'),//用來指示編碼格式(4個字元)
		fps,
		size);
	//新建一個對數極座標影象
	IplImage* logpolar_frame = cvCreateImage(
		size,
		IPL_DEPTH_8U,
		3
	);
	//
	while ((bgr_frame = cvQueryFrame(capture)) != NULL) {
		//(源影象,目標影象,變化前影象中心的二維座標,變化尺度引數,插值方法)
		cvLogPolar(bgr_frame, logpolar_frame,
			cvPoint2D32f(bgr_frame->width / 2,
				bgr_frame->height / 2),
			40,
			CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS);//*****************
		cvWriteFrame(writer, logpolar_frame);
	}
	cvReleaseVideoWriter(&writer);
	cvReleaseImage(&logpolar_frame);
	cvReleaseCapture(&capture);
	return 0;
}