Can someone please help me implementing this code without loop ?

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

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]);
I think your code is buggy. Solve the bug then you can ask for optimization.
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.
I'm trying to implement a form of the Interest Point Operator in the V8 space for a pixel , given the equation that I wrote before.
Hate to repeat myself, now you correct v, but this looks buggy still:
[x,y]=min([v1,v2,v3,v4])
@Bruno Luong, I've edited in the correct form now
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)
Ok, I understand that I forgot the pre-allocation but what is the connection between pre-allocation and no-loop code ?
Bruno Luong
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.
pb1106
pb1106 am 4 Nov. 2018
Bearbeitet: pb1106 am 4 Nov. 2018
This is what I'm trying to implement

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

f = rand(4,5);
[m,n] = size(f);
x = 2:m-1;
y = 2:n-1;
v1 = max(abs(f(x+1,y)-f(x,y)),abs(f(x-1,y)-f(x,y)));
v2 = max(abs(f(x,y-1)-f(x,y)),abs(f(x,y+1)-f(x,y)));
v3 = max(abs(f(x-1,y+1)-f(x,y)),abs(f(x+1,y-1)-f(x,y)));
v4 = max(abs(f(x+1,y+1)-f(x,y)),abs(f(x-1,y-1)-f(x,y)));
vmin = min(cat(3,v1,v2,v3,v4),[],3);
alpha = 0.1;
[x,y] = find(vmin>alpha);
x = x+1;
y = y+1;
I_f = [x y]

Weitere Antworten (0)

Kategorien

Produkte

Version

R2018b

Gefragt:

am 4 Nov. 2018

Erneut geöffnet:

am 5 Nov. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by