![]() |
如何使用MATLAB检索任意图像的背景色?
这又是从弹簧联接另一个问题, [URL="https://stackoverflow.com/questions/2605202/how-to-automate-the-padding-for-arbitary-images-using-matlab"]这一个[/URL]锛?/ p> 如何以编程方式获取图像的背景色?
例: [IMG]https://www.google.com/intl/en_ALL/images/srpr/logo1w.png[/IMG] 对于上面的图像,背景颜色为白色。 [B]回答:[/B] 如问题本身的注释中所述,“背景颜色”的概念相当主观,因此实际上不可能编写一种算法来保证所有输入的期望结果。 就是说,但是,我想我了解您要完成的工作,并且我编写了一些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 [url=https://stackoverflow.com/questions/2612194]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 04:55。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.