使用OpenCV python模块读取图像并将其另存为灰度系统
In Python, we can use an OpenCV library named cv2. Python does not come with cv2, so we need to install it separately.
在Python中,我們可以使用名為cv2的OpenCV庫 。 Python沒有隨附cv2 ,因此我們需要單獨安裝它。
For Windows:
對于Windows:
pip install opencv-pythonFor Linux:
對于Linux:
sudo apt-get install python-opencvIn the below given program, we are using following three functions:
在下面給出的程序中,我們使用以下三個功能:
imread():
imread():
It takes an absolute path/relative path of your image file as an argument and returns its corresponding image matrix.
它以圖像文件的絕對路徑/相對路徑作為參數,并返回其對應的圖像矩陣。
imshow():
imshow():
It takes the window name and image matrix as an argument in order to display an image in the display window with a specified window name.
它以窗口名稱和圖像矩陣為參數,以便在具有指定窗口名稱的顯示窗口中顯示圖像。
cv2.cvtcolor():
cv2.cvtcolor():
It takes image matrix and a flag for changing color-space from one color space to another(in this case we are using BGR2GRAY color-space conversion) and returns the newly converted image matrix.
它需要圖像矩陣和用于將顏色空間從一種顏色空間更改為另一種顏色的標志(在這種情況下,我們使用BGR2GRAY顏色空間轉換),并返回新轉換的圖像矩陣。
Imwrite() :
Imwrite():
It takes an absolute path/relative path (of the desired location where you want to save a modified image) and image matrix as an argument.
它采用絕對路徑/相對路徑(要保存修改后圖像的所需位置)和圖像矩陣作為參數。
使用OpenCV python模塊讀取圖像并將其另存為灰度系統的Python代碼 (Python code to read an image and save it as grayscale system using OpenCV python module)
# open-cv library is installed as cv2 in python # import cv2 library into this program import cv2# read an image using imread() function of cv2 # we have to pass only the path of the image img = cv2.imread(r'C:/Users/user/Desktop/pic6.jpg')# displaying the image using imshow() function of cv2 # In this : 1st argument is name of the frame # 2nd argument is the image matrix cv2.imshow('original image',img)# converting the colourfull image into grayscale image # using cv2.COLOR_BGR2GRAY argument of # the cvtColor() function of cv2 # in this : # ist argument is the image matrix # 2nd argument is the attribute gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)# save the image at specified location cv2.imwrite(r"image\gray_img.jpg",gray_img)Output
輸出量
翻譯自: https://www.includehelp.com/python/read-an-image-and-save-it-as-grayscale-system-using-opencv-python-module.aspx
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的使用OpenCV python模块读取图像并将其另存为灰度系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哈尔滨治精子活力低最好的医院推荐
- 下一篇: python整数转换字符串_使用Pyth