'How to write a python program to compare RGB values of thermal images to get the temperature?
I am a high school student from Taiwan writing a program for a research project to compare RGB values of thermal images to the RGB values of the temperature bar to get the temperature of each pixel, then save it to an Excel file. The program was first written in MATLAB, and now I am rewriting it to Python. I already used SMOP to convert it and tried fixing it. However, there are still many problems I don't understand.
This is the Python code:
import cv2
import numpy
numpy.seterr(over='ignore')
import os
import pandas as pd
colorbar = cv2.imread(r'C:\Users\Pictures\Data\Colorbar.jpg')
num = 0
while num<=15300:
picture = os.path.join(r'C:\Users\Pictures\Data\frame %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:\Users\Pictures\Data\frame %d.xlsx" %num)
This is the current error:
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('C:\Users\Pictures\Data\frame 0.jpg'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "C:\Users\PycharmProjects\MATLAB\main.py", line 24, in <module>
dif_r = abs(pic[i, j, 0] - colorbar[n, 10, 0])
TypeError: 'NoneType' object is not subscriptable
This is the MATLAB code:
colorbar = imread(‘C:\Users\\Pictures\Data\Colorbar.jpg’);
for num = 90:+90:15300
picture = fullfile(‘C:\Users\\Pictures\Data\’, 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
Solution 1:[1]
You should index using [] not (). So colorbar[n,10,1] for example.
However, there are easier ways of subtracting images than going through each pixel for example using cv2.subtract
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MLRAL |
