8 neighbours of a pixel after converting to 1D array

5 ビュー (過去 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 Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by