检测 Python 图像中的暖色

Detecting warm colors in the Python image

我有问题需要你的帮助。

我有一系列热成像图像,我需要在其中检测正在进行分析的区域中的热点(显示在图像右侧的栏中)。在这些示例图像的情况下,热点位于十字准线的焦点,但是,目标是想象我不知道这个点在哪里并且算法本身找到它,基于正确的。我在下面留下其中一些图片作为示例:

IR_1544.jpg

IR_1546.jpg

IR_1548.jpg

IR_1566.jpg

IR_1574.jpg

在这个例子中,边栏指示的温度范围在 33.2 和 97.7 °C 之间。我想在图像中识别 97.7 °C 点在哪里。最初我创建了一个代码,我在其中读取条形最高点的 BGR 值并在图像的其余部分寻找这种组合,这没有 return 任何东西。不信,我创建了一个代码,识别整个条中的 RGB 代码,并在图像中查看,它也没有 return 任何东西,代码如下:

# Find one of temperature bar colors in the image
import cv2
image_path = r"C:\Users\bruno\PycharmProjects\TCC\Imagens\IR_1544.jpg"

img = cv2.imread(image_path)
crop1 = img[69:171, 309:310]

for i in range(70, 172):
    crop = img[i-1:i, 309:310]
    num1, num2, num3 = cv2.split(crop)
    for i in range(0, crop.shape[0]):
        for j in range(0, crop.shape[1]):
            if img[i][j][0] == num1:
                if img[i][j][1] == num2:
                    if img[i][j][2] == num3:
                        print("I found")

cv2.imshow("img1", img)
cv2.imshow("img2", crop1)
cv2.waitKey(0)
cv2.destroyAllWindows()

我想知道是否有其他方法可以识别图像中的这些颜色。 我感谢所有能提供帮助的人!!

我必须遵循很多教程才能实现我的目标:
Estimate Brightness of an image Opencv
Convert HSV to grayscale in OpenCV
Finding the Brightest Spot in an Image using Python and OpenCV
OpenCV-Python Tutorials
OpenCV-Python Tutorials
Recognizing digits with OpenCV and Python
Recognise text and digit from the image with Python, OpenCV and Tesseract OCR
Recognize specific numbers from table image with Pytesseract OCR
Convert a number range to another range, maintaining ratio

import cv2
import numpy as np
import pytesseract # used to read the digits on images
from PIL import Image # transformation of image read with OpenCV to use it with pytesseract 


src_path = 'C:/Users/user/Documents/Whosebug/WarmColorDetection/'
pytesseract.pytesseract.tesseract_cmd = 'C:/Users/user/AppData/Local/Tesseract-OCR/tesseract.exe'


def find_temperature_range(img, y1=0, y2=0, x1=0, x2=0):
    '''
    Find the number that indicates the temperature range for that image.
    :param img: The image where the temperature range is located.
    :param y1: Start of the temperature scale label height.
    :param y2: End of the temperature scale label height.
    :param x1: Start of of the temperature scale label width.
    :param x2: End of of the temperature scale label width.
    :return: A temperature range value read.
    '''
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    roi = gray[y1:y2, x1:x2]  # ROI - Region of Interest

    thresh = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    kernel = np.ones((1, 1), np.uint8)
    dilation = cv2.dilate(thresh, kernel, iterations=1)

    # Recognize text with tesseract for python
    binimagem = Image.fromarray(dilation)
    temperature_range = pytesseract.image_to_string(binimagem,
                                                    config='--psm 10 -c tessedit_char_whitelist=01234567890.')
    return float(temperature_range)


def find_warm_pixel(img, radius=3):
    '''
    Find warm pixel in the given image
    :param img: Image where the warm pixel will be searched
    :param radius: kernel
    :return: A tuple with the values of (minVal, maxVal, minLoc, maxLoc)
    '''
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Apply a Gaussian Blur to the image then find the brightest region
    gray = cv2.GaussianBlur(gray, (radius, radius), 0)
    return cv2.minMaxLoc(gray)


if __name__ == '__main__':
    # Loop over all images and show the warm point of all of them
    for i in range(1, 6):
        img = cv2.imread(f'img/img{i}.jpg', 1)
        y, x, _ = img.shape
        img_copy = img.copy()

        max_temp_range = find_temperature_range(img_copy, 45, 60, 280, 315)
        min_temp_range = find_temperature_range(img_copy, 178, 194, 280, 315)

        if i == 1:
            max_temp_range = 97.7  # Could not read the correct number only for this case, as it's showing 77

        (minVal, maxVal, minLoc, maxLoc) = find_warm_pixel(img_copy)

        # Converting a pixel value based on minimum and maximum value range read from the image
        # new_value = ( (old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min
        old_value = maxVal
        old_min = 0
        old_max = 255
        temperature = ((old_value - old_min) / (old_max - old_min)) * (max_temp_range - min_temp_range) + min_temp_range

        circle_radius = 3
        cv2.circle(img, maxLoc, circle_radius, (255, 0, 0), 2)  # draw a circle around the britest pixel
        cv2.putText(img, f'Coordinate: {maxLoc}', (122, 210), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1,
                    cv2.LINE_AA)
        cv2.putText(img, f'Value: {temperature:.2f}', (122, 225), cv2.FONT_HERSHEY_SIMPLEX, 0.35,
                    (255, 255, 255), 1,
                    cv2.LINE_AA)

        # Display the result
        cv2.namedWindow(f'Image {i}', cv2.WINDOW_GUI_NORMAL)
        cv2.resizeWindow(f'Image {i}', x, y)
        cv2.imshow(f'Image {i}', img)

    cv2.waitKey(0)
    cv2.destroyAllWindows()