【问题标题】:Plotting a Gradient Vector Field over the edges in Octave在 Octave 的边缘上绘制梯度向量场
【发布时间】: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);

Resultant Image

【问题讨论】:

  • 所以为了清楚起见,您拥有的这段代码对您有用吗?它产生正确的输出?那么您是否正在寻找一些具体的改进?速度?如果不是,为什么步骤更少?如果他们做同样的工作,清晰的代码通常比紧凑的代码更好,除非你有其他目标。为了提高速度,您可以尝试运行代码分析器以查看其大部分时间花在哪里。
  • 另外,最好链接一个示例图像,以便其他人可以看到您开始使用的内容。
  • 代码有效并且产生了正确的输出。但是我删除箭头的方式对我来说看起来很复杂。我想知道是否有办法让它变得更简单。另一方面,我还不允许在我的帖子中嵌入图片,所以我添加了一个链接(结果图片)。

标签: octave


【解决方案1】:

还有另一种方法可以减少 Quiver 绘制的箭头数量: https://www.mathworks.com/matlabcentral/newsreader/view_thread/306387

quiver(gx(1:2:end,1:2:end),gy(1:2:end,1:2:end))

问题是它会缩小图像,因为它占用了一半的梯度信息。因此,您可能也可以缩放背景图像并以更少的步骤获得类似的结果。

最好的问候

【讨论】:

  • 我明白了,这个解决方案的工作步骤更少。但正如你提到的,它会缩小我的矢量图像。尽管如此,我也可以缩小背景图像并以较低的分辨率获得类似的结果。如果您不关心最终分辨率,这可能是个不错的选择。
猜你喜欢
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 2018-04-10
  • 2012-05-12
相关资源
最近更新 更多