您好我正在嘗試將一系列影像轉換為陣列,然后將 RGB 轉換為灰度。
在我的作業檔案夾中,我有 x 個 frames.png,我需要讀取陣列中的所有這些幀,然后將每個幀 (RGB) 轉換為灰度。
對于一幀,我的代碼是:
import numpy as np
import cv2 as cv
from PIL import Image
# Read image
Image = cv.imread('frame0.png')
# RGB to Gray Scale
GS = cv.cvtColor(Image, cv.COLOR_BGR2GRAY)
th, Gray = cv.threshold(GS, 128, 192, cv.THRESH_OTSU)
任何的想法?
uj5u.com熱心網友回復:
您可以使用 os 從檔案夾中讀取檔案。使用“endswith”功能,您可以提取檔案格式并將它們全部提取。
這是一個作業代碼
import numpy as np
import cv2 as cv
import os
for file in os.listdir("/mydir"): # images folder path
if file.endswith((".png",".jpg")): # you can add formats
img = cv.imread(file)
GS = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
th, Gray = cv.threshold(GS, 128, 192, cv.THRESH_OTSU)
cv.imwrite("converted-" file,Gray)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471342.html
