Color Pixel Segment Pixels (Matlab)

I am trying to segment an image containing several lego blocks using only color information (for now). The goal is to find light bricks, for example, that are green. I tried to use k-mean clusters, but the number of different colored bricks present in this one is changing. I also tried using the following example on the Matlab website

but it failed. Is there an easy way to segment based on color?

An example image for the problem:

enter image description here

+6
source share
1 answer

Thus, RGB or LAB color spaces are actually not the best that you can use when choosing regions based only on color. The best choice is HSV (Hue-Saturation-Value). Here we can determine which hue ranges define green, the saturation parameter defined as a color pixel, and the minimum region size. Then some threshold value based on these values, some morphological filtering and filtering of the areas that are returned before plotting. Normal routine.

The code below defines green bricks in the image you provide. This is not entirely perfect, because neighboring bricks come back as a single region, but you can do more exhaustive work inside these detected areas using an edge filter, for example, to get the exact 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 

enter image description here

+8
source

Source: https://habr.com/ru/post/981160/


All Articles