【问题标题】:Color Intensity of Pixel(X,Y) on Image [OpenCV / Python]图像上像素(X,Y)的颜色强度 [OpenCV / Python]
【发布时间】:2018-08-10 08:05:22
【问题描述】:

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

谁能建议我如何获取特定中心点的阴影/颜色?

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

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

【问题讨论】:

    标签: python opencv cv2 template-matching


    【解决方案1】:

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

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

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

    【讨论】:

    • @CodeGuru 我已经扩展了我的答案
    猜你喜欢
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多