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
Bruno Luong
2018년 11월 4일
편집: Bruno Luong
2018년 11월 4일
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
2018년 11월 4일
I think your code is buggy. Solve the bug then you can ask for optimization.
Rik
2018년 11월 4일
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
2018년 11월 4일
Bruno Luong
2018년 11월 4일
Hate to repeat myself, now you correct v, but this looks buggy still:
[x,y]=min([v1,v2,v3,v4])
pb1106
2018년 11월 4일
Bruno Luong
2018년 11월 4일
편집: Bruno Luong
2018년 11월 4일
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
2018년 11월 4일
Bruno Luong
2018년 11월 4일
편집: Bruno Luong
2018년 11월 4일
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
