简体   繁体   English

加快倍频程/ Matlab绘图

[英]Speeding up Octave / Matlab plot

Gnoivce and Hartmut helped a lot with this code but it takes a while to run. Gnoivce和Hartmut在这段代码中提供了很多帮助,但是要花一些时间才能运行。 The CData property in the bar command doesn't seem to be implemented in the Octave 4.0-4.2.1 version which I'm using . bar命令中的CData属性似乎没有在我使用的Octave 4.0-4.2.1版本中实现 The work around for this was to plot all the single bars individually and set an individual color for each individual bar. 解决方法是分别绘制所有单个条形,并为每个单独的条形设置单独的颜色。 People helped me out and got me this far but it takes 5 minutes for the plot to show does anyone know a way of speeding this up? 人们帮助了我,使我走了这么远,但是剧情花了5分钟才能显示出有人知道加快这一步的方法吗?

The following code runs: marbles.jpg image file used below: 运行以下代码:大理石使用以下图像文件: marbles.jpg

clear all,clf reset,tic,clc 
rgbImage = imread('/tmp/marbles.jpg');
hsvImage = rgb2hsv(rgbImage);         % Convert the image to HSV space
hPlane = 360.*hsvImage(:, :, 1);      % Get the hue plane scaled from 0 to 360
binEdges = 0:360;                 %# Edges of histogram bins
N = histc(hPlane(:),binEdges);    %# Bin the pixel hues from above

C = colormap(hsv(360));  %# create an HSV color map with 360 points
stepsize = 1; % stepsize 1 runs for a while...

for n=binEdges(2:stepsize:end)   %# Plot the histogram, one bar each
    if (n==1), hold on, end
    h=bar(n,N(n));
    set(h,'FaceColor',C(n,:));   %# set the bar color individually
end

axis([0 360 0 max(N)]);           %# Change the axes limits
set(gca,'Color','k');             %# Change the axes background color
set(gcf,'Pos',[50 400 560 200]);  %# Change the figure size
xlabel('HSV hue (in degrees)');   %# Add an x label
ylabel('Bin counts');             %# Add a y label
fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);

Plot created after 5 mins: 5分钟后创建的图: 情节

To see original question original question 看原题原题

I'm guessing the loop is the bottleneck in your code that is taking so long? 我猜循环是花很长时间的代码瓶颈? You could remove the loop and create the plot with one call to bar , then call set to modify the hggroup object and its child patch object: 你可以删除环和一个调用创建的情节bar ,然后调用set修改hggroup对象及其子patch对象:

h = bar(binEdges(1:end-1), N(1:end-1), 'histc');   % hggroup object
set(h, 'FaceColor', 'flat', 'EdgeColor', 'none');
hPatch = get(h, 'Children');                       % patch object
set(hPatch, 'CData', 1:360, 'CDataMapping', 'direct');

Repeating your code with this fix renders right away for me in Octave 4.0.3: 重复此修复程序的代码将立即在Octave 4.0.3中为我呈现:

在此处输入图片说明

As I suggested in a comment, I would use image (takes 0.12s on my system for your image). 正如我在评论中建议的那样,我将使用图像(系统上的图像占用0.12s)。

EDIT: more comments, fix little bug, allow to create bins with stepsize > 1 编辑:更多评论,修复小错误,允许创建步骤大小> 1的垃圾箱

img_fn = "17S9PUK.jpg";
if (! exist (img_fn, "file"))
  disp ("downloading image from imgur.com...");
  fflush (stdout);
  urlwrite ("http://i.imgur.com/17S9PUK.jpg", "17S9PUK.jpg");
endif

rgbImage = imread (img_fn);

## for debugging so the matrixes fit on screen
if (0)
  pkg load image
  rgbImage = imresize (rgbImage, [6 8]);
endif

hsvImage = rgb2hsv(rgbImage);
hPlane = 360 .* hsvImage(:, :, 1); 

## create bins, I've choosen 2 step to "smooth" the result
binEdges = 1:2:360;
N = histc (hPlane(:), binEdges)';

cm = permute (hsv (numel (binEdges)), [3 1 2]);

## Create an image with x = hue
img = repmat (cm, max(N), 1);

## Create binary mask which is used to black "img" dependent on N
sp = sparse (N(N > 0), (1:360)(N > 0), true, max(N), numel (binEdges));
mask = full (cumsum (flipud (sp)));

## extend mask in depth to suppress RGB
mask = repmat (mask, [1 1 3]);

## use inverted mask to "black out" pixels < N
img(logical (1 - flipud (mask))) = 0;

## show image
image (binEdges, 1:max(N), img)
set (gca, "ydir", "normal");
xlabel('HSV hue (in degrees)');
ylabel('Bin counts');

## print it for stackoverflow
print ("out.png")

脚本生成的输出

Same as above but with bin width 1 (Elapsed time is 0.167423 seconds.) 与上述相同,但纸槽宽度为1(经过的时间为0.167423秒。)

binEdges = 1:360;

纸箱宽度1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM