Speeding up Octave / Matlab plot

时间:2017-04-24 17:15:21

标签: matlab plot octave

Gnoivce and Hartmut helped a lot with this code but it takes a while to run. 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. 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?

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: Plot

To see original question original question

2 个答案:

答案 0 :(得分:3)

我猜测循环是代码中的瓶颈,花了这么长时间?您可以删除循环并通过调用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');

使用此修复重复代码会在Octave 4.0.3中立即呈现给我:

enter image description here

答案 1 :(得分:2)

正如我在评论中建议的那样,我会使用图像(我的系统需要0.12秒才能显示图像)。

编辑:更多评论,修复小错误,允许创建带有步骤大小的分档> 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")

output generated by script

与上面相同但是宽度为1(经过时间为0.167423秒。)

binEdges = 1:360;

bin width 1