我想用Python比较两个图像,但我不熟悉这种语言。你知道吗
我有两张同样大小的照片。我必须创建一个包含两个图像逐像素差异的数组。最后,我必须计算数组中所有值之和的平均值,作为一个浮点数。你知道吗
我可以使用处理来完成它,但是我不能使用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
你知道吗
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 ()
正如@martineau所建议的,Python图像库是一个很好的方法。我个人也认为可以使用numpy和matplotlib作为替代。python的好处是,您可以使用数组对整个图像执行操作,而不是使用for循环,这样看起来更好,速度更快。你知道吗
作为一个例子,我很快将您的代码移植到python中(不确定过滤器在那里做什么,阈值是多少,但其余的应该基本相同)
我也有一些疑问(你将二进制化后的值设置为255,这意味着最终的平均值会有点高,也许使用1到0之间的值之后更容易解释,但这取决于你)。你知道吗
希望这有帮助!你知道吗
相关问题 更多 >
编程相关推荐