Li Feifei视觉课程作业H00

%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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值