我想將13個彩色塊的圖片信息變成一些文本。例如,我需要知道這裏有多少黃色和藍色的塊,以及它們的序列。使用Python在2個組合數組中排列序列
「C:\ target.jpg」
「C:\ blue.jpg」
「C:\ yellow.jpg」
我有什麼是:
import cv2
import numpy as np
img_rgb = cv2.imread("c:\\target.jpg")
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('c:\\blue.jpg',0)
# template = cv2.imread('c:\\blue.jpg',0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.99
loc = np.where (res >= threshold)
# if print loc
# (array([ 3, 31, 59, 87, 115, 143, 171, 199, 227, 255, 283, 311, 339], dtype=int64), array([7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], dtype=int64))
print str(loc[0] + loc[1])
當我分開運行,它給出的結果像這樣:
[ 13 41 69 97 125 153 181 209 237 265 293 321 349]
和
[ 10 38 66 94 122 150 178 206 234 262 290 318 346]
那麼這些都是每個13號,但我不知道該怎麼處理它們。
我怎樣才能把它們變成像文本:
「藍,黃,藍,黃,青,藍,黃,黃,藍,黃,藍,黃,藍,黃」。
輸入圖像總是像這樣簡單的固定堆棧,或者他們有時有其他模式,如多列,錯位,旋轉等? –
@John Zwinck,感謝您的評論。塊形狀總是簡單而相同。 –