我编写了一个 Matlab 函数来计算每个像素的对比度(即窗口的中心像素 (3x3) 与窗口中所有像素的平均值之间的差异,以及窗口中所有像素的标准偏差的差异窗户)。
对于 1024x1024 灰度图像,代码运行速度非常慢。有什么方法可以加快代码速度吗?谢谢!
function [ imContrast ] = LocalContrast( im )
% [ imContrast ] = LocalContrast( im )
% Compute the contrast between a center contrast and its neighbors.
%
% Equation: window_size = 3x3
% x_contrast = (x_center - mu) / std(pixels_in_window)
% mu: mean of the pixels' gray values in the window
%
% Input:
% im - original image in gray scale
%
% Output:
% imContrast - feature matrix of contrast, same size of im
[rows, cols] = size(im);
imContrast = double(zeros(size(im)));
% Boundary - keep the gray values of those in im
imContrast(1,:) = im(1,:) / 255;
imContrast(rows,:) = im(rows,:) / 255;
imContrast(:,1) = im(:,1) / 255;
imContrast(:,cols) = im(:,cols) / 255;
% Compute contrast for each pixel
for x = 2:(rows-1)
for y = 2:(cols-1)
winPixels = [ im(x-1,y-1), im(x-1,y), im(x-1,y+1),...
im(x,y-1), im(x,y), im(x,y+1),...
im(x+1,y-1), im(x+1,y), im(x+1,y+1)];
winPixels = double(winPixels);
mu = mean(winPixels);
stdWin = std(winPixels);
imContrast(x,y) = (double(im(x,y)) - mu) / stdWin;
end
end
end