python-3.x - 数字化热图并将像素映射到值

标签 python-3.x image opencv image-processing heatmap

我想数字化热图,此来源中的面板D

image

第一步,我尝试在opencv中读取图像并获取矩阵

import cv2
from pprint import pprint


def read_as_digital(image):
    # mage dimensions
    h = image.shape[0]
    w = image.shape[1]
    print(h, w)
    pass


if __name__ == '__main__':

    image = cv2.imread('ip.jpg', 1)
    pprint(image)

    read_as_digital(image)

我可以将图像作为矩阵读取,但是我不知道如何在热图中指定单元格的开始(对应于图像面板D中的不同子图)。最后,我想将像素映射到值。

关于如何进行的任何建议将非常有帮助

编辑1:

我试图获得点击值

例如,当我考虑源代码中提供的热图的一小部分时

enter image description here

我希望获得图像中每个单元的平均值(以黄色圆点为中心)。
在不同的点单击会产生不同的值。单击已协调的单元格
enter image description here在不同点给出不同的RGB值。

关于如何获取每个单元格平均值的任何建议(例如enter image description here)将非常有帮助。

编辑2:

我尝试了更新的代码。

enter image description here

平均值((例如enter image description here))非常有效。但是,旁边的单元格有问题。当我单击相邻的单元格时,代码显示的均值是3个具有相同颜色的单元格。如果有一种方法可以限制像元大小,这很不错,它指定了一个边界,在该边界中应在代码中计算均值。编辑1中显示的图像有6行和6列。如果我们将其视为6×6矩阵,即A,则应为矩阵的每个Aijth条目均值。

最佳答案

import cv2
import numpy as np

# print pixel value on click
def mouse_callback(event, x, y, flags, params):
    if event == cv2.EVENT_LBUTTONDOWN:
        # get specified color
        row = y
        column = x
        color = image[row, column]
        print('color = ', color)
        # calculate range
        thr = 20  # ± color range
        up_thr = color + thr
        up_thr[up_thr < color] = 255
        down_thr = color - thr
        down_thr[down_thr > color] = 0
        # find points in range
        img_thr = cv2.inRange(image, down_thr, up_thr)  # accepted range
        height, width, _ = image.shape
        left_bound = x - (x % round(width/6))
        right_bound = left_bound + round(width/6)
        up_bound = y - (y % round(height/6))
        down_bound = up_bound + round(height/6)
        img_rect = np.zeros((height, width), np.uint8)  # bounded by rectangle
        cv2.rectangle(img_rect, (left_bound, up_bound), (right_bound, down_bound), (255,255,255), -1)
        img_thr = cv2.bitwise_and(img_thr, img_rect)
        # get points around specified point
        img_spec = np.zeros((height, width), np.uint8)  # specified mask
        last_img_spec = np.copy(img_spec)
        img_spec[row, column] = 255
        kernel = np.ones((3,3), np.uint8)  # dilation structuring element
        while cv2.bitwise_xor(img_spec, last_img_spec).any():
            last_img_spec = np.copy(img_spec)
            img_spec = cv2.dilate(img_spec, kernel)
            img_spec = cv2.bitwise_and(img_spec, img_thr)
            cv2.imshow('mask', img_spec)
            cv2.waitKey(10)
        avg = cv2.mean(image, img_spec)[:3]
        print('mean = ', np.around(np.array(avg), 2))
        global avg_table
        avg_table[:, 6 - int(x / (width/6)), 6 - int(y / (height/6))] = avg
        print(avg_table)

# average value of each cell in 6x6 matrix
avg_table = np.zeros((3, 6, 6))

# create window and callback
winname = 'img'
cv2.namedWindow(winname)
cv2.setMouseCallback(winname, mouse_callback)

# read & display image
image = cv2.imread('ip.jpg', 1)
image = image[3:62, 2:118]  # crop the image to 6x6 cells
cv2.imshow(winname, image)
cv2.waitKey()  # press any key to exit
cv2.destroyAllWindows()

请注意,OpenCV具有BGR颜色格式而不是RGB。因此,例如,单击红色将打印出[0, 0, 255]

您可以更改thr来调整可接受颜色的范围。

图像被裁剪为仅包含6 x 6矩阵部分。

关于python-3.x - 数字化热图并将像素映射到值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60777750/

相关文章:

python - 边数少于 n 的无向图

image - 使用阈值进行分割

python - 使用 cv2 进行色彩空间转换

opencv - 激光相对于相机的校准

c++ - 从opencv中的每一行矩阵中减去一个 vector ?

python - 按 0-10 的顺序轮换代理数组

python - 在Python中加速分割并写入文件

python - 如何屏蔽线以下的点?

显示为白色 block 的 android 通知图标

JavaFX Image获取图像路径