python找出在原图中的位置,使用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.

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.

Regards,

Sander

解决方案

Count nuclei

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

4ed6023eee41e11e742134ee9b6b172f.png

#!/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()

Output

Number of objects is 17

4f7c7053a6a538bf0997f24aad7e32d5.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值