在matlab中快速计算图像的梯度 [英] Fast computation of a gradient of an image in matlab

查看:2361
本文介绍了在matlab中快速计算图像的梯度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图优化我的代码,发现我的一个代码是瓶颈。我的代码是:

I was trying to optimize my code and found that one of my code is a bottleneck. My code was :

function [] = one(x)
I = imread('coins.png');
I = double(I);
I = imresize(I,[x x]);
sig=.8;    % scale parameter in Gaussian kernel
G=fspecial('gaussian',15,sig); % Caussian kernel
Img_smooth=conv2(I,G,'same');  % smooth image by Gaussiin convolution
[Ix,Iy]=gradient(Img_smooth);
f=Ix.^2+Iy.^2;
g=1./(1+f);  % edge indicator function.
end

我试图像这样运行:
全部清除;关闭全部;

I tried to run it like this : clear all;close all;

x=4000;N=1;
tic
for i=1:N
    one(x);
end
toc

我发现消耗的最大时间是总时间的渐变运算符语句(约60%)。所以它让我思考如何进一步优化我的代码....

I found that the largest amount of time was consumed by the gradient operator statement(about 60%) of the total time. So it got me thinking how can I optimize my code further ....

我咨询过几个网站: Dgradient 梯度-的图像合的matlab http://regularize.wordpress.com/2013/06/19/how-fast-can-you-calculate-the-gradient-of-an-image- in-matlab /

I consulted few sites like : Dgradient and http://regularize.wordpress.com/2013/06/19/how-fast-can-you-calculate-the-gradient-of-an-image-in-matlab/

然而Dgradient是一个MEX文件,我不想使用它。我想写自己的渐变功能。我在博客中读到,matlab中的梯度算子明显变慢,并且使用shift& amp;减法和稀疏矩阵。

However Dgradient is a MEX file and I do not want to use it. I wanted to write my own gradient function. I read in the blogs that the gradient operator in matlab is significantly slower and be speeded up by using shift & substract and sparse matrix.

我不了解稀疏矩阵。但是我确实尝试使用shift和substract方法。但是我很确定我的代码是错误的。请问任何人都可以澄清matlab用于计算其梯度的差异是什么?并告诉我如何在我的代码中执行此操作?

I do not have knowledge about sparse matrix. However I did try doing it by using shift and substract method. However I am preety sure my code is wrong. Please could anyone clarify what kind of difference matlab uses for calculation of its gradient ? And show me how to do it in my code ?

clc;clear all;close all;
I = imread('coins.png');
I = double(I(:,:,1));
I = imresize(I,[4 4]);

tic
[dx dy] = gradient(I);
toc

tic
%//Doing forward difference on both directions
dx1 = [I(:,2:end) I(:,end)] - I;
dy1 = [I(2:end,:);I(end,:)] - I;
toc

请大家查看代码并建议我如何正确应用它?或者使用稀疏矩阵指导我如何做到这一点?

Please could you guys look into the code and suggest me how to correctly apply it ? Or guide me how to do so using sparse matrix ?

推荐答案

感谢您的所有答案和有用的建议。我接受了 pseudoDust Hugues Dima 高性能标记的建议,并编写了自己的代码。我的代码如下:

Thanks to all of your answers and helpful suggestions . I took the suggestions of pseudoDust, Hugues , Dima and High Performance Mark and wrote my own code. My code is given below :

clc;clear all;close all;
x=32;
I = imread('coins.png');
I = imresize(I,[x x]);
I = double(I(:,:,1));

tic
[dx dy] = gradient(I);
toc

tic
[m,n]=size(I);
A = [I(:,2:end) zeros(m,1)];
B = [zeros(m,1) I(:,1:end-1)];
dx1 = [I(:,2)-I(:,1) (A(:,2:end-1)-B(:,2:end-1))./2 I(:,end)-I(:,end-1)];
A = [I(2:end,:) ; zeros(1,n)];
B = [zeros(1,n) ; I(1:end-1,:)];
dy1 = [I(2,:)-I(1,:) ; (A(2:end-1,:)-B(2:end-1,:))./2 ; I(end,:)-I(end-1,:)];
toc

nnz(dx-dx1)
nnz(dy-dy1)

我的基本想法是:渐变平均2个相邻位置(左右和上下),除了它取值和相邻的位置。然后我用matlab梯度函数(dx,dy)生成的矩阵检查了我生成的矩阵(dx1,dy1)。

My basic idea was that : Gradient averages the 2 adjacent positions (left and right or top and bottom), except for the edges where it takes the difference between the value and the adjacent position. I then checked the matrix generated by me (dx1,dy1) with the matrix generated by matlab gradient function (dx,dy).

Elapsed time is 0.010232 seconds.
Elapsed time is 0.000066 seconds.
ans =
     0
ans =
     0

所以我相信我的代码是正确的。至少可以说,时间结果令人惊讶。然后我用matlab计算我的代码,用于不同大小的图像。

So I believe my code is correct. Also the timing results was surprising to say the least. I then timed my code with the matlab one for different sizes of the images.

我得到了这个结果:

%x=16
Elapsed time is 0.010790 seconds.
Elapsed time is 0.000057 seconds.
%x=32
Elapsed time is 0.010564 seconds.
Elapsed time is 0.000069 seconds.
%x=64
Elapsed time is 0.010627 seconds.
Elapsed time is 0.000152 seconds.
%x=128
Elapsed time is 0.011346 seconds.
Elapsed time is 0.000669 seconds.
%x=256
Elapsed time is 0.017311 seconds.
Elapsed time is 0.004468 seconds.
%x=512
Elapsed time is 0.044148 seconds.
Elapsed time is 0.030435 seconds.
%x=1024
Elapsed time is 0.093386 seconds.
Elapsed time is 0.093029 seconds.
%x=2048
Elapsed time is 0.345423 seconds.
Elapsed time is 0.387762 seconds.






所以我的结论是这样的:图像尺寸高达1024X1024我的代码比在matlab中内置的渐变命令更快。


So my conclusion was this : For image size upto 1024X1024 my code was faster than the gradient command inbuilt in matlab.

编辑:我更新了我的答案并添加了这个图表:

Edit : I updated my answer and added this graph:

清楚地显示对于较小的数组大小,我的代码明显快于matlab梯度函数。

It clearly shows that for smaller array size my code is significantly faster than the matlab gradient function.

我的代码是否正确?伙计们请仔细查看并查看。请提供反馈。我实际上是matlab的新手,我对这个结果非常惊讶。请检查我在做什么是否正确?

Is my code correct ? Guys please look through it and check. Do give your feedback. I am literally a novice in matlab and am highly surprised by this result. Please check whether what I am doing is correct or not ?

这篇关于在matlab中快速计算图像的梯度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆