![](/img/trans.png)
[英]Get pixel location of binary image with intensity 255 in python opencv
[英]Color Intensity of Pixel(X,Y) on Image [OpenCV / Python]
我正在使用模板匹配來檢測大圖像中的較小圖像。 在檢測到它之后,我將抓住檢測到的圖像的主圖像的中心點(xy)。
誰能建議我如何抓住那個特定中心點的陰影/顏色?
我了解根據此示例,模板匹配會忽略顏色,總有沒有辦法抓住特定像素的顏色強度? 該中心點的
# 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]
將返回0-255范圍內的強度值,而img_rgb[y][x]
將返回[B, G, R (, A)]
值的列表,每個其強度值將在0-255范圍內。
因此,當您調用img_gray[10][50]
或print(img_gray[10][50])
的值是x=50
, y=10
的像素值。 類似地,當您調用img_rgb[10][50]
時返回的值是x=50
, y=10
處的像素值,但是以這種方式調用將返回該位置的像素值列表,例如[93 238 27]
對於RGB
或[93 238 27 255]
對於RGBA
。 要僅獲取B,G或R值,可以調用img_rgb[10][50][chan]
,其中chan
, B=0
, G=1
, R=2
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.