8 neighbours of a pixel after converting to 1D array
Show older comments
Hi
Is there a simple way to access the 8 neighbours of a pixel x(i,j) given by x(i-1,j-1), x(i-1,j), x(i-1,j+1), x(i,j-1), ... ,x(i+1,j+1) after converting x into a 1D array? I have been using for loops to achieve this but it's quite slow if the matrix x is large.
Thanks.
6 Comments
Sean de Wolski
on 28 Feb 2013
Not sure what you mean. Do you, for example, have a 100x100 matrix that you turn into 10000x1 vector and you want to find the neighbors of pixel (24,46)?
Image Analyst
on 28 Feb 2013
Edited: Image Analyst
on 28 Feb 2013
I'm assuming he is scanning the image and for each pixel, he has created an 8 element 1D array, and he wants to know how to access those elements of his 1D array (because he says "after converting"). He did not ask HOW to create the 1D array - and he seems to imply that he needs to know what to do AFTER it's been created, though that seems rather obvious to me.
zepp
on 28 Feb 2013
Sean de Wolski
on 28 Feb 2013
@Sriram, read my answer. It addresses both of your concerns.
Use the two for-loops on the matrix and keep the column vector also for your column vector needs.
Image Analyst
on 28 Feb 2013
Thanks for the clarification. So your question should have read: "access the 8 neighbours of a pixel x(i,j) given by x(i-1,j-1), x(i-1,j), x(i-1,j+1), x(i,j-1), ... ,x(i+1,j+1) and convert them into a 1D array".
Saying "after converting x into a 1D array" was imprecise since you did not have this 1D array yet, and you never converted x - the entire x - into a 1D array.
Accepted Answer
More Answers (2)
Image Analyst
on 28 Feb 2013
Yes, To access the 3rd element of your 1D array, you do
thirdElement = array1D(3)
Is that what you meant? It seems like what you asked. It does not depend on the size of x obviously, just on the size of the "ring", so a 3x3 window would have 8 elements, a 5x5 would have 18, and so on.
Sean de Wolski
on 28 Feb 2013
Edited: Sean de Wolski
on 28 Feb 2013
As I mentioned in the comment: I would just reshape the image to its original (or leave it alone) and make a second variable that is the column vector:
x = rand(10000);
x2 = reshape(x,numel(x),1);
Does not require a deep copy of x so the data is only stored in memory once. And reshape of full matrices is up there on the list of the fastest MATLAB functions. You're certainly not paying any penalties to do what I did above.
5 Comments
zepp
on 28 Feb 2013
Sean de Wolski
on 28 Feb 2013
I doubt vectorization will not be faster than nested for-loops here.
What I am saying is that the overhead you will pay for figuring things out in a column vector will cost much more than using a matrix where you kinda get this spatial information for free. The for-loops will cruise over it and in the time we've you've spent looking into this (which is great, by the way +1!) MATLAB could've done this for millions of matrix elements like this, literally. With parfor loop you could probably get even more!
zepp
on 28 Feb 2013
Sean de Wolski
on 28 Feb 2013
How many images do you actually have and how big are they?
zepp
on 28 Feb 2013
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!