教程1:Matlab图像处理基础(一)

本周主要任务:完成下面作业

作业1: 逐行在Matlab里面实现下列代码

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Introduction to Matlab 
% (adapted from https://cs.brown.edu/courses/cs143/docs/matlab-tutorial/)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Symbol "%"is used to comment a line
%% Helpful commands
clc;
clear all;
close all;

%% Basics
% data type
5       % integer 32-bit
3.1415  % double 64-bit
isfloat(5.0) 

% Arithmetic 
1 + 2
1 / 2
2 ^ 5

% Logic 
1 == 0
1 ~= 0
1 && 0
1 || 0

% variable
a = 4                   % displays output
b = 0.5;                % ";" suppresses output
c = a ^ b

%% Vector and matrix, Basic
% vector
s = 5                   % A scalar
r = [1 2 3]             % A row vector
c = [4; 5; 6]           % A column vector

v = r'
v = 1:3                 % from 1 to 3, with a default size of 1
v = 1:.5:3              % from 1 to 3, with a step size of 0.5

% matrix
A = [1 2 3;4 5 6]   % 2x3 matrix, ";" separates row, "," separates column (optional)
B = zeros(2,4)      % 2x4 matrix filled with zero
B = ones(2,4)       % 2x4 matrix filled with one
C = eye(3)          % 3x3 identity matrix


% indexing
v= 1:.5:3
v(1)                % vector element
A = [1 2 3 4; 5 6 7 8; 9 10 11 12]
A(2,3)              % matrix element
A(2,:)              % 2nd row 
A(2:end,2)          % [6;10;14]

size(A)
size(A, 1)          % number of rows
size(A, 2)          % number of columns
numel(A)            % number of elements

%% Vector and matrix, operations
% vector
a = 1:3
b = 4:6
a + b

2 * a               % scalar multiplication
a .* b              % element-wise multiplication
a * b'              % vector multiplication

% built-in functions
sum(a)                       % Sum of vector elements
mean(a)                      % Mean of vector elements
var(a)                       % Variance of elements
std(a)                       % Standard deviation
max(a)                       % Maximum
min(a)                       % Minimum
log(a)                       % Element-wise logarithm

% matrix
A = [1 2; 3 4; 5 6];         % A 3x2 matrix
B = [5 6 7];                 % A 1x3 row vector
B * A                        % Vector-matrix product results in
                             %   a 1x2 row vector
                             
B = A(:)                     % 6x1 column vector 
max(B(:))                    % max of all elements

A = reshape(B, 2, 3)
C = repmat(B, 1, 2)

[U, S, V] = svd(A)
%% Control flow
% Copy positive elements from A to B
% Implementation using loops:
A = [1 -1 1; -1 1 -1; 1 -1 1]
[m,n] = size(A);
B = zeros(m,n);
for i=1:m
  for j=1:n
    if A(i,j)>0
      B(i,j) = A(i,j);
    end
  end
end
B
% All this can be computed w/o any loop!
B = zeros(m,n);
ind = find(A > 0);           % Find indices of positive elements of A 
                             %   (see "help find" for more info)
B(ind) = A(ind);             % Copies into B only the elements of A
                             %   that are > 0
B
%% Image Processing
img = imread('superman.png');
imshow(img);
MINGREEN = 173; MAXGREEN = 173; 
mask = ~(MINGREEN <= img(:,:,2) & img(:,:,2) <= MAXGREEN);
img_new = repmat(mask,[1 1 3]) .* double(img);
figure;
imshow(uint8(img_new));
%% Plot
figure;                      % Open new figure
x = pi*[-24:24]/24;          % 1x49 row vec
plot(x, sin(x));
xlabel('radians');           % Assign label for x-axis
ylabel('sin value');         % Assign label for y-axis
title('Sin wave');           % Assign plot title

作业2:(superman, space的图片附在后面)

vectorize = false;

% Read in the images. They must be the same size.
superman = imread('superman.png');
space = imread('space.png');

characterImg = superman;
backgroundImg = space;

% Create a matrix matching the dimensions of characterImg.
outputImg = zeros(size(characterImg));
height = size(characterImg, 1);
width = size(characterImg, 2);

% By trial and error, I determined that the green screen pixels are within
% these color values.
MINRED = 10; MINGREEN = 100; MINBLUE = 10;
MAXRED = 160; MAXGREEN = 220; MAXBLUE = 110;

if ~vectorize
    % The straightforward implementation using nested for loops.
    for y = 1:height
        for x = 1:width
            redMatch = MINRED <= superman(y, x, 1) && superman(y, x, 1) <= MAXRED;
            greenMatch = MINGREEN <= superman(y, x, 2) && superman(y, x, 2) <= MAXGREEN;
            blueMatch = MINBLUE <= superman(y, x, 3) && superman(y, x, 3) <= MAXBLUE;
            match = redMatch && greenMatch && blueMatch;
            if match
                outputImg(y,x,:) = backgroundImg(y,x,:);
            else
                outputImg(y,x,:) = characterImg(y,x,:);
            end
        end
    end
else
    % The advanced, fast implementation using matrix math.

    % Make a 2D array that holds a '1' where red is within bounds.
    redMatchImg = (MINRED <= superman(:,:,1)) & (superman(:,:,1) <= MAXRED);
    greenMatchImg = MINGREEN <= superman(:,:,2) & superman(:,:,2) <= MAXGREEN;
    blueMatchImg = MINBLUE <= superman(:,:,3) & superman(:,:,3) <= MAXBLUE;

    % Holds a '1' where all colors matched the green screen.
    matchImg = redMatchImg & greenMatchImg & blueMatchImg;

    % Duplicate the matrix to convert it from 480x640 to 480x640x3.
    % This makes it easy to do an element-by-element multiplication with the images.
    matchImg = repmat(matchImg, [1 1 3]);

    % Make another matrix which is '1' everywhere the original was '0', and vice versa
    nonMatchImg = ~matchImg;

    % Now, multiply the character image by '1' where there's no green screen, and
    % multiply the background by '1' where there IS green screen. This
    % replaces the "if" statement above.
    outputImg = double(characterImg) .* nonMatchImg + double(backgroundImg) .* matchImg;

    % The cast to double is to deal with a MATLAB quirk: images are read in
    % as arrays of type "uint8" (a.k.a. byte). But MATLAB generally needs to work in
    % doubles. So we'll convert to double to do math, and then convert back
    % to uint8, below, to display.
end

% Note that, if the image color format is 0 to 255, we need to cast to uint8 for it to display.
imshow(uint8(outputImg), []);

space.jpg图片如下:
教程1:Matlab图像处理基础(一)_第1张图片

superman.jpg 图片如下:
教程1:Matlab图像处理基础(一)_第2张图片

你可能感兴趣的:(教程1:Matlab图像处理基础(一))