如何使用python修复鼠标悬停在像素值上的“ IndexError”?

时间:2019-06-26 03:53:29

标签: image rgb pixel python-3.7 temperature

我正在尝试通过鼠标移动从红外图像中获取像素值,并希望将其转换为温度。但是我收到以下错误:IndexError:索引598超出了轴480的大小480的范围

任何有关解决此问题的建议将不胜感激!

我尝试了以下方法。它显示某些x和y轴的像素值,但有时会超出限制。

import cv2
import numpy as np
import colour
import pytemperature
from PIL import Image
def mouse_drawing(event, x, y, flags, params):
    w = params[1]
    h = params[0]
    if event == cv2.EVENT_LBUTTONDOWN:
        print("Left Click")
        print(w,h)
    elif event == cv2.EVENT_MOUSEMOVE:
        if x<=w and y<=h:
            print("Coordinates of the pixel: X: ",x,"Y: ",y)
            # Assuming sRGB encoded colour values.
            RGB = img[x,y]
            # Conversion to tristimulus values.
            XYZ = colour.sRGB_to_XYZ(RGB / 255)
            # Conversion to chromaticity coordinates.
            xy = colour.XYZ_to_xy(XYZ)
            # Conversion to correlated colour temperature in K.
            CCT = colour.xy_to_CCT(xy, 'hernandez1999')
            print('TEMP: ',pytemperature.k2c(CCT))                        
img_file = 'examples/ax8.jpg'
img1 = cv2.imread(img_file, cv2.IMREAD_COLOR)
img = np.array(img1)
p,q,z = img.shape
params = [p,q]
cv2.namedWindow('image')
cv2.setMouseCallback('image',mouse_drawing,params)
while True:
    cv2.imshow('image',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

在范围内,输出格式为: 像素坐标:X:175 Y:51 TEMP:11958.9911017

0 个答案:

没有答案