簡體   English   中英

同一張圖像的2個灰度圖像有何不同?

[英]How are the 2 gray scale images of the same image different?

將圖像讀取為灰度圖像並將3通道圖像轉換為灰度圖像之間有什么區別?

為了清楚起見,如果我閱讀如下圖像:

gray_1 = cv2.imread("model.jpg", 0)

colored = cv2.imread("model.jpg")

gray_2 = cv2.cvtColor(colored, cv2.COLOR_RGB2GRAY)

print(gray_1.shape) #(1152,1536)
print(gray2.shape)  #(1152, 1536)

現在,如果我檢查兩個numpy數組gray_1gray_2相等性,則它們不相等。

np.array_equal(gray_1, gray_2)

上面的語句返回False 這是為什么? gray_1gray_2什么gray_2

請注意,該答案沒有陳述,也從未說過,灰度加載與彩色加載以及隨后轉換為灰度之間沒有區別。 它僅指出:

1)OP需要使用cv2.COLOR_BGR2GRAY而不是cv2.COLOR_RGB2GRAY進行正確的比較,並且

2)對於准備使用有損JPEG來存儲其圖像的任何人,差異可能微不足道。


OpenCV本機以BGR順序存儲,因此真正的比較實際上是:

gray_2 = cv2.cvtColor(colored, cv2.COLOR_BGR2GRAY)

與使用cv2.COLOR_RGB2GRAY


量化由於直接以灰度加載而不是彩色加載然后進行轉換的結果,這兩個圖像的“差異”可能會有所幫助,因此我計算了以下統計信息:

  • 絕對誤差 -只是像素數不同

  • 峰值絕對誤差 -任何兩個對應像素之間的最大絕對差

  • 平均絕對誤差 -相應像素之間的平均絕對差

  • 均方誤差 -相應像素之間的均方差

  • 均方根誤差 -上面的平方根

如果您使用標准的512x512 Lena圖像,並將直接加載為灰度的圖像與加載為彩色並隨后轉換的圖像進行比較,則會得到以下結果:

AE: 139
PAE: 4
MAE: 0.00072479248046875
MSE: 0.001220703125
RMSE: 0.034938562148434216

因此,在262,144個像素中, 只有 139個像素存在差異,並且任何兩個像素之間的最大差異在0..255范圍內僅為4,即小於1.6%

相比之下,如果將保存為JPEG質量90和質量89的Lena圖像進行比較,則會得到以下差異:

AE: 158575
PAE: 13
MAE: 0.9766883850097656
MSE: 2.2438392639160156
RMSE: 1.4979450136490378

因此,我是說JPEG質量的1%差異會導致100倍的像素相差3倍之多。 因此,您選擇將數據存儲為JPEG的事實比兩種灰度轉換方法的差異具有更大的影響,並且,如果您真正關心准確性,則應該使用PNG / TIFF / PPM或其他無損格式。


#!/usr/bin/env python3

import math
import numpy as np
from PIL import Image
import cv2

def compare(im1, im2, metric):
   """
   Compare two images in terms of given metric.
   'AE'   Absolute Error. Simply the number of pixels that are different.
   'PAE'  Peak Absolute Error. The largest absolute difference between two corresponding pixels.
   'MAE'  Mean Absolute Error. The average difference between correspondng pixels.
   'MSE'  Mean Squared Error.
   'RMSE' Root Mean Squared Error.
   """

   assert(im1.shape==im2.shape)

   im1 = np.ravel(im1).astype(np.int64)
   im2 = np.ravel(im2).astype(np.int64)

   if metric == 'AE':
      # Return count of pixels that differ
      res = (im1 != im2).sum()
      return res

   if metric == 'PAE':
      # Return largest absolute difference
      res = np.abs((im1-im2)).max()
      return res

   if metric == 'MAE':
      # Return average absolute difference between corresponding pixels
      res = np.abs((im1-im2)).mean()
      return res

   # Calculate mean squared difference between corresponding pixels
   res = ((im1-im2)*(im1-im2)).mean()

   if metric == 'MSE':
      return res

   if metric == 'RMSE':
      return math.sqrt(res)


# Uncomment any one of the three following blocks

# Create greyscale image 640x480 filled with mid-grey
#w,h = 640,480
#im1 = np.zeros([h,w,1], dtype=np.uint8) + 128
#im2 = im1.copy()
#im2[1,1]=7

# Load first image as greyscale, second as colour but then convert to greyscale afterwards
#gray_1   = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
#coloured = cv2.imread('lena.jpg',cv2.IMREAD_COLOR)
#gray_2   = cv2.cvtColor(coloured, cv2.COLOR_BGR2GRAY)

# Load Lena in 89 and 90 JPEG quality
gray_1   = cv2.imread('lena89.jpg',cv2.IMREAD_GRAYSCALE)
gray_2   = cv2.imread('lena90.jpg',cv2.IMREAD_GRAYSCALE)

res = compare(gray_1, gray_2, 'AE')
print('AE: {}'.format(res))
res = compare(gray_1, gray_2, 'PAE')
print('PAE: {}'.format(res))
res = compare(gray_1, gray_2, 'MAE')
print('MAE: {}'.format(res))
res = compare(gray_1, gray_2, 'MSE')
print('MSE: {}'.format(res))
res = compare(gray_1, gray_2, 'RMSE')
print('RMSE: {}'.format(res))

OpenCV在imload功能中使用內部編解碼器。 但是對於cvtColor,它使用以下公式:

RGB[A] to Gray:Y←0.299⋅R + 0.587⋅G + 0.114⋅B

這是一種已知的行為(但看起來像個錯誤:))。 您可以在這里這里跟蹤其歷史。

另一個答案中提出的COLOR_BGR2GRAY將不起作用:

In [6]: gray1 = cv2.imread('1.png', 0)

In [7]: col = cv2.imread('1.png')

In [8]: gray2 = cv2.cvtColor(col, cv2.COLOR_RGB2GRAY)

In [10]: np.array_equal(gray1, gray2)
Out[10]: False

In [16]: gray3 = cv2.cvtColor(col, cv2.COLOR_BGR2GRAY)

In [17]: np.array_equal(gray1, gray3)
Out[17]: False

TLDR:他們不同,沒關系。 只是忍受它。

暫無
暫無

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

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