Matlab and vectorising code

Started by
11 comments, last by benjamin bunny 19 years, 10 months ago
I realise it''s a long shot, but I have a question about MatLab. I''ve found that when used for loops, it''s just about the slowest language I''ve ever used (with the possible exception of C64 basic, which, in its defense was running on a 6502 processor). The following code, which generates an RGB histogram with 16x16x8 bins, takes seconds to complete, even for small images.

    %qImage is a quantised integer version of the original image
    imSize=size(qImage);
    for (x = 1:imSize(1)) 
        for (y = 1:imSize(2))          
            a=qImage(x,y,1);
            b=qImage(x,y,2);
            c=qImage(x,y,3);
            h(a,b,c)=h(a,b,c)+1; 
        end
    end
Does anyone know how I would go about vectorising this block of code in order to speed it up? I''ve tried for ages to do this myself, but I can''t see a way of doing it.

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Advertisement
As far as I know, matlab is just horribly slow. I have to use it, and Ive found it to function at the same speed as a ti calculator, but on a computer. If youre using a newer version, i suggest, if possible, to use an older one. The new version aparently has some major flaws (mostly random crashing), but im not sure if they are slow or not. Sorry i couldnt help you more...

If it has a += operator (i honestly have no idea if it does, you could maybe save a little time by doing:

    %qImage is a quantised integer version of the original image    imSize=size(qImage);    szX = imSize(1)    szY = imSize(2)                  for (x = 1:szX )        for (y = 1:szY)                               h(qImage(x,y,1),qImage(x,y,2),c=qImage(x,y,3))+= 1;         end    end


[edited by - Mulligan on November 17, 2003 10:54:56 AM]

[edited by - Mulligan on November 17, 2003 10:56:16 AM]
I''m told it''s The language for image processing, which is why I''m using it. It certainly makes things image plotting and image loading relatively easy, although not more so than a decent C or Java library. I only have access to Version 6.5, which my university gave me. It which supposedly supports "acceleration", which as far as I can tell means recompiling matlab code in a sensible language; but it''s still dog slow.

I''ve even tried using the compiler to make a standalone C++ application, but then it complains about missing functions.

Can anyone help?

____________________________________________________________
www.elf-stone.com | Automated GL Extension Loading: GLee 2.00 for Win32 and Linux

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Sadly it doesn''t have a += operator, at least, it''s not called += if it does.

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Matlab is incredibly fast if used correct. It''s just when you start using for loops it becomes slow. So trying to make the inner loop more efficient is just a waste of time when it''s the loop itself that is the real problem.

I''m having a little problem understanding what h is. Is is supposed to count unique RGB-colors (each element is the number of times a certain color appears in the image)?
quote:I'm having a little problem understanding what h is. Is is supposed to count unique RGB-colors (each element is the number of times a certain color appears in the image)?

Yes, h is the quantised 3 channel histogram with 16x16x8 bins, which contains the count for each colour.

Actually it's not RGB, it's opponent-colour, hence the disparity in the bin dimensions.

[edited by - benjamin bunny on November 17, 2003 11:19:28 AM]

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Ok, this is what I've come up with. I will do my best to explain how it works, but I don't know how much you know about Matlab programming, so tell me if something isn't clear. Also, I haven't tested it much, but it did work as expected for some small handmade 3D arrays. It also assumes you have access to the imagaging toolbox.

First, I assume each value in your qImage is in the range [1, 16]/[1, 8], since you use the values directly as an index into h. We need to change this into the range [0, 15]/[0, 7] instead.
qImage = qImage-1; % Simple   

Then lets take the image and convert each tripple into a single value. Much like converting an unsigned char RGB tripple in C to an unsinged int, by packing the values into different bits of the integer.
A = qImage(:, :, 1) + 16*qImage(:, :, 2) + 16*16*qImage(:, :, 3);  

Now each unique tripple will be represented by a unique integer. Let's make a regular 1-dimensional histogram of this array.
hist_1d = histc(A( : ), (0:16*16*8-1)-0.5); % 16*16*8-1 is maximum value you can store in A  

How we reshape this histogram into a 3 dimensional histogram.
h = reshape(hist_1d, 16, 16, 8);  

h(a, b, c) will give you the number of times the color (a,b,c) appears in qImage (I hope ).

[edited by - Brother Bob on November 17, 2003 12:08:12 PM]
Thanks, It works great, and runs about 100x faster. I''d previously tried bitshifting to get the values into a 2D array, so I was on the right track; but I hadn''t figured out how to convert that to a 3D histogram.

I''ve been using matlab for about 2 weeks BTW, so this is all new to me. I''m used to much more low-level programming than this, so vectorising things like this is very different to what I''m used to. Still, it''s better than prolog .

____________________________________________________________
www.elf-stone.com | Automated GL Extension Loading: GLee 2.00 for Win32 and Linux

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Programming in Matlab is easy, but programming efficiently is an art. You just can''t think the same way as when you program in C or C++, you have to move to a whole new level. When you start realizing how little code you need to accomplish some tasks, you will be surprised
never loop in matlab, define a function and call it once for all values in the image....

This topic is closed to new replies.

Advertisement