2

I'm using Template Matching to detect for smaller images in a large image. After detecting it , i would grab the center point(x y) of main picture of the detected image.

Could anyone advice how I could grab the shade/color of that particular center point?

I understand the Template Matching ignores color , based this example, is there anyway to grab the color intensity of the particular pixel? of that center point

# 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)
CodeGuru
  • 3,645
  • 14
  • 55
  • 99

1 Answers1

6

The grayscale image has just a single channel and the colour image has 3 or 4 channels (BGR or BGRA).

Once you have the pixel coordinates, the pixel value in the grayscale image will be an intensity value or you can get the BGR values from that pixel in the original image. That is, img_gray[y][x] will return an intensity value in the range 0-255, and img_rgb[y][x] will return a list of [B, G, R (, A)] values, each of which will have intensity values in the range 0-255.

Thus the value returned when you call e.g. img_gray[10][50] or print(img_gray[10][50]) is the pixel value at x=50, y=10. Similarly the value returned when you call e.g. img_rgb[10][50] is the pixel value at x=50, y=10, but calling it in this way will return the list of pixel values for that location e.g. [93 238 27] for RGB or [93 238 27 255] for RGBA. To get just the B, G or R value you would call img_rgb[10][50][chan] where for chan, B=0, G=1, R=2.

Mick
  • 811
  • 14
  • 31