python数组两两组合,使用Python在2个组合数组中排列序列

I am trying to turn information on a picture of 13 colourful blocks into some texts. For example, I need to know how many yellow and blue blocks here, and their sequence.

"c:\target.jpg"

e59967bf54205ac60bd3554c4ae0a719.png

"c:\blue.jpg"

63e72f21677dcf9993c8e6e389c54f3e.png

"c:\yellow.jpg"

8d0b33eafcefcbe3464b406e7f014a28.png

What I have is:

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

When I run them separately, it gives results like these:

[ 13 41 69 97 125 153 181 209 237 265 293 321 349]

and

[ 10 38 66 94 122 150 178 206 234 262 290 318 346]

Well those are each 13 numbers but I don’t know how to handle them.

How can I turn them into texts like:

"blue, yellow, blue, yellow, blue, blue, yellow, yellow, blue, yellow, blue, yellow, blue, yellow".

解决方案

Here's a quite simple solution that just reads a stripe of pixels down the center:

from PIL import Image

im = Image.open(filename)

xMin, yMin, xMax, yMax = im.getbbox()

x = (xMin + xMax) / 2

colors = []

oldColor = None

for y in xrange(yMin, yMax):

r, g, b = im.getpixel((x, y))

if r > 240 and g > 240 and b > 240:

newColor = 'white'

elif g > 150 and b > 150:

newColor = 'blue'

elif r > 150 and g > 150:

newColor = 'yellow'

else:

newColor = 'unknown'

if newColor != oldColor:

if newColor != 'white':

colors.append(newColor)

oldColor = newColor

print colors

It prints:

['blue', 'yellow', 'blue', 'yellow', 'blue', 'blue', 'yellow', 'yellow', 'blue', 'yellow', 'blue', 'yellow', 'blue']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值