8 neighbours of a pixel after converting to 1D array
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Sean de Wolski
el 28 de Feb. de 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
el 28 de Feb. de 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.
Respuesta aceptada
Jan
el 28 de Feb. de 2013
Editada: Jan
el 28 de Feb. de 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 comentarios
Jan
el 17 de Oct. de 2017
@Sajid Rahim: Do not post unrelated code in a thread of someone else, please. Open a new thread for you own problem.
Gilles
el 25 de Mzo. de 2020
Editada: Gilles
el 25 de Mzo. de 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
Más respuestas (2)
Image Analyst
el 28 de Feb. de 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.
0 comentarios
Sean de Wolski
el 28 de Feb. de 2013
Editada: Sean de Wolski
el 28 de Feb. de 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 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!