![](/img/trans.png)
[英]How to segment individual cells from a medical image using Python/OpenCV?
[英]How to segment nearby elements in a binary image using Python/Opencv
这不是最优雅的方法,但是应该足够简单。
考虑带w
的垂直切片(与您在问题中发布的切片相同)。 如果将切片的各行上的白色像素相加,则应获得与帽子的六个边缘相对应的六个漂亮的“峰值”:
但是,由于轮缘是圆形的,因此对于此类估计,某些垂直切片会比其他切片好。
因此,我建议查看所有宽度为w
切片,并对每个切片的峰值进行计数。
这是执行此操作的Matlab代码
img = imread('http://i.stack.imgur.com/69FfJ.jpg'); % read the image
bw = img(:,:,1)>128; % convert to binary
w = 75; % width of slice
all_slices = imfilter(single(bw), ones(1,w)/w, 'symmetric')>.5; % compute horizontal sum of all slices using filter
% a peak is a slice with more than 50% "white" pixels
peaks = diff( all_slices, 1, 1 ) > 0; % detect the peaks using vertical diff
count_per_slice = sum( peaks, 1 ); % how many peaks each slice think it sees
查看count_per_slice
的分布:
您会看到,尽管许多切片预测出错误的帽子数量(介于4到9之间),但大多数人投票赞成正确的帽子数量6:
num_hats = mode(count_per_slice); % take the mode of the distribution.
一个执行相同操作的python代码(假设bw
是shape
(h,w)
和dtype bool
的numpy数组):
from scipy import signal, stats
import numpy as np
w = 75;
all_slices = signal.convolve2d( bw.astype('f4'), np.ones((1,w),dtype='f4')/float(w), mode='same', boundary='symmetric')>0.5
peaks = np.diff( all_slices, n=1, axis=0 ) > 0
count_per_slice = peaks.sum( axis=0 )
num_hats = stats.mode( count_per_slice )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.