Archived

This topic is now archived and is closed to further replies.

benjamin bunny

Matlab and vectorising code

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
never loop in matlab,


Yes, I''m aware that looping is slow in matlab, hence my question asking how to vectorise the code.

I''m not sure I fully agree with this approach to programming though. In image processing, surely performance and space efficiency is critical. I like the way vectorising works in matlab, and it''s certainly convenient, but it will never be as flexible as using for loops.

Still, it looks like they''re on the right track with acceleration, even though I couldn''t get it to work properly. In the examples they give in the help docs, using accelerated for loops is up to 300 times faster than the original code, or 5-6 times faster than the vectorised versions.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi, I was looking the net and found this massege.
I was looking for a way to implament a function that find the histogram of an image, without using loops or the built in functions like hist or histc and others of the same type.

If someone has an idea, it would be really great, and help me.

Share this post


Link to post
Share on other sites