我問這個問題是因為我有一個關于https://pyimagesearch.com/2017/06/05/computing-image-colorfulness-with-opencv-and-python/的建議
在這里,他們參考了 Hasler 和 Süsstrunk 2003 年的論文,《測量自然影像中的色彩》。他們正在使用 NumPy 執行計算,但我有點擔心計算影像色彩的速度。
原始方法
from imutils import build_montages
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
def image_colorfulness(image):
# split the image into its respective RGB components
(B, G, R) = cv2.split(image.astype("float"))
# compute rg = R - G
rg = np.absolute(R - G)
# compute yb = 0.5 * (R G) - B
yb = np.absolute(0.5 * (R G) - B)
# compute the mean and standard deviation of both `rg` and `yb`
(rbMean, rbStd) = (np.mean(rg), np.std(rg))
(ybMean, ybStd) = (np.mean(yb), np.std(yb))
# combine the mean and standard deviations
stdRoot = np.sqrt((rbStd ** 2) (ybStd ** 2))
meanRoot = np.sqrt((rbMean ** 2) (ybMean ** 2))
# derive the "colorfulness" metric and return it
return stdRoot (0.3 * meanRoot)
uj5u.com熱心網友回復:
我嘗試通過使用OpenCV函式來加快速度,并在我的機器上比你的Numba版本提高了 6.5 倍:
#!/usr/bin/env python3
import numpy as np
import cv2
from numba import *
def me(image,B,G,R):
# Use OpenCV methods wherever possible
rg = cv2.absdiff(R,G)
rgMean, rgStd = cv2.meanStdDev(rg)
avg = cv2.addWeighted(R, 0.5, G, 0.5, 0)
yb = cv2.absdiff(avg, B)
ybMean, ybStd = cv2.meanStdDev(yb)
# combine the mean and standard deviations
stdRoot = np.sqrt((rgStd ** 2) (ybStd ** 2))
meanRoot = np.sqrt((rgMean ** 2) (ybMean ** 2))
# derive the "colorfulness" metric and return it
return stdRoot (0.3 * meanRoot), rgMean, rgStd, ybMean, ybStd
@njit(parallel=True)
def image_colorfulness(image,B,G,R):
# compute rg = R - G
rg = np.absolute(R - G)
# compute yb = 0.5 * (R G) - B
yb = np.absolute(0.5 * (R G) - B)
# compute the mean and standard deviation of both `rg` and `yb`
rgMean, rgStd = np.mean(rg), np.std(rg)
ybMean, ybStd = np.mean(yb), np.std(yb)
# combine the mean and standard deviations
stdRoot = np.sqrt((rgStd ** 2) (ybStd ** 2))
meanRoot = np.sqrt((rgMean ** 2) (ybMean ** 2))
# derive the "colorfulness" metric and return it
return stdRoot (0.3 * meanRoot), rgMean, rgStd, ybMean, ybStd
image = cv2.imread('Oscars-selfie_620x349.png')
# OP's original method
B, G, R = cv2.split(image.astype("float"))
score, *others = image_colorfulness(image,B=B,G=G,R=R)
print(score, others)
%timeit image_colorfulness(image,B=B,G=G,R=R)
# Using OpenCV and avoiding promotion to float
B, G, R = cv2.split(image)
score, *others = me(image,B=B,G=G,R=R)
print(score, others)
%timeit me(image,B=B,G=G,R=R)
原始方法和 OpenCV 方法的時序
1.11 ms ± 43.7 μs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
171 μs ± 1.49 μs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505709.html
下一篇:如何提取給定影像中的白環
