MATLAB: How does the the division of image by 255 work and multiplication of one image with another? Need help understanding the bold part of the code. This code is complete and can run in MATLAB.

digital image processinggraded circlehow to make a graded circleimage processing

for i = 1:256
for j = 1:256
R(i,j) = j-1;
end
end
colormap(gray(256));
I = zeros(256,256);
for i=1:256
for j=1:256
dist = sqrt((i-128)^2 +(j-128)^2);
if(dist<80)
I(i,j) = 255;
else
I(i,j) = 0;
end
end
end
colormap(gray(256))
for i=1:256
for j=1:256
G(i,j)= R(i,j).*I(i,j)/255; %HERE
end
end
colormap(gray(256));
axis('image');
image(G)

Best Answer

You create the matrix I with the values 0 and 255. Finally you divide the elements of I by 255, which is exactly the same as creating the matrix with the values 0 and 1 directly. Then elements of R and I are multiuplied elementwise (not a matrix multiplication). This keeps the values of R in all elements, which have a 1 in I, and set the output to 0 otherwise.
By the way: This is a more matlabish version of your code using vectorization:
R = repmat(0:255, 256, 1);
v = (-127:128).^2;
dist2 = bsxfun(@plus, v, v.');
I = dist2 < 80*80; % Omit the expensive SQRT()
G = R .* I;
axis('image');
image(G)
colormap(gray(256));