我是台湾的一名高中生,正在为一个研究项目编写一个程序,将热图像的RGB值与温度条的RGB值进行比较,以获得每个像素的温度,然后将其保存到Excel文件中。该程序最初是用MATLAB编写的,现在我正在将其重写为Python。我已经用SMOP转换了它,并尝试修复它。但是,仍然有很多问题我不理解。
这是Python代码:
import cv2
import numpy
numpy.seterr(over='ignore')
import os
import pandas as pd
colorbar = cv2.imread(r'C:UsersPicturesDataColorbar.jpg')
num = 0
while num<=15300:
picture = os.path.join(r'C:UsersPicturesDataframe %d.jpg' %num)
pic = cv2.imread(picture)
h = 480
w = 638
k = 339
min = 60
temperature = numpy.linspace(25 + 273.15, 45 + 273.15, k)
tem = temperature.T
t = numpy.flipud(tem)
pict = numpy.zeros((h, w))
for i in numpy.arange(1, h).reshape(-1):
for j in numpy.arange(1, w).reshape(-1):
for n in numpy.arange(1, k).reshape(-1):
dif_r = abs(pic[i, j, 0] - colorbar[n, 10, 0])
dif_g = abs(pic[i, j, 1] - colorbar[n, 10, 1])
dif_b = abs(pic[i, j, 2] - colorbar[n, 10, 2])
d = dif_r + dif_g + dif_b
if d < min:
pict[i, j] = t[n]
num = num + 90
df = pd.DataFrame(pict).T
df.to_excel(excel_writer=r"C:UsersPicturesDataframe %d.xlsx" %num)
这是当前错误:
[ WARN:0@0.610] global D:aopencv-pythonopencv-pythonopencvmodulesimgcodecssrcloadsave.cpp (239) cv::findDecoder imread_('C:UsersPicturesDataframe 0.jpg'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "C:UsersPycharmProjectsMATLABmain.py", line 24, in <module>
dif_r = abs(pic[i, j, 0] - colorbar[n, 10, 0])
TypeError: 'NoneType' object is not subscriptable
这是MATLAB代码:
colorbar = imread(‘C:Users\PicturesDataColorbar.jpg’);
for num = 90:+90:15300
picture = fullfile(‘C:Users\PicturesData’, sprintf(‘frame %d.jpg’, num));
pic = imread(picture);
h = 480;
w = 638;
k = 340;
min = 60;
temperature = linspace(25+273.15, 45+273.15, k);
tem = temperature’;
t = flipud(tem);
pict = zeros(h, w);
for i = 1:h
for j = 1:w
for n = 1:k
dif_r = abs(pic(i, j, 1) – colorbar(n, 10, 1));
dif_g = abs(pic(i, j, 2) – colorbar(n, 10, 2));
dif_b = abs(pic(i, j, 3) – colorbar(n, 10, 3));
d = dif_r + dif_g + dif_b;
if d < min
pict(i, j) = t(n, 1);
end
end
end
end
filename = sprintf(‘frame %d.xlsx’, num);
writematrix(pict, filename);
end
您应该使用[]not((进行索引。例如,colorbar[n,10,1]。
然而,有比通过每个像素更容易的方法来减去图像,例如使用cv2.减法