使用 Python 从图片中查找彩色形状的数量 [英] Finding number of colored shapes from picture using Python

查看:42
本文介绍了使用 Python 从图片中查找彩色形状的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与从图片中识别颜色有关.做微生物学我需要计算用显微镜相机拍摄的照片上存在的细胞核数量.我使用 GIMP 用红色点标记原子核.现在我需要用 python 编写一个脚本,给定一个图像,它会告诉我有多少个红点.图片中除了点之外没有红色.

My problem has to do with recognising colours from pictures. Doing microbiology I need to count the number of cell nuclei present on a picture taken with a microscope camera. I've used GIMP to tag the nuclei with dots of red colour. Now I'd need to make a script in python, which, given an image, would tell me how many red dots are present. There is no red in the picture except in the dots.

我想到了一个相当复杂的解决方案,但它可能不是最好的解决方案:拍一张照片并开始遍历像素,检查每个像素的颜色.如果是红色,则检查所有 8 个最近的像素,再次递归检查每个红色的邻居,直到找不到更多相邻的红色像素.然后将核数增加 1 并标记遍历的像素,以便它们不会再次迭代.然后从停止的地方继续迭代.看起来有点沉重所以我想我会问,也许有人已经更优雅地处理了类似的问题.

I've thought of a rather complicated solution which is probably not the best one: Take a picture and start iterating through pixels checking each one's colour. If that is red, check all 8 nearest pixels, recursively check each red one's neighbours again until no more neighbouring red pixels are found. Then increment nuclei count by one and mark traversed pixels so they won't be iterated through again. Then continue iteration from where it stopped. Seems kind of heavy so I thought I'd ask, maybe someone has already dealt with a similar problem more elegantly.

问候,桑德

推荐答案

计数原子核

改编自 Python 图像教程的代码.使用教程中的原子核输入图像:

Count nuclei

The code adapted from Python Image Tutorial. Input image with nuclei from the tutorial:

#!/usr/bin/env python
import scipy
from scipy import ndimage

# read image into numpy array
# $ wget http://pythonvision.org/media/files/images/dna.jpeg
dna = scipy.misc.imread('dna.jpeg') # gray-scale image


# smooth the image (to remove small objects); set the threshold
dnaf = ndimage.gaussian_filter(dna, 16)
T = 25 # set threshold by hand to avoid installing `mahotas` or
       # `scipy.stsci.image` dependencies that have threshold() functions

# find connected components
labeled, nr_objects = ndimage.label(dnaf > T) # `dna[:,:,0]>T` for red-dot case
print "Number of objects is %d " % nr_objects

# show labeled image
####scipy.misc.imsave('labeled_dna.png', labeled)
####scipy.misc.imshow(labeled) # black&white image
import matplotlib.pyplot as plt
plt.imsave('labeled_dna.png', labeled)
plt.imshow(labeled)

plt.show()

输出

Number of objects is 17 

这篇关于使用 Python 从图片中查找彩色形状的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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