根据颜色分割图像中的像素(Matlab) [英] Segment pixels in an image based on colour (Matlab)

查看:540
本文介绍了根据颜色分割图像中的像素(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅使用颜色信息来分割包含多个乐高积木的图像(目前).目的是找到例如是绿色的.我尝试使用k-均值聚类,但是给定中存在的不同彩色砖的数量有所不同.我还尝试过使用Matlab中的以下示例网站

I'm trying to segment an image containing multiple lego bricks using colour information only (for now). The aim is to find lego bricks that e.g. are green. I have tried using k-means clustering, but the number of different coloured bricks present in a given varies. I have also tried using the following example from the Matlab website

但这并不成功.有没有一种简单的根据颜色进行细分的方法?

but that wasn't successful. Is there a simple way of segmenting based on colour?

问题的示例图片:

推荐答案

因此,仅基于颜色选择区域时,RGB或LAB色彩空间并不是真正最佳的选择.更好的选择是HSV(色相饱和度值).在这里,我们可以定义哪些色调范围定义为绿色",饱和度的参数以定义为彩色"像素以及最小区域大小.然后基于这些值进行一些阈值处理,进行形态学过滤以及对绘制前返回的区域进行过滤.通常的例行程序.

So RGB or LAB colorspaces aren't really the best ones to use when choosing regions based on color alone. The better choice is HSV (Hue-Saturation-Value). Here we can define what hue ranges define 'green', a parameter for the saturation to defined what is a 'colored' pixel, and a minimum region size. Then some thresholding based on those values, some morphological filtering, and filtering of the regions that are returned before plotting. The usual routine.

下面的代码检测您提供的图像中的绿色砖块.这不是很完美,因为相邻的砖块作为单个区域返回,但是您可以使用边缘过滤器在这些检测到的区域内做一些更详尽的工作,例如,最终得到砖块数量的精确计数.

The code below detects the green bricks in your provided image. It's not quite perfect because adjacent bricks are returned as a single region, but you can do some more exhaustive work inside these detected regions with an edge filter, for example, to end up with a precise count of the number of bricks.

% Input image
img = imread('http://i.stack.imgur.com/HSYc1.jpg');

greenRange = [0.4 0.5]; % Range of hue values considered 'green'
minSat = 0.5; % Minimum saturation value for 'colored' pixels to exclude bkgd noise
minRegionsize = 500; % Min size for a single block

%%%%%%%%%%%%%%%%%%%
% Denoise with a gaussian blur
imgfilt = imfilter(img, fspecial('gaussian', 10, 2));
% Convert image to HSV format
hsvImg = rgb2hsv(imgfilt);

% Threshold hue to get only green pixels and saturation for only colored
% pixels
greenBin = hsvImg(:,:,1) > greenRange(1) & hsvImg(:,:,1) < greenRange(2) & hsvImg(:,:,2) > minSat;
greenBin = bwmorph(greenBin, 'close'); % Morphological closing to take care of some of the noisy thresholding

% Use regionprops to filter based on area, return location of green blocks
regs = regionprops(greenBin, 'Area', 'Centroid', 'BoundingBox');
% Remove every region smaller than minRegionSize
regs(vertcat(regs.Area) < minRegionsize) = [];

% Display image with bounding boxes overlaid
figure()
image(img);
axis image
hold on
for k = 1:length(regs)
    plot(regs(k).Centroid(1), regs(k).Centroid(2), 'cx');

    boundBox = repmat(regs(k).BoundingBox(1:2), 5, 1) + ...
        [0 0; ...
        regs(k).BoundingBox(3) 0;...
        regs(k).BoundingBox(3) regs(k).BoundingBox(4);...
        0 regs(k).BoundingBox(4);...
        0 0];    
    plot(boundBox(:,1), boundBox(:,2), 'r');
end
hold off

这篇关于根据颜色分割图像中的像素(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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