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

조회 수: 1 (최근 30일)
Bob
Bob 2013년 2월 11일
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 :)

채택된 답변

Sven
Sven 2013년 2월 11일
편집: Sven 2013년 2월 11일
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
Bob
Bob 2013년 2월 12일
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

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2013년 2월 11일
How about using mat2cell()?

the cyclist
the cyclist 2013년 2월 11일
편집: the cyclist 2013년 2월 11일
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