【OpenCV:從零到一】01:加載、修改、保存圖像

前言
這是我《OpenCV:從零到一》專欄的第一篇博客,想看跟多請戳這。
本文概要
本文主要涉及一下幾個函數
imread、imwrite (imgcodecs)
namedWindow、imshow、waitKey(highgui)
cvtColor(imgproc)
後面括號裏的是頭文件名(下面是官方文檔裏的主要模塊)
在這裏插入圖片描述
不難發現,命名規律:
im=image
proc=process
這樣一來頭文件的功能就好理解很多了。事不宜遲趕緊來看代碼吧

案例代碼
大概內容:圖像的加載、修改、保存。

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui_c.h>
#include <iostream>

using namespace cv;

int main() {
	//讀取圖像
	Mat src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");//創造一個Mat對象並且賦值給他
	if (src.empty()) {//Returns true if the array has no elements.
		printf("could nor load image...\n");
		return -1;
	}
	namedWindow("lena", CV_WINDOW_AUTOSIZE);//創造一個窗口
	imshow("lena", src);//把圖片展示得到窗口中
	//修改圖像
	namedWindow("result", CV_WINDOW_AUTOSIZE);//再創造一個窗口
	Mat result;//創造一個空的圖片對象
	cvtColor(src, result, CV_BGR2HLS);//修改顏色模型
	imshow("result", result);//Displays an image in the specified window.
	//保存圖像
	imwrite("D:\\86186\\Documents\\opencv\\test_lena1.jpg", result);//Saves an image to a specified file.
	waitKey(6000);//Waits for a pressed key.
	return 0;
}

解析及注意事項

  • 學習過數字圖像處理的話我們知道所有圖片都會被表示爲一個矩陣,而這個矩陣對應在OpenCV中則是Mat對象
  • Mat對象顧名思義Matrix 矩陣、母體(著名的黑客帝國英文原名就是matrix,有沒有因爲這部電影入行的?反正我不是哈哈啊)
  • Mat有超級多的成員函數,當然大多說成員函數單憑名字大概就知道是什麼功能了
  • imread、imwrite是一對函數,一個從電腦讀取,一個寫入電腦。imshow和他們的形式很像,但是他來自另一個頭文件,而且他前面必須要有一個namedWindow創造窗口,後面也必須要跟一個waitKey關閉窗口,這三個可以說是輸出三板斧了,那麼前面兩個姑且可以命名爲讀寫雙子星吧(幼稚鬼
  • imread第一個參數是圖片在計算機中的名稱(url),第二個參數可以改變加載類型(如彩色圖片灰色來顯示),沒有特別需要就不用加了。
  • imread讀取的圖像類型取決於圖像內容而不是拓展名
  • cvtColor用於改變圖像顏色空間(比如灰色圖像變爲紅色圖像),cvt=convert。第一個參數是原Mat對象,第二個參數是結果Mat對象,第三個參數是顏色空間參數。
  • namedWindow第一個參數是窗口名,不能與其他窗口重複,否則語句無效。第二個參數是窗口類型,CV_WINDOW_AUTOSIZE類型的意思是自動大小,用戶不能改變。
  • imshow第一個參數是窗口名,輸出到指定的窗口,imshow後面沒有waitkey的話不會展示圖片(第一次寫程序的時候我就嘗試使用system(“pause”)來停止程序,後面發現圖像沒有顯示,當時把他歸結爲玄學…)第二個參數是mat對象
  • waitKey用來監聽鍵盤事件,參數大於0的時候則在delay ms內監聽鍵盤,若無輸入則返回-1,若參數值小於等於0則無限等待下去,通常用0。

全註釋代碼

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui_c.h>
#include <iostream>

using namespace cv;

int main() {
	//mat對象是一個 n-dimensional dense array class 擁有衆多成員函數
	Mat src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");//創造一個Mat對象並且賦值給他
	/*
	imread功能是加載圖像文件成爲一個Mat對象(Loads an image from a file.)
	第一個參數表示圖像文件名稱
	第二個參數,表示加載的圖像是什麼類型:
		IMREAD_UNCHANGED (<0) 表示加載原圖,不做任何改變
		IMREAD_GRAYSCALE(0)表示把原圖作爲灰度圖像加載進來
		IMREAD_COLOR(> 0) 表示把原圖作爲RGB圖像加載進來
	The function imread loads an image from the specified file and returns it.
	If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format),the function returns an empty matrix ( Mat::data==NULL ).
	如果圖像因爲 找不到文件\不正確權限\非法格式 則返回一個空矩陣
	The function determines the type of an image by the content, not by the file extension.
	該圖像類型取決於內容而不是拓展名
	*/

	if (src.empty()) {//Returns true if the array has no elements.
		printf("could nor load image...\n");
		return -1;
	}
	namedWindow("lena", CV_WINDOW_AUTOSIZE);//創造一個窗口
	//If a window with the same name already exists, the function does nothing.如果窗口名重複了則該語句不會生效
	/*
	You can call cv::destroyWindow or cv::destroyAllWindows to close the window and de-allocate any associated memory usage.
	For a simple program, you do not really have to call these functions
	because all the resources and windows of the application are closed automatically by the operating system upon exit.
	*/
	imshow("lena", src);//把圖片展示得到窗口中
	//This function should be followed by cv::waitKey function which displays the image for specified milliseconds.Otherwise, it won't display the image.
	//該函數後面必須要跟一個waitkey函數,否則不會顯示圖片
	namedWindow("result", CV_WINDOW_AUTOSIZE);//再創造一個窗口
	Mat result;//創造一個空的圖片對象
	cvtColor(src, result, CV_BGR2HLS);//修改顏色空間Converts an image from one color space to another.
	//顏色空間命名還是一些規範的,這裏展示不展開但可以看出些東西如BGR(RGB在opencv存儲順序) HLS顏色模型.....
	imshow("result", result);//Displays an image in the specified window.
	imwrite("D:\\86186\\Documents\\opencv\\test_lena1.jpg", result);//Saves an image to a specified file.
	//和imread是一對的,使用方法和注意事項差不多

	waitKey(6000);//Waits for a pressed key.
	/*
	waitKey(delay);//delay爲int型
	waitKey官方原文:
	The function waitKey waits for a key event infinitely (when delay≤0 ) or for delay milliseconds, when it is positive.
	該函數用來監聽鍵盤事件,如果delay大於0則等待delay ms,否則無限等待下去(通常用0)
	Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms,
	因爲OS(操作系統)切換線程需要時間,所以函數不是等待準確的delay ms,而是至少等待delay ms
	depending on what else is running on your computer at that time.
	這取決於你當時運行的電腦
	It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.
	函數返回輸入鍵的ASCII碼或者-1(如果你沒有輸入任何鍵在指定時間過去之前)
	*/
	return 0;
}

翻譯筆記
n-dimensional dense array class 多維密集數組類
specified file 指定的文件
improper permissions 不正確限權
file extension 文件後綴名
infinitely 無限的
switching threads 切換線程
before the specified time had elapsed 在指定的時間過去之前

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章