簡體   English   中英

如何在Python中使圖像去飽和?

[英]How to desaturate the image in Python?

我正在經歷將灰色圖像轉換為彩色圖像的代碼,但是在此之前,代碼在給定的代碼行的幫助下將輸入圖像轉換為去飽和圖像:

def load_image(path):
    img = imread(path)
    # crop image from center
    short_edge = min(img.shape[:2])
    yy = int((img.shape[0] - short_edge) / 2)
    xx = int((img.shape[1] - short_edge) / 2)
    crop_img = img[yy : yy + short_edge, xx : xx + short_edge]
    # resize to 224, 224
    img = skimage.transform.resize(crop_img, (224, 224))
    # desaturate image
    return (img[:,:,0] + img[:,:,1] + img[:,:,2]) / 3.0

我在此特定行中遇到錯誤,錯誤讀取為:

return (img[:,:,0] + img[:,:,1] + img[:,:,2]) / 3.0
IndexError: too many indices for array

請幫助我解決我面臨的問題。

您的大多數代碼都可以使用2D(灰度)或3D(RGB)陣列。 但是,最后一行明確需要3D陣列。 您可以添加條件以通過2D數組,因為代碼只是將通道平均在一起:

if img.ndim < 3:
    return img
# return average

另外,您可以在知道函數2的二維數組后立即在函數啟動時引發更多信息錯誤。 做任何有意義的事情。

這不是使圖像飽和的正確方法。 請使用skimage.color.rgb2gray ,它可以skimage.color.rgb2gray地應用於2D圖像(已經飽和)。

即,將最后一行更改為:

return color.rgb2gray(img)

並在腳本的前面部分from skimage import color添加。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM