8 neighbours of a pixel after converting to 1D array

3 次查看(过去 30 天)
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 个评论
Sean de Wolski
Sean de Wolski 2013-2-28
@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 2013-2-28
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.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2013-2-28
编辑:Jan 2013-2-28
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 个评论
Jan
Jan 2017-10-17
@Sajid Rahim: Do not post unrelated code in a thread of someone else, please. Open a new thread for you own problem.
Gilles
Gilles 2020-3-25
编辑:Gilles 2020-3-25
@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

请先登录,再进行评论。

更多回答(2 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by