我尝试过这种阈值方法
)
最终结果是连接在一起的一些块形成了一个大段,这不是我所希望的。
任何其他方式来解决它
答案 0 :(得分:4)
我会试着给你一个基于深度梯度分离汽车的算法草图。唉,简单地看一下大深度渐变的轮廓,汽车并没有完全分开,因此,一些细节"边界轮廓是必需的。轮廓完成后,一个简单的连接组件聚类足以分离汽车。
这是我的代码(在Matlab中,但我非常肯定,找到opencv等效函数并不太复杂):
img = imread('http://i.stack.imgur.com/8lJw8.png'); % read the image
depth = double(img(:,:,1));
depth(depth==255)=-100; % make the background VERY distinct
[dy dx] = gradient(depth); % compute depth gradients
bmsk = sqrt(dx.^2+dy.^2) > 5; % consider only significant gradient
% using morphological operations to "complete" the contours around the cars
bmsk = bwmorph( bwmorph(bmsk, 'dilate', ones(7)), 'skel');
% once the contours are complete, use connected components
cars = bwlabel(~bmsk,4); % segmentation mask
st = regionprops(cars, 'Area', 'BoundingBox');
% display the results
figure;
imshow(img);
hold all;
for ii=2:numel(st), % ignore the first segment - it's the background
if st(ii).Area>200, % ignore small regions as "noise"
rectangle('Position',st(ii).BoundingBox, 'LineWidth', 3, 'EdgeColor', 'g');
end;
end;
输出
和
不完美,但会让你足够近。
进一步阅读:
bwmorph
:执行形态学操作。 bwlabel
:输出连接组件的分段掩码(标记)。 regionprops
:计算图像区域的统计信息(例如,区域和边界框)。想到它,深度有如此漂亮的渐变,你可以阈值深度渐变并得到漂亮的连接组件。
答案 1 :(得分:1)