python opencv cv2.cvtColor()方法(将图像从一种颜色空间转换为另一种颜色空间)(转换成灰度图)
生活随笔
收集整理的這篇文章主要介紹了
python opencv cv2.cvtColor()方法(将图像从一种颜色空间转换为另一种颜色空间)(转换成灰度图)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
def cvtColor(src, code, dst=None, dstCn=None): # real signature unknown; restored from __doc__"""cvtColor(src, code[, dst[, dstCn]]) -> dst. @brief Converts an image from one color space to another.將圖像從一種顏色空間轉換為另一種顏色空間。. . The function converts an input image from one color space to another. In case of a transformation. to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note. that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the. bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue. component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and. sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.該功能將輸入圖像從一種顏色空間轉換為另一種顏色空間。 在從RGB顏色空間轉換的情況下,應明確指定通道的順序(RGB或BGR)。 請注意,OpenCV中的默認顏色格式通常稱為RGB,但實際上是BGR(字節是相反的)。 因此,標準(24位)彩色圖像中的第一個字節將是8位藍色分量,第二個字節將是綠色分量,第三個字節將是紅色分量。 第四,第五和第六個字節將是第二個像素(藍色,然后是綠色,然后是紅色),依此類推。. . The conventional ranges for R, G, and B channel values are:. - 0 to 255 for CV_8U images. - 0 to 65535 for CV_16U images. - 0 to 1 for CV_32F imagesR,G和B通道值的常規范圍是:。 -CV_8U圖像為0至255。 -CV_16U圖像為0至65535。 -CV_32F圖像為0到1. . In case of linear transformations, the range does not matter. But in case of a non-linear. transformation, an input RGB image should be normalized to the proper value range to get the correct. results, for example, for RGB \f$\rightarrow\f$ L\*u\*v\* transformation. For example, if you have a. 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will. have the 0..255 value range instead of 0..1 assumed by the function. So, before calling #cvtColor ,. you need first to scale the image down:. @code. img *= 1./255;. cvtColor(img, img, COLOR_BGR2Luv);. @endcode. If you use #cvtColor with 8-bit images, the conversion will have some information lost. For many. applications, this will not be noticeable but it is recommended to use 32-bit images in applications. that need the full range of colors or that convert an image before an operation and then convert. back.. 在線性變換的情況下,范圍無關緊要。 但是在進行非線性變換的情況下,應將輸入的RGB圖像規范化為適當的值范圍以獲得正確的結果,例如,對于RGB \ f $ \ rightarrow \ f $ L \ * u \ * v \ * 轉型。 例如,如果您有一個32位浮點圖像直接從8位圖像轉換而沒有任何縮放,則它將具有0..255的值范圍,而不是該函數假定的0..1。 因此,在調用#cvtColor之前,您需要先按比例縮小圖像:。 @代碼。 img * = 1./255;。 cvtColor(img,img,COLOR_BGR2Luv);。 @endcode。 如果將#cvtColor與8位圖像一起使用,轉換將丟失一些信息。 對于許多應用程序來說,這不會引起注意,但是建議在需要全彩范圍的應用程序中使用32位圖像,或者在進行操作之前先轉換圖像然后再轉換回來。. If conversion adds the alpha channel, its value will set to the maximum of corresponding channel. range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F.如果轉換添加了Alpha通道,則其值將設置為相應通道范圍的最大值:CV_8U為255,CV_16U為65535,CV_32F為1。. . @param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), or single-precision. floating-point.輸入圖像:8位無符號,16位無符號(CV_16UC ...)或單精度浮點。. @param dst output image of the same size and depth as src.輸出圖像:與src具有相同大小和深度。. @param code color space conversion code (see #ColorConversionCodes).顏色空間轉換代碼(請參見#ColorConversionCodes)。. @param dstCn number of channels in the destination image; if the parameter is 0, the number of the. channels is derived automatically from src and code.目標圖像中的通道數; 如果參數為0,則通道數自動從src和code得出。. . @see @ref imgproc_color_conversions"""pass
示例:
# -*- coding: utf-8 -*- """ @File : 191213_測試_閾值分割.py @Time : 2019/12/13 15:14 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import cv2 as cv# 【讀取圖像】 image = cv.imread('feiji.jpg') # 【將圖像灰度化】 image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) # 【顯示圖像】 cv.imshow('win', image) cv.waitKey(0)原圖:
處理后:
參考文章:python opencv ColorConversionCodes
總結
以上是生活随笔為你收集整理的python opencv cv2.cvtColor()方法(将图像从一种颜色空间转换为另一种颜色空间)(转换成灰度图)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10下输入法突然变成繁体了怎么设置
- 下一篇: python 为什么要用astype()