【发布时间】:2017-02-08 01:32:59
【问题描述】:
我想计算边缘的梯度并将其绘制为 Octave 中的向量,叠加到现有图像上。
我应用了 Sobel 算子来获取边缘。向量似乎很好。但我想尽可能地减少我的代码。并以更少的步骤达到相同的结果。
为了更清楚,我在这里放了一些代码。提前感谢您的帮助。
###########################################################################
#
# Get Panda picture
#
###########################################################################
C = imread ("C:\\Users\\Elizabeth Judith\\Desktop\\cesar\\panda.png");
###########################################################################
#
# Transform Panda picture from Colors to Gray
#
###########################################################################
G = 0.3*C(:,:,1) + 0.6*C(:,:,2) + 0.1*C(:,:,3);
imwrite(G,"C:\\Users\\Elizabeth Judith\\Desktop\\cesar\\panda_gray.png");
###########################################################################
#
# Gradient of Sobel
#
###########################################################################
Edge = edge(G,"sobel");
[gx,gy] = gradient(double(Edge));
indices = find(abs(gx)==0.5);
indices = indices(1:2:end); # To delete arrows
gx(indices) = NaN;
indices = find(abs(gx)==0.5);
indices = indices(1:2:end); # To delete arrows
gx(indices) = NaN;
indices = find(abs(gx)==0.5);
indices = indices(1:2:end); # To delete arrows
gx(indices) = NaN;
indices = find(abs(gy)==0.5);
indices = indices(1:2:end); # To delete arrows
gy(indices) = NaN;
indices = find(abs(gy)==0.5);
indices = indices(1:2:end); # To delete arrows
gy(indices) = NaN;
indices = find(abs(gy)==0.5);
indices = indices(1:2:end); # To delete arrows
gy(indices) = NaN;
###########################################################################
#
# plot gradient vectors over image
#
###########################################################################
figure;
imshow(G, []);
hold on;
###########################################################################
#
# Quiver of the gradient
#
###########################################################################
h1 = quiver(abs(gx),abs(gy));
###########################################################################
#
# To scale quiver arrows
#
###########################################################################
set(h1,'AutoScale','on', 'AutoScaleFactor', 15);
【问题讨论】:
-
所以为了清楚起见,您拥有的这段代码对您有用吗?它产生正确的输出?那么您是否正在寻找一些具体的改进?速度?如果不是,为什么步骤更少?如果他们做同样的工作,清晰的代码通常比紧凑的代码更好,除非你有其他目标。为了提高速度,您可以尝试运行代码分析器以查看其大部分时间花在哪里。
-
另外,最好链接一个示例图像,以便其他人可以看到您开始使用的内容。
-
代码有效并且产生了正确的输出。但是我删除箭头的方式对我来说看起来很复杂。我想知道是否有办法让它变得更简单。另一方面,我还不允许在我的帖子中嵌入图片,所以我添加了一个链接(结果图片)。
标签: octave