如何让我的代码 运行 更快?
How can I make my code run faster?
我目前正在编写一段代码,通过在每个像素周围获取像素的圆形样本,找到它们的均值,然后将其应用于中心像素来模糊图像。它有效,但需要很长时间,尤其是大图像和大半径。
谁能给我一些关于如何加快速度的提示?
function [] = immean(IMAGE, r)
%Find the maximum width, nx, and maximum height, ny, of the image - so that
%it can be used to end the for-loop at the appropriate positions.
[nx, ny] = size(IMAGE);
%Create a completely black image of the same size as the subject image,
%into which the appropriate pixel values can be fed.
average = uint8(zeros(size(IMAGE)));
%Loop through all the pixels of the image.
for x = 1:nx
for y = 1:ny
%This next code takes a square sample of pixels, with dimensions of
%r x r.
%First, set the boundaries of this square, from which the circular
%sample of pixels will be taken.
if x-r <= 0
startx = 1;
else
startx = x-r;
end
if x+r > nx
endx = nx;
else
endx = x+r;
end
if y-r <= 0
starty = 1;
else
starty = y-r;
end
if y+r > ny
endy = ny;
else
endy = y+r;
end
%Loop through this square sample and, if the pixel is within the
%range of the circle, add its intensity to the total.
total = 0;
pixelcount = 0;
for xp = startx : endx
for yp = starty : endy
if (x-xp)^2 + (y-yp)^2 <= r^2
total = total + uint32(IMAGE(xp, yp));
pixelcount = pixelcount + 1;
end
end
end
mean = total / pixelcount;
average(x,y) = mean;
end
end
imshow(average)
我曾尝试更改 uint32
之类的内容,但没有奏效。除此之外,我还是个初学者,所以我不确定在这种情况下最好的技巧是什么。谢谢你的时间。
MATLAB 中的循环非常慢。通常,您应该始终尽可能向量化您的代码。这是其中一个案例。遍历每个像素非常慢。
MATLAB 有一个函数 imfilter 基本上可以满足您的需求。由于您只是采用平均强度,因此一个简单的过滤函数可以非常好且非常快速地完成这项工作。您可以将滤波器系数定义为矩阵:
% Define a 2D Filter with radius r:
d = 2*r+1;
h = zeros(d);
% Now make it a "circular" filter (keeping it square would be much easier
% and probably look the same but whatever):
[x, y] = meshgrid(1:d,1:d);
distance = sqrt((x-(r+1)).^2 + (y-(r+1)).^2);
h(distance<=r) = 1;
h = h / sum(h(:))
% Now pump it into imfilter and youre done:
average = imfilter(uint32(IMAGE), h);
此外,还有大量的 MATLAB 图像处理工具,所以搜索一下,您可能会找到对您的工作有用的工具,而无需重新发明轮子。我面前没有图像来测试这个,但如果它有效,请告诉我。
我目前正在编写一段代码,通过在每个像素周围获取像素的圆形样本,找到它们的均值,然后将其应用于中心像素来模糊图像。它有效,但需要很长时间,尤其是大图像和大半径。
谁能给我一些关于如何加快速度的提示?
function [] = immean(IMAGE, r)
%Find the maximum width, nx, and maximum height, ny, of the image - so that
%it can be used to end the for-loop at the appropriate positions.
[nx, ny] = size(IMAGE);
%Create a completely black image of the same size as the subject image,
%into which the appropriate pixel values can be fed.
average = uint8(zeros(size(IMAGE)));
%Loop through all the pixels of the image.
for x = 1:nx
for y = 1:ny
%This next code takes a square sample of pixels, with dimensions of
%r x r.
%First, set the boundaries of this square, from which the circular
%sample of pixels will be taken.
if x-r <= 0
startx = 1;
else
startx = x-r;
end
if x+r > nx
endx = nx;
else
endx = x+r;
end
if y-r <= 0
starty = 1;
else
starty = y-r;
end
if y+r > ny
endy = ny;
else
endy = y+r;
end
%Loop through this square sample and, if the pixel is within the
%range of the circle, add its intensity to the total.
total = 0;
pixelcount = 0;
for xp = startx : endx
for yp = starty : endy
if (x-xp)^2 + (y-yp)^2 <= r^2
total = total + uint32(IMAGE(xp, yp));
pixelcount = pixelcount + 1;
end
end
end
mean = total / pixelcount;
average(x,y) = mean;
end
end
imshow(average)
我曾尝试更改 uint32
之类的内容,但没有奏效。除此之外,我还是个初学者,所以我不确定在这种情况下最好的技巧是什么。谢谢你的时间。
MATLAB 中的循环非常慢。通常,您应该始终尽可能向量化您的代码。这是其中一个案例。遍历每个像素非常慢。
MATLAB 有一个函数 imfilter 基本上可以满足您的需求。由于您只是采用平均强度,因此一个简单的过滤函数可以非常好且非常快速地完成这项工作。您可以将滤波器系数定义为矩阵:
% Define a 2D Filter with radius r:
d = 2*r+1;
h = zeros(d);
% Now make it a "circular" filter (keeping it square would be much easier
% and probably look the same but whatever):
[x, y] = meshgrid(1:d,1:d);
distance = sqrt((x-(r+1)).^2 + (y-(r+1)).^2);
h(distance<=r) = 1;
h = h / sum(h(:))
% Now pump it into imfilter and youre done:
average = imfilter(uint32(IMAGE), h);
此外,还有大量的 MATLAB 图像处理工具,所以搜索一下,您可能会找到对您的工作有用的工具,而无需重新发明轮子。我面前没有图像来测试这个,但如果它有效,请告诉我。