Обнаружение теплых цветов на изображении Python - PullRequest
0 голосов
/ 04 мая 2020

У меня проблема, и мне нужна ваша помощь.

У меня есть серия термографических c изображений, из которых мне нужно обнаружить горячую точку (показано на панели справа от изображения ) в области, где проводится анализ. В случае этих примеров изображений горячая точка находится в фокусе перекрестия, однако цель состоит в том, чтобы представить, что я не знаю, где находится эта точка, и что сам алгоритм ее находит, основываясь на полосе на правильно. Я оставляю ниже некоторые из этих изображений в качестве примера:

IR_1544.jpg

IR_1546.jpg

IR_1548.jpg

IR_1566.jpg

IR_1574.jpg

В этом примере на боковой панели отображается диапазон температур от 33,2 до 97,7 ° C. Я хотел бы определить на изображении, где находится точка 97,7 ° C. Первоначально я создал код, в котором я читаю значение BGR в самой высокой точке шкалы и ищу эту комбинацию в остальной части изображения, это ничего не возвращало. Не убедившись, я создал код, который идентифицирует код RGB во всей строке и просматривает изображение, которое также ничего не возвращало, код следует ниже:

# 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()

Я хотел бы знать, если есть это еще один способ, которым я могу определить эти цвета на изображении. Я благодарю всех, кто может помочь !!

1 Ответ

0 голосов
/ 05 мая 2020

Мне пришлось пройти множество уроков, чтобы достичь своей цели:
Оценить яркость изображения Opencv
Преобразовать HSV в оттенки серого в OpenCV
Нахождение самого яркого пятна на изображении с использованием Python и OpenCV
OpenCV- Python Учебники
OpenCV- Python Учебники
Распознавание цифр с помощью OpenCV и Python
Распознавание текста и ди git из изображения с помощью Python, OpenCV и Tesseract OCR
Распознавание указать c числа из табличного изображения с помощью Pytesseract OCR
Преобразовать диапазон номеров в другой диапазон, сохраняя соотношение

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/StackOverflow/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()

enter image description here

...