opencv api 筆記

前言
opencv在圖像處理中使用廣泛,許多常見的應(yīng)用場景例如人臉識別,車牌識別等都是基于opencv開發(fā)的。本文是學(xué)習(xí)opencv api的一些筆記。
感興趣的朋友歡迎加入學(xué)習(xí)小組QQ群: 193765960

版權(quán)歸作者所有,如有轉(zhuǎn)發(fā),請注明文章出處:https://xiaodanchen.github.io/archives/

《openCV中文網(wǎng)站》

Mat-基本圖像容器

Mat官方解釋
教程
基本上講 Mat 是一個(gè)類,由兩個(gè)數(shù)據(jù)部分組成:矩陣頭(包含矩陣尺寸,存儲(chǔ)方法,存儲(chǔ)地址等信息)和一個(gè)指向存儲(chǔ)所有像素值的矩陣(根據(jù)所選存儲(chǔ)方法的不同矩陣可以是不同的維數(shù))的指針。矩陣頭的尺寸是常數(shù)值,但矩陣本身的尺寸會(huì)依圖像的不同而不同,通常比矩陣頭的尺寸大數(shù)個(gè)數(shù)量級。因此,當(dāng)在程序中傳遞圖像并創(chuàng)建拷貝時(shí),大的開銷是由矩陣造成的,而不是信息頭。OpenCV是一個(gè)圖像處理庫,囊括了大量的圖像處理函數(shù),為了解決問題通常要使用庫中的多個(gè)函數(shù),因此在函數(shù)中傳遞圖像是家常便飯。同時(shí)不要忘了我們正在討論的是計(jì)算量很大的圖像處理算法,因此,除非萬不得已,我們不應(yīng)該拷貝 大 的圖像,因?yàn)檫@會(huì)降低程序速度。

為了搞定這個(gè)問題,OpenCV使用引用計(jì)數(shù)機(jī)制。其思路是讓每個(gè) Mat 對象有自己的信息頭,但共享同一個(gè)矩陣。這通過讓矩陣指針指向同一地址而實(shí)現(xiàn)。而拷貝構(gòu)造函數(shù)則 只拷貝信息頭和矩陣指針 ,而不拷貝矩陣。

但某些時(shí)候你仍會(huì)想拷貝矩陣本身(不只是信息頭和矩陣指針),這時(shí)可以使用函數(shù)** clone() **或者 copyTo()

  • OpenCV函數(shù)中輸出圖像的內(nèi)存分配是自動(dòng)完成的(如果不特別指定的話)。
  • 使用OpenCV的C++接口時(shí)不需要考慮內(nèi)存釋放問題。
  • 賦值運(yùn)算符和拷貝構(gòu)造函數(shù)( ctor )只拷貝信息頭。
  • 使用函數(shù) clone() 或者 copyTo() 來拷貝一副圖像的矩陣。

Mat::ptr

ptr API文檔
Returns a pointer to the specified matrix row
返回指向指定矩陣行的指針

Mat::channels

channels API文檔
Returns the number of matrix channels.
對channels(通道)的理解可能會(huì)比較抽象一些,可以參考下列鏈接:
《圖像的通道(channels)問題》

API

imread

imread API文檔
imread:讀取圖片文件,并按照一定的格式將其返回(Mat對象)
C++: Mat imread(const string& filename, int flags=1 )
Python: cv2.imread(filename[, flags]) → retval
C: IplImage* cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
C: CvMat* cvLoadImageM(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR )
Python: cv.LoadImage(filename, iscolor=CV_LOAD_IMAGE_COLOR) → None
Python: cv.LoadImageM(filename, iscolor=CV_LOAD_IMAGE_COLOR) → None

Parameters:

  • filename – Name of file to be loaded.
  • flags –Flags specifying the color type of a loaded image:
    • CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
    • CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
    • CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
    • 0 Return a 3-channel color image.
      Note In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.

  • =0 Return a grayscale image.
  • <0 Return the loaded image as is (with alpha channel).

cvtColor

cvtColor API文檔
對圖片進(jìn)行處理,從一種顏色空間轉(zhuǎn)換到另一種顏色空間(Converts an image from one color space to another.)

  • RGB <--> GRAY ( CV_BGR2GRAY, CV_RGB2GRAY, CV_GRAY2BGR, CV_GRAY2RGB )
  • **RGB <--> CIE XYZ.Rec 709 with D65 white point **( CV_BGR2XYZ, CV_RGB2XYZ, CV_XYZ2BGR, CV_XYZ2RGB )
  • RGB <--> YCrCb JPEG (or YCC) ( CV_BGR2YCrCb, CV_RGB2YCrCb, CV_YCrCb2BGR, CV_YCrCb2RGB )
  • RGB <--> HSV ( CV_BGR2HSV, CV_RGB2HSV, CV_HSV2BGR, CV_HSV2RGB )
  • RGB <--> HLS ( CV_BGR2HLS, CV_RGB2HLS, CV_HLS2BGR, CV_HLS2RGB )
  • RGB <--> CIE Lab* **( CV_BGR2Lab, CV_RGB2Lab, CV_Lab2BGR, CV_Lab2RGB )
  • RGB <--> CIE Luv* **( CV_BGR2Luv, CV_RGB2Luv, CV_Luv2BGR, CV_Luv2RGB )
  • Bayer -> RGB ( CV_BayerBG2BGR, CV_BayerGB2BGR, CV_BayerRG2BGR, CV_BayerGR2BGR, CV_BayerBG2RGB, CV_BayerGB2RGB, CV_BayerRG2RGB, CV_BayerGR2RGB )

split

split API文檔
split函數(shù)的主要功能是把一個(gè)彩色圖像分割成3個(gè)通道,方便進(jìn)一步的圖像處理,具體說明如下:
split Divides a multi-channel array into several single-channel arrays.
C++: void split(const Mat& mtx, Mat* mv)
C++: void split(const Mat& mtx, vector& mv)

merge

merge API文檔
merge可以實(shí)現(xiàn)與split相反的操作,簡單說明如下:
merge Composes a multi-channel array from several single-channel arrays.
C++: void merge(const Mat* mv, size_t count, OutputArray dst)
C++: void merge(const vector& mv, OutputArray dst)

mixChannels

mixChannels API文檔

equalizeHist

equalizeHist API文檔
直方圖均衡化
將灰度圖進(jìn)行直方圖均衡化(直方圖均衡化是通過拉伸像素強(qiáng)度分布范圍來增強(qiáng)圖像對比度的一種方法)

C++: void equalizeHist(InputArray src, OutputArray dst)
Python: cv2.equalizeHist(src[, dst]) → dst
C: void cvEqualizeHist(const CvArr* src, CvArr* dst)
Parameters:
src – Source 8-bit single channel image.
dst – Destination image of the same size and type as src .

原理:
1,Calculate the histogram H for src .
2,Normalize the histogram so that the sum of histogram bins is 255.
3,Compute the integral of the histogram:


求和

4,Transform the image using H' as a look-up table: dst(x,y) = H'(src(x,y))

The algorithm normalizes the brightness and increases the contrast of the image.

threshold

threshold API文檔
閾值化

  • 最簡單的圖像分割的方法。
  • 應(yīng)用舉例:從一副圖像中利用閾值分割出我們需要的物體部分(當(dāng)然這里的物體可以是一部分或者整體)。這樣的圖像分割方法是基于圖像中物體與背景之間的灰度差異,而且此分割屬于像素級的分割。
  • 為了從一副圖像中提取出我們需要的部分,應(yīng)該用圖像中的每一個(gè)像素點(diǎn)的灰度值與選取的閾值進(jìn)行比較,并作出相應(yīng)的判斷。(注意:閾值的選取依賴于具體的問題。即:物體在不同的圖像中有可能會(huì)有不同的灰度值。
  • 一旦找到了需要分割的物體的像素點(diǎn),我們可以對這些像素點(diǎn)設(shè)定一些特定的值來表示。(例如:可以將該物體的像素點(diǎn)的灰度值設(shè)定為:‘0’(黑色),其他的像素點(diǎn)的灰度值為:‘255’(白色);當(dāng)然像素點(diǎn)的灰度值可以任意,但最好設(shè)定的兩種顏色對比度較強(qiáng),方便觀察結(jié)果)

C++: double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
Python: cv2.threshold(src, thresh, maxval, type[, dst]) → retval, dst


閾值函數(shù)的5種類型

erode

erode API文檔
腐蝕操作
C++: void erode(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue() )
Python: cv2.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst

dilate

dilate API文檔
膨脹操作
C++: void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue() )
Python: cv2.dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst

open

開運(yùn)算:開運(yùn)算是通過先對圖像腐蝕再膨脹實(shí)現(xiàn)的,能夠排除小團(tuán)塊物體(假設(shè)物體較背景明亮)。
dst = open(src,element) = dilate( erode(src, element) )

開操作

close

閉運(yùn)算:閉運(yùn)算是通過先對圖像膨脹再腐蝕實(shí)現(xiàn)的,能夠排除小型黑洞(黑色區(qū)域)。
dst = close(src,element) = erode( dilate(src, element) )

閉操作

形態(tài)梯度(Morphological Gradient)

膨脹圖與腐蝕圖之差,能夠保留物體的邊緣輪廓。
dst = morph_grad(src,element) = dilate(src, element) - erode(src, element)

形態(tài)梯度

頂帽(Top Hat)

原圖像與開運(yùn)算結(jié)果圖之差。
**dst = tophat(src,element) = src - open(src, element) **


頂帽

黑帽(Black Hat)

閉運(yùn)算結(jié)果圖與原圖像之差。
dst = blackhat(src,element) = close(src, element) - src

頂帽

findcontours

findcontours API
在圖像中尋找輪廓

Sobel算子

  • Sobel 算子是一個(gè)離散微分算子 (discrete differentiation operator)。 它用來計(jì)算圖像灰度函數(shù)的近似梯度。
  • Sobel 算子結(jié)合了高斯平滑和微分求導(dǎo)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容