本文介紹了Python中兩個圖像的比較的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想使用Python比較兩個圖像,但我不熟悉此語言。
我有兩個大小相同的圖像。我必須創建一個包含兩個圖像逐個像素差異的數組。最后,我必須以浮點數的形式計算數組的所有值??之和的平均值。
我可以使用Processing執行此操作,但無法使用Python執行此操作。
如果兩個圖像相同,則結果顯然為0。
我想將此代碼轉換為Python(最重要的是最終平均值的值)。
PImage img,img2;
int threshold = 64;
void setup(){
//size(600,400);
img = loadImage(args[0]);
img2 = loadImage(args[1]);
println(comparison(img,img2));
exit();
}
PImage binarization(PImage img,int threshold){
for(int i = 0; i < img.pixels.length; i++){
if(green(img1.pixels[i]) > threshold) img.pixels[i] = color(255);
else img.pixels[i] = color(0);
}
return img;
}
float comparison(PImage img, PImage img2){
img.filter(GRAY);
img2.filter(GRAY);
img = binarazation(img,threshold);
img2 = binarization(img2,threshold);
int array[] = new int[img.pixels.length];
for(int i = 0; i < img.pixels.length; i++){
array[i] = int( abs(green(img.pixels[i]) - green(img2.pixels[i])));
}
float average = 0;
for(int i = 0; i < img.pixels.length; i++){
average+= array[i];
}
average = average/img.pixels.length;
return average;
}
編輯:
非常感謝!
我之前發布的比較函數不是真的正確
它實際上應該與另一幅圖像(應用了Canny算法)一起顯示(在轉換為灰度之后)
如何修改elgordorafiki發布的比較函數?
要使用的Canny算法如下:
import cv2
import numpy as np
from matplotlib import pyplot as plt
1
img = cv2.imread ('img', 0)
edges = cv2.Canny (img, 100,110)
plt.subplot (2,1,1), plt.imshow (img, cmap = 'gray')
plt.title ('Original Image'), plt.xticks ([]), plt.yticks ([])
plt.subplot (2,1,2), plt.imshow (edges, cmap = 'gray')
plt.title ('Canny Edge Detection'), plt.xticks ([]), plt.yticks ([])
plt.show ()
Python
正如@martineau建議的那樣,推薦答案圖像庫是一個不錯的選擇。我個人也認為可以使用Numpy和matplotlib作為替代。Python的好處是您可以使用數組對整個圖像進行操作,而不是使用for循環,這樣看起來更好,速度也更快。
作為示例,我很快將您的代碼移植到Python(不確定過濾在那里做什么以及您的閾值有什么值,但睡覺應該大致相同)
我也有一些疑問(您將二進制化后的值設置為255,這意味著最終平均值會有些高,可能使用1到0之間的值會更容易解釋,但這取決于您)。
import numpy as np
import matplotlib.pyplot as plt
import sys
def binarize(img, threshold):
# create an image with True or False (that can be seen as 1 or 0) and multiply by 255
binaryimg = (img > threshold).astype(int) * 255
return binaryimg
def comparison(img1, img2):
# convert to gray. What's the filter doing?
gray1 = rgb2gray(img1)
gray2 = rgb2gray(img2)
# select a threhsold value and binarize the image
threshold = 0.5
gray1_bin = binarize(gray1, threshold)
gray2_bin = binarize(gray2, threshold)
# in python you can compute a difference image.
# so diff will contain in each pixel the difference between the two images
diff = gray1_bin - gray2_bin
# the np.mean gives you already sum / number of pixels
average = np.mean(diff)
return average
def rgb2gray(col_img):
# converts images to gray
weights = np.array([0.3, 0.59, 0.11])
gray = np.sum([col_img[:, :, i].astype(np.float64) * weights[i] for i in range(3)], axis=0)
return gray
# the "main" method
if __name__ == "__main__":
# read the images
img = plt.imread(sys.argv[1])
img2 = plt.imread(sys.argv[2])
result = comparison(img, img2)
print("The difference between the images is {}".format(result))
希望這能有所幫助!
這篇關于Python中兩個圖像的比較的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,