Can someone please help me implementing this code without loop ?
Ältere Kommentare anzeigen
Hello,
Can someone help me to speed up this code? Without loops
img = imread("my_img.png");
for i=2:256
for j=2:256
v1(i,j) = max(abs(img(i+1,j)-img(i,j)),abs(img(i-1,j)-img(i,j)));
v2(i,j) = max(abs(img(i,j-1)-img(i,j)),abs(img(i,j+1)-img(i,j)));
v3(i,j) = max(abs(img(i-1,j+1)-img(i,j)),abs(img(i+1,j-1)-img(i,j)));
v4(i,j) = max(abs(img(i+1,j+1)-img(i,j)),abs(img(i-1,j-1)-img(i,j)));
end
end
min_v1 = min(v1);
min_v2 = min(v2);
min_v3 = min(v3);
min_v4 = min(v4);
Thanks
10 Kommentare
Bruno Luong
am 4 Nov. 2018
Bearbeitet: Bruno Luong
am 4 Nov. 2018
Since your loop does store intermediate results, you don't need loop at all, just do the last one:
img = imread("my_img.png");
i=256
j=256
v1=max(abs(img(i+1,j)-img(i,j)),abs(img(i-1,j)-img(i,j)));
v2=max(abs(img(i,j-1)-img(i,j)),abs(img(i,j+1)-img(i,j)));
v3=max(abs(img(i-1,j+1)-img(i,j)),abs(img(i+1,j-1)-img(i,j)));
v4=max(abs(img(i+1,j+1)-img(i,j)),abs(img(i-1,j-1)-img(i,j)));
[x,y]=min([v1,v2,v3,v4]);
Bruno Luong
am 4 Nov. 2018
I think your code is buggy. Solve the bug then you can ask for optimization.
Rik
am 4 Nov. 2018
It looks like you are trying to implement something to do with a GLCM. You can check either the FEX or the native function to see if it is something you can use.
pb1106
am 4 Nov. 2018
Bruno Luong
am 4 Nov. 2018
Hate to repeat myself, now you correct v, but this looks buggy still:
[x,y]=min([v1,v2,v3,v4])
pb1106
am 4 Nov. 2018
Bruno Luong
am 4 Nov. 2018
Bearbeitet: Bruno Luong
am 4 Nov. 2018
Repeat myself a third time. Here is how your edited code works:
img = rand(4,5)
[m,n] = size(img);
for i=2:m-1
for j=2:n-1
v1(i,j) = max(abs(img(i+1,j)-img(i,j)),abs(img(i-1,j)-img(i,j)));
v2(i,j) = max(abs(img(i,j-1)-img(i,j)),abs(img(i,j+1)-img(i,j)));
v3(i,j) = max(abs(img(i-1,j+1)-img(i,j)),abs(img(i+1,j-1)-img(i,j)));
v4(i,j) = max(abs(img(i+1,j+1)-img(i,j)),abs(img(i-1,j-1)-img(i,j)));
end
end
min_v1 = min(v1)
min_v2 = min(v2)
min_v3 = min(v3)
min_v4 = min(v4)
img =
0.1544 0.8711 0.5306 0.2992 0.5583
0.3813 0.3508 0.8324 0.4526 0.7425
0.1611 0.6855 0.5975 0.4226 0.4243
0.7581 0.2941 0.3353 0.3596 0.4294
min_v1 =
0 0 0 0
min_v2 =
0 0 0 0
min_v3 =
0 0 0 0
min_v4 =
0 0 0 0
>>
The optimization code is very simple (no need for-loop at all)
min_v1 = zeros(1,n-1)
min_v2 = zeros(1,n-1)
min_v3 = zeros(1,n-1)
min_v4 = zeros(1,n-1)
pb1106
am 4 Nov. 2018
Bruno Luong
am 4 Nov. 2018
Bearbeitet: Bruno Luong
am 4 Nov. 2018
Preallocation or not it's still give outcome vectors containing only 0s.
At this point I'm not even sure the formula inside the for-loop is really what you want, since there are still errors somewhere else after 2/3 corrections.
I suggest you debug your code and post the version that works as you expected, then ask the question because we can't help you to remove a loop if we don't know what is the expected result.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Image Processing Toolbox finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
