8 neighbours of a pixel after converting to 1D array

3 views (last 30 days)
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
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
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.

Sign in to comment.

Accepted Answer

Jan
Jan on 28 Feb 2013
Edited: Jan on 28 Feb 2013
w = 200;
h = 100;
x = rand(h, w);
xv = x(:); % A very fast reshape
pattern = [1,2,3, h+1, h+3, 2*h+1, 2*h+2, 2*h+3];
focus = h + 2;
% Neighborhood of the pixel at position (2,2):
xv(pattern) - xv(focus)
% Next pixel:
pattern = pattern + 1;
focus = focus + 1;
xv(pattern) - xv(focus)
% etc. This would happen in a loop of course.
% Consider the edges: pattern = pattern + 3
I cannot create the required loops, because I do not know the wanted output style.
  6 Comments
Jan
Jan on 17 Oct 2017
@Sajid Rahim: Do not post unrelated code in a thread of someone else, please. Open a new thread for you own problem.
Gilles
Gilles on 25 Mar 2020
Edited: Gilles on 25 Mar 2020
@Jan, Thanks a lot for this usefull code to find neighboor indices of a pixel after converting a matrix into an 1D array.
Do you know the formules to generalize this code for kernel of large size ?
I want to find the neighboors indices for :
  • 5x5 kernel --> 24 neighboors
  • 7x7 kernel --> 48 neighboors
Thanks in advance

Sign in to comment.

More Answers (2)

Image Analyst
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
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
Sean de Wolski
Sean de Wolski on 28 Feb 2013
How many images do you actually have and how big are they?
zepp
zepp on 28 Feb 2013
200 images and each is 640x480. I have to run it for different neighbourhood sizes (3x3, 5x5, 7x7) etc.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!