8 neighbours of a pixel after converting to 1D array

조회 수: 4 (최근 30일)
zepp
zepp 2013년 2월 28일
편집: Gilles 2020년 3월 25일
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에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by