Effacer les filtres
Effacer les filtres

How do I store a series of 5*5 arrays into a multidimensional array?

1 vue (au cours des 30 derniers jours)
Bob
Bob le 11 Fév 2013
Hi people, I am new to this forum as well as MATLAB. As of now I'm working on copy-move forgery and i intend to do a 5*5 pixel comparison within the image. I want to store the values of every 5*5 array within the image into a multidimensional array but i'm not too sure how. Below is what i've come up with so far:
clear;
clf;
A=imread('file1.png');
Agray=rgb2gray(A);
[ar,ac]=size(Agray);
v1=5;
v2=5;
r=ar-v1+1;
c=ac-v2+1;
noOfArrays=r*c;
for i=1:r
for j=1:c
k(:,:,noOfArrays)=(Agray(i:i+4,j:j+4));
end
end
k
In which k is my supposed multidimensional array. Any help would be appreciated thank you :)

Réponse acceptée

Sven
Sven le 11 Fév 2013
Modifié(e) : Sven le 11 Fév 2013
Bob, here's a fix to your loop:
Agray=imread('rice.png');
[ar,ac]=size(Agray);
v1=5;
v2=5;
r=ar-v1+1;
c=ac-v2+1;
noOfArrays=r*c;
k = zeros([v1,v2,noOfArrays], class(Agray));
currSliceNo = 0;
for i=1:r
for j=1:c
currSliceNo = currSliceNo+1;
k(:,:,currSliceNo)=(Agray(i:i+4,j:j+4));
end
end
Note that I added a preallocation which will speed things up substantially, and I'm referencing "currSliceNo" as the 3rd dimension to k, rather than "noOfArrays".
Keep in mind that depending on how you will implement your copy/move detection, there may be some much "nicer" ways than rebuilding a large stack of little image tiles. It looks like convolution of a sample 5-by-5 image could be performed over the whole image at once, which I image will be both quicker and much less likely to run into memory issues for very large images.
  1 commentaire
Bob
Bob le 12 Fév 2013
Woah this is perfect, exactly what i wanted !! Thanks for the quick response, i really appreciate it. Wow this forum is really active. Yea i'm quite aware memory issues would be a problem, but since i'm totally new to this, running through 5 by 5 dimensional arrays of the image was the only thing i could think of :( I'll heed your advice and see if i can find any information regarding your suggestion though :D

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 11 Fév 2013
How about using mat2cell()?

the cyclist
the cyclist le 11 Fév 2013
Modifié(e) : the cyclist le 11 Fév 2013
In the line
k(:,:,noOfArrays)=(Agray(i:i+4,j:j+4));
you are alway writing to the same "plane" along the 3rd dimension of k, because noOfArrays is a constant. I expect you actually wanted to write to different planes, varying with i and j.
The simplest way to do that is probably to just have a counter for each pass through the loop.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by