图像上像素 (X,Y) 的颜色强度 [OpenCV / Python]

Color Intensity of Pixel(X,Y) on Image [OpenCV / Python]

我正在使用模板匹配来检测大图像中的小图像。 检测到后,我会抓取检测图像的主图的中心点(x y)。

谁能告诉我如何抓住那个特定中心点的 shade/color?

我知道模板匹配会忽略颜色,基于这个例子,有没有办法获取特定像素的颜色强度?该中心点

# Python program to illustrate 
# template matching
import cv2
import numpy as np
import time
import sys


# Read the main image
img_rgb = cv2.imread('test.png')

# Convert it to grayscale
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

# Read the template
template = cv2.imread('template.png',0)

# Store width and heigth of template in w and h
w, h = template.shape[::-1]

# Perform match operations.
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)

# Specify a threshold
threshold = 0.90

# Store the coordinates of matched area in a numpy array
loc = np.where( res >= threshold)

xyMiddle = ""
for pt in zip(*loc[::-1]):
    xyMiddle = str(pt[0] + w/2) +"," +str(pt[1] + h/5)
if(xyMiddle != ""):
    print(xyMiddle)

灰度图像只有一个通道,彩色图像有 3 或 4 个通道(BGR 或 BGRA)。

获得像素坐标后,灰度图像中的像素值将是强度值,或者您可以从原始图像中的该像素获取 BGR 值。也就是说,img_gray[y][x] 将 return 范围为 0-255 的强度值,而 img_rgb[y][x] 将 return 一个 [B, G, R (, A)] 值的列表,每个值将强度值在 0-255 范围内。

因此,当您调用例如img_gray[10][50]print(img_gray[10][50])x=50y=10处的像素值。同样,当您致电时 returned 的值img_rgb[10][50]x=50y=10 处的像素值,但以这种方式调用它会 return 该位置的像素值列表,例如[93 238 27] 代表 RGB[93 238 27 255] 代表 RGBA。要仅获得 B、G 或 R 值,您可以调用 img_rgb[10][50][chan],其中 chanB=0G=1R=2