• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Matlab is SO SLOW for image processing..

Status
Not open for further replies.

TecHNooB

Diamond Member
What's the fastest way to extract a bajillion X by X blocks from an image matrix by shifting that block 1 pixel at a time across the entire image and storing each block as a row in a 2D array? Also, what is the programming language of choice for image processing.
 
Assembly, Fortran, C, C++, Java...

Take your pick. Matlab is great because it's already carefully designed the algorithms and easy to setup the code. However, I've found that something like C++ or Fortran is much much much faster than Matlab. Also, in C++, try to avoid multi-rank dynamically allocated arrays. The extra pointer dereferences can really add up in some stuations.
 
Matlab is very fast for image processing in almost every case. How exactly are you doing this? The algorithm is likely the cause of the poor performance. If you are using a loop rather than the built-in array functionality, then that is probably the reason.
 
Originally posted by: CycloWizard
Matlab is very fast for image processing in almost every case. How exactly are you doing this? The algorithm is likely the cause of the poor performance. If you are using a loop rather than the built-in array functionality, then that is probably the reason.

I know there is probably a better method. What I'm trying to do is sweep a block of size N x N across every pixel in a 2-D matrix (image), and re-arrange the information in each block as a row in another 2-D matrix where every row represents 1 block. It's not a block by block operation because I am sweeping by 1 pixel.
 
This should probably be in the programming forum, but it would help if you could post the code you're using now. I'm not going to write the whole program, but I can probably help you out if I see what you're doing.
 
MATLAB is probably as good as your gonna get. It is designed for speed on vector applications which is exactly what you are doing there.
 
What do you mean by "sweep a block"? (I have no experience w/image processing; i'm a cfd guy.) But as CycloWizard mentioned, if you're looping a lot, that'll be bad. Doing NxN blocks 1 pixel at a time will lead to horrible performance; nominally you'd like to at least be operating on the whole NxN block (or more) at once.

The reasoning for this is that MATLAB calls the BLAS/LAPACK for it's matrix, vector, vector-vector, matrix-vector, and matrix-matrix operations. BLAS/LAPACK are highly optimized (but here more importantly -compiled-) libraries for doing linear algebra (anything from finding eigenvalues all the way down to multiplying vectors). So suppose I have:
A = rand(1e4); B = rand(1e4);
Doing:
result = A*B;
will be like more than 100x faster than doing:
for i = 1:1e4
for j = 1:1e4
result(i,j) = 0;
for k = 1:1e4
result(i,j) = result(i,j) + A(i,k)*B(k,j)
end
end
end

(sorry i'm too lazy to space it nicely)
 
Turns out the slowness was because matlab was running out of memory because my matrix was too big 🙁

I was basically taking the DCT of an N by N block. Moving it over by one pixel. Repeat until the entire image matrix is 'swept'. And storing every block into a row of a super huge matrix.
 
Originally posted by: TecHNooB
Turns out the slowness was because matlab was running out of memory because my matrix was too big 🙁

I was basically taking the DCT of an N by N block. Moving it over by one pixel. Repeat until the entire image matrix is 'swept'. And storing every block into a row of a super huge matrix.
Maybe I should have asked this the first time, but why would you ever do this?
 
Originally posted by: CycloWizard
Originally posted by: TecHNooB
Turns out the slowness was because matlab was running out of memory because my matrix was too big 🙁

I was basically taking the DCT of an N by N block. Moving it over by one pixel. Repeat until the entire image matrix is 'swept'. And storing every block into a row of a super huge matrix.
Maybe I should have asked this the first time, but why would you ever do this?

Detecting JPEG copy-move via block comparison. Basically, take a JPEG image, copy a part of it and move it somewhere else in the image. I'm writing a simple code to detect these things. Oh, the extraction of blocks and storing into a 2-D matrix is for row-comparison purposes. I probably could write a version that does not save the block information (although saving all of it would be more convenient).
 
Originally posted by: TecHNooB
Originally posted by: CycloWizard
Originally posted by: TecHNooB
Turns out the slowness was because matlab was running out of memory because my matrix was too big 🙁

I was basically taking the DCT of an N by N block. Moving it over by one pixel. Repeat until the entire image matrix is 'swept'. And storing every block into a row of a super huge matrix.
Maybe I should have asked this the first time, but why would you ever do this?

Detecting JPEG copy-move via block comparison. Basically, take a JPEG image, copy a part of it and move it somewhere else in the image. I'm writing a simple code to detect these things. Oh, the extraction of blocks and storing into a 2-D matrix is for row-comparison purposes. I probably could write a version that does not save the block information (although saving all of it would be more convenient).

You can reference an entire gridspace. For example, if you have a NxM matrix and you want the entire xth row, then you use:

M(x,🙂

You can use "end" for the last element, so to choose elements 3 through M inclusive of the xth row:

M(x,3:end)

With Matlab it is always best to use their functions when possible and to try and operate on entire matrices or vectors at a time. Doing element by element operations will be much much slower.
 
Status
Not open for further replies.
Back
Top