答案 0 :(得分:5)
正如对问题本身的评论所讨论的那样,“背景颜色”的概念是相当主观的,因此编写算法以保证所有输入的期望结果是不可能的。
那说,但是,我想我明白你要完成的是什么,并且我已经编写了几个MATLAB函数,这些函数非常成功地为我尝试过的许多输入图像识别可能的背景颜色
我使用的启发式是基于这样的观察:一般来说,图像的背景颜色可能是低频信息的区域,而前景可能是高频的。 (注意,如果不是这种情况,我的getBackgroundColor
函数将失败。)所以我所做的是隔离频域中的高频信息,将其转换回空间域,“传播 - 输出“所选像素以覆盖宽广的高频区域,然后简单地删除这些像素。
代码中有很多地方可以收紧和摆弄以提高特定应用程序的性能,但它似乎可以很好地适用于各种各样的测试用例。
getBackgroundColor.m:
function [img, meanColor, modeColor] = getBackgroundColor (img)
%
% function [img, meanColor, modeColor] = getBackgroundColor (img)
%
% img - Either a string representing the filename of an image to open
% or an image itself. If the latter, it must be either a
% 3-dimensional matrix representing an RGB image or a 2-dimensional
% matrix representing a grayscale image.
if ischar(img)
img = imread(imageFile);
end
img = double(img);
% Handle RGB and Grayscale separately.
if ndims(img)==3
% There are probably some spiffy ways to consolidate this sprawl
% so that the R, G, and B channels are not being processed
% independently, but for the time being, this does work.
red = getBG(img(:, :, 1));
green = getBG(img(:, :, 2));
blue = getBG(img(:, :, 3));
% For each channel, remove the "foreground" regions identified in
% each of the other channels.
red(isnan(green)) = NaN;
red(isnan(blue)) = NaN;
green(isnan(red)) = NaN;
green(isnan(blue)) = NaN;
blue(isnan(red)) = NaN;
blue(isnan(green)) = NaN;
% Compute the mean and mode colors.
meanColor = [ ...
mean(mean( red(~isnan(red)) )) ...
mean(mean( green(~isnan(green)) )) ...
mean(mean( blue(~isnan(blue)) )) ];
modeColor = [ ...
mode(mode( red(~isnan(red)) )) ...
mode(mode( green(~isnan(green)) )) ...
mode(mode( blue(~isnan(blue)) )) ];
% Update each the foreground regions of each channel and set them
% to their mean colors. This is only necessary for visualization.
red(isnan(red)) = meanColor(1);
green(isnan(green)) = meanColor(2);
blue(isnan(blue)) = meanColor(3);
img(:, :, 1) = red;
img(:, :, 2) = green;
img(:, :, 3) = blue;
else
img = getBG(img);
meanColor = mean(mean( img( ~isnan(img) ) ));
modeColor = mode(mode( img( ~isnan(img) ) ));
img(isnan(img)) = meanColor;
end
% Convert the image back to integers (optional)
img = uint8(img);
% Display the results before returning
display(meanColor)
display(modeColor)
function image = getBG (image)
mask = getAttenuationMask(size(image), min(size(image)) / 2, 0, 1);
% Assume that the background is mostly constant, so isolate the high-frequency
% parts of the image in the frequency domain and then transform it back into the spatial domain
fftImage = fftshift(fft2(image));
fftImage = fftImage .* mask;
invFftImage = abs(ifft2(fftImage));
% Expand the high-frequency areas of the image and fill in any holes. This should
% cover all but the (hopefully) low frequency background areas.
edgeRegion = imfill(imdilate(invFftImage, strel('disk', 4, 4)), 'holes');
% Now remove the parts of the image that are covered by edgeRegion
edgeMean = mean(mean(edgeRegion));
image(edgeRegion>edgeMean) = NaN;
end
end
getAttenuationMask.m:
function mask = getAttenuationMask (maskSize, radius, centerValue, edgeValue)
%
% function mask = getAttenuationMask (maskSize, radius, centerValue, edgeValue)
%
if nargin==2
centerValue = 1;
edgeValue = 0;
end
width = maskSize(1);
height = maskSize(2);
mx = width / 2;
my = height / 2;
mask=zeros(maskSize);
for i=1:width
for j=1:height
d = sqrt( (i-mx)^2 + (j-my)^2 );
if (d >= radius)
d = edgeValue;
else
d = (centerValue * (1 - (d / radius))) + (edgeValue * (d / radius));
end
mask(i, j) = d;
end
end