%reads in the image, converts it to grayscale, and converts the intensities
%from uint8 integers to doubles. (Brightness must be in 'double' format for
%computations, or else MATLAB will do integer math, which we don't want.)
I = imread('u2dark.png');
dark = double(rgb2gray(I));
figure(1);
imshow(imread('u2dark.png'));
figure(2);
imshow(dark);
%%%%%% Your part (a) code here: calculate statistics
%%%%%% get averge value pixel
[cols, rows] = size(dark);
s = 0;
for x = 1:cols
for y = 1:rows
s = s + dark(x,y);
end
end
averge_pix = s/(cols*rows);
display(averge_pix);
%%%%%%% get the max and min value of pixel
min = dark(1,1);
max = dark(1,1);
for x = 1:cols
for y = 1:rows
if(dark(x,y) < min)
min = dark(x,y);
end
if(dark(x,y)> max)
max = dark(x,y);
end
end
end
display(min);
display(max);
%%%%%% Your part (b) code here: apply offset and scaling
% fixedimg = [];
fixedimg = I;
for i=1:cols
for j=1:rows
fixedimg(i,j) = uint8(((fixedimg(i,j) - 25)/128) * 255);
end
end
figure(3),imshow(fixedimg);
%displays the image
imshow((fixedimg));
% display(fixedimg);
%%%%%% Your part (c) code here: apply the formula to increase contrast,
% and display the image
contrasted = I;
for i=1:cols
for j=1:rows
contrasted(i,j) = 2*(contrasted(i,j) - 128) + 128;
end
end
figure(4),imshow(contrasted);
% This function calls the functions below and displays their results.
% You don't need to edit it.
function edgedetector()
img = double(rgb2gray(imread('buoys.jpg')));
edges = DetectVerticalEdges(img);
blurred_edges = BoxBlur(edges);
figure('Name','Original Image')
imshow(img, []);
figure('Name','Edges')
imshow(edges, []);
figure('Name','Blurred Edges')
imshow(blurred_edges, []);
end
% Returns a matrix containing the horizontal component of the gradient at every
% image location.
function edges = DetectVerticalEdges(img)
% MATLAB images use matrix indices, so the order is (y,x), and the +y
% direction is downward.
[cols,rows] = size(img);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Your part (a) code here. You can accomplish part (a) with
%%%%%%%%%% nested "for" loops, but an easier way is to use matrix
%%%%%%%%%% indexing to make a matrix of the "left" pixels and a matrix
%%%%%%%%%% of the "right" pixels, and subtract the two matrices.
%%%%%%%%%% REMEMBER: left/right position is the SECOND index in MATLAB.
edges = zeros(cols, rows-1);
[x, y] = size(edges);
for i = 2:x
for j = 1:y
edges(i-1, j) = img(i, j) - img(i-1, j);
end
end
%%%%%%%%%% End of your part (a) code.
end
% Applies a box blur to the input image and returns the result.
function blurred = BoxBlur(img)
img = double(img);
height = size(img, 1);
width = size(img, 2);
n=5; % width of the blur
blurred = zeros(height-(n-1),width-(n-1));
% Loop through each pixel location in the result
for x=1:height-(n-1)
for y=1:width-(n-1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Your part (b) code here. Calculate blurred(y,x).
sum = 0;
for i=0:(n-1)
sum = sum + img(x + i, y + i);
end
blurred(x,y) = sum/(n^2);
%%%%%%% End of part (b) code
end
end
% Usually we'll divide at the end so that we don't make the image
% brighter:
% blurred = blurred / n^2;
end