如何根据opencv中的深度颜色分割连接区域

时间:2017-07-21 22:41:55

标签: opencv image-processing computer-vision image-segmentation

我有一张像enter image description here这样的图片,我需要将图片分割成8个块。

我尝试过这种阈值方法


)
在阈值处理后 enter image description here

最终结果是连接在一起的一些块形成了一个大段,这不是我所希望的。 enter image description here enter image description here enter image description here 任何其他方式来解决它

2 个答案:

答案 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;

输出

enter image description here

enter image description here

不完美,但会让你足够近。

进一步阅读:

  • bwmorph:执行形态学操作。
  • bwlabel:输出连接组件的分段掩码(标记)。
  • regionprops:计算图像区域的统计信息(例如,区域和边界框)。

想到它,深度有如此漂亮的渐变,你可以阈值深度渐变并得到漂亮的连接组件。

答案 1 :(得分:1)

天真的方法但它有效

步骤1:以灰度读取图像后,获得底部车辆的阈值。

ret1, car_thresh1 = cv2.threshold(cars, 191, 254, 0)

这给了我这个。 carsBottom

步骤2:从主图像中减去此图像

car_thresh2 = car_thresh1 - cars

这给了我这个。 enter image description here

步骤3:减去图像阈值

ret3, cars_thresh3 = cv2.threshold(car_thresh2, 58, 255, 0)

给了我carsTop

然后,我只是简单地完成了在 carsTop carsBottom 中提取和绘制轮廓所做的操作,这就是结果。 cars