1
我寫了一個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