function exercise2(input_image, ax_outim_h, radius, lightsource, ... material) % This function segments the object with the chosen reflectance spectrum. % Its chromaticity is in % % material % % The lightsource chromaticity is in lightsource. % % radius is the value behind the button autosegment axes(ax_outim_h); % cast the image from 8 bit integer to double I = im2double(input_image); % get the size of the image [y,x,z]=size(I); % calculate the chromaticities RGB = I(:,:,1)+I(:,:,2)+I(:,:,3); r = I(:,:,1)./RGB; g = I(:,:,2)./RGB; b = I(:,:,3)./RGB; % pre calculate point line distance parameters r0 = lightsource; a = material - lightsource; a=a./norm(a); % if lightsource(1) < material(1) t1 = lightsource(1)+0.025; t2 = material(1)+radius; else t1 = material(1)-radius; t2 = lightsource(1)-0.025; end % segment by distance to line spanned between material and light source % iterate over all pixels for yi = 1:y for xi = 1:x r1=[r(yi,xi) g(yi,xi) b(yi,xi)]; dist = norm(cross(a,(r1-r0))); if RGB(yi,xi) > 0.1 if dist < radius if r(yi,xi) > t1 & r(yi,xi) < t2 input_image(yi,xi,1) = uint8(255); input_image(yi,xi,2) = uint8(0); input_image(yi,xi,3) = uint8(0); end end end end end % currently the input image is shown imshow(input_image) title('segmented image')