在2相结合阵列的Python,安排序列 [英] Python, arranging sequences in 2 combined arrays

查看:117
本文介绍了在2相结合阵列的Python,安排序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,给所有。

我试图打开的13块五颜六色的图片到一些文本信息。例如,我需要知道这里有多少黄色和蓝色块,他们的顺序。

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

"c:\target.jpg"

C:\\ blue.jpg

"c:\blue.jpg"

C:\\ yellow.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])

当我分开运行,它给出的结果像这样的:

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

[ 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号,但我不知道如何处理它们。

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

它打印:

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

这篇关于在2相结合阵列的Python,安排序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆