Calculate the difference between adjacent pixels

Hello Team,
I have written this Matlab code to calculate the absolute difference between horizontally adjacent pixels.
imageArray = imread('index.jpg');
information = imfinfo('index.jpg')
for j = 1:1:information.Height - 1
for i = 1:1:information.Width - 2
D_hor(i,j) = abs(imageArray(i,j) - imageArray(i+1, j))
i = i +1
end
j = j+1
end
Once I started verifying the code using a simple Matrix, it displayed incorrect results.
Can you please help me?
Regards,

 採用された回答

jonas
jonas 2018 年 9 月 30 日

0 投票

Try this instead

abs(diff(imageArray,1,2)) 

7 件のコメント

Matlab Student
Matlab Student 2018 年 9 月 30 日
It works, thanks you Jonas.
jonas
jonas 2018 年 9 月 30 日
Happy to help!
Matlab Student
Matlab Student 2018 年 10 月 2 日
Hello again Jonas,
I have one more question please. The code should work fine even without the i=i+1 and j=j+1 statements cause the for loop statement controls the step by 1 "for i=1:1:information.height-1" (:1: should do the job).
Am I right?
imageArray = imread('index.jpg');
information = imfinfo('index.jpg')
for j = 1:1:information.Height - 1
for i = 1:1:information.Width - 2
abs(diff(imageArray,1,2))
end
end
jonas
jonas 2018 年 10 月 2 日
編集済み: jonas 2018 年 10 月 2 日
Hi! You do not need the loops at all in this case. This is enough:
imageArray = imread('index.jpg');
information = imfinfo('index.jpg')
abs(diff(imageArray,1,2))
But yes, in general you are correct in that you do not need those lines at the end of for loops to adjust the looping variable.
Matlab Student
Matlab Student 2018 年 10 月 2 日
Thank you (flower)
Matlab Student
Matlab Student 2018 年 10 月 3 日
Good Morning Jonas,
May I ask how can I calculate the same in the vertical direction? I mean how to calculate the difference between vertically adjacent pixels?
Thanks in advance,
jonas
jonas 2018 年 10 月 3 日
Just change the dimension argument from 2 to 1 :)
abs(diff(imageArray,1,1))

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2018 年 9 月 30 日

コメント済み:

2018 年 10 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by