imbackground.m

来自「彩色图像纹理提取的一种算法」· M 代码 · 共 60 行

M
60
字号
function back_image = imbackground(img, lightback)

% Image Background, (Adam Meadows: ameadows@cs.ucr.edu)
% back_image = imbackground(img, lightback)
% 
% IMG is the input image
% LIGHTBACK is a flag specifying that the background is lighter than the foreground
%
% BACK_IMAGE returns the blank background image with the same dimensions as IMG
%

%convert to binary image
I = rgb2gray(img);
bw = im2bw(I, graythresh(I));

%if a light background, swap the values of bw
if(lightback == 1)
    bw = swap(bw);
end %end of if it was a light background

%variables to hold RGB info for all background pixels, and count
R = uint32(0);
G = uint32(0);
B = uint32(0);
numPixels = 0;

%now loop through the binary image, only considering coordinates == 1
[r c] = size(bw);
while (numPixels < 1000)
        
    i = floor(rand * r);
    if(i == 0)
        i = 1;
    end
    j = floor(rand * c);
    if(j == 0)
        j = 1;
    end
    
    if(bw(i,j) == 0)
        numPixels = numPixels + 1;
        R = R + uint32(img(i,j,1));
        G = G + uint32(img(i,j,2));
        B = B + uint32(img(i,j,3));
    end %end of if it's a background pixel

end %end of outer loop

R = floor(R / numPixels);
G = floor(G / numPixels);
B = floor(B / numPixels);

%create the background image
back_image = img;
back_image(:,:,1) = R;
back_image(:,:,2) = G;
back_image(:,:,3) = B;


%end %end of find_back function

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?