Can anyone explain this coding?

%contrast measurement
clc;
clear all;
format long;
I1=imread('image11.jpg');
I2=double(I1);
A=I2;
clear vision;
k=1;
for i=1:10:(960-96);
for j=1:10:(1280-128);
m1=max(max(A(i:i+95,j:j+127)));
m2=min(min(A(i:i+95,j:j+127)));
vision(k)=(m1-m2)/(m1);
k=k+1
end;
end;
visionk=mean(vision)

 Risposta accettata

dpb
dpb il 4 Mag 2017
The variable visionk ends up holding the average of a measure of the distance (max-min) scaled by the max of the subsections of the image in 96x128 rectangular areas. These areas overlap since the loops are stepping by 10 instead of by 96/128. The max(max(...)) is a common Matlab idiom to get the overall maximum of a rectangular array as max() returns the maxima by column first, then the second call finds the maximum of that vector. This lets one get the overall max/min without either a temporary or using reshape() to transform to a vector.
There's quite a lot of suprerfluous "stuff" in the loop starting with the casting to double and then assigning I2 to A; might as well have just used I1 throughout.
The one real lack that hurts performance is that vision is not preallocated so it is reallocated on every pass through the loop.
vision=zeros(ceil((960-96)/10)*ceil((1280-128)/10),1);
before the loop would be beneficial.
Minor rewrite--it would probably be better to use size() to compute the limits, but don't know that there isn't a reason for the specific magic numbers here so will leave as "exercise for the student".
I=imread('image11.jpg');
vision=zeros(ceil((960-96)/10)*ceil((1280-128)/10),1);
k=0;
for i=1:10:(960-96)
for j=1:10:(1280-128)
k=k+1;
A=I(i:i+95,j:j+127); % select the subset
m1=max(A(:)); % overall max; eliminate double call
m2=min(A(:)); % and min ditto...
vision(k)=(m1-m2)/(m1);
end
end
visionk=mean(vision);

2 Commenti

thank you sir
ElleAlba
ElleAlba il 19 Ott 2017
Salam Sahirah, dapat jwpn x dari coding berkenaan?

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by