📄 digitalimages.m
字号:
%% Digital Images and Measures
%
% *Wavelets Workshop*
%
% June 4-7, 2008
%
% University of St. Thomas
%
% *Catherine Beneteau*, *Caroline Haddad*, *David Ruch*, *Patrick Van Fleet*
%% Objectives
% The purpose of this notebook is to give you a brief introduction to the
% DiscreteWavelets Toolbox and show you how to use it to load
% images. Some basic image manipulation is illustrated as well. You will
% also learn how to use measures and tools such as cumulative energy,
% entropy, PSNR, and Huffman coding.
%% Help on the DiscreteWavelets Toolbox
%
% Help for the toolbox is available by clicking on Help and then Product
% Help (or press F1) and then clicking on the DiscreteWavelets Toolbox.
% Several demos and examples are available as well by clicking on the Demos
% tab on the Help menu.
%% Image Basics
%
% The DiscreteWavelets Toolbox comes with 18 grayscale images and 9 color
% images for you to use. There are three functions available to tell you more about these images.
%
% The first function is called |ImageList|. This function can tell you the
% names and sizes of the digital images in the Toolbox.
ImageList('ImageType','GrayScale')
ImageList('ImageType','Color')
ImageList()
%%
%
% The second function is called |ImageNames|. This function returns a list
% of absolute path names to each image. Since the Toolbox can be installed
% in multiple locations, this functions takes the work out of locating the
% images.
color=ImageNames('ImageType','Color')
%%
%
% You'll notice a list of 9 path/file names. If you want to use
% building.png in an application (the second name in the list), you simply
% use |color{2}|.
color{2}
%%
% From |ImageList|, you probably noticed that the images were quite large.
% Sometimes it's handy to work with smaller images. If you set the
% directive |ListThumbnails| to |True|, then the file names returned are
% those of the thumbnails.
color=ImageNames('ImageType','Color','ListThumbnails','True');
color{2}
%%
%
% If you wish to view thumbnails of all images included in the Toolbox, use
% the |ShowThumbnails| command. You can specify the |ImageType| as either
% |GrayScale| or |Color|. If no image type is given the function shows
% thumbnails of all included images.
ShowThumbnails('ImageType','GrayScale')
ShowThumbnails('ImageType','Color')
%% Loading a Digital Image
%
% DiscreteWavelets comes with a command called |ImageRead|. This command
% essentially needs one argument - the location, either on a computer or
% on the internet, of a digital image. In the case of a grayscale image,
% |ImageRead| returns a matrix (named A in the commands below) containing
% the gray scale intensity values for each pixel.
%
% Here are some examples.
gray = ImageNames('ImageType','GrayScale');
A = ImageRead(gray{1});
[rows cols]=size(A);
str=sprintf('The dimensions of A are %i x %i.',rows,cols);
disp(str);
%%
%
% You can read in the thumbnail images too.
%
gray = ImageNames('ImageType','GrayScale','ListThumbnails','True');
A = ImageRead(gray{1});
[rows cols]=size(A);
str=sprintf('The dimensions of A are %i x %i.',rows,cols);
disp(str);
%%
% Here is the first row of A
A(1,:)
%%
%
% There are two additional directives for |ImageRead|. The first is called
% |PrintInfo| and if set to |True|, will give you the dimensions and type
% (grayscale, color) of image you have read. The |PowersOfTwo| option, if
% set (say to K) will chop off enough rows on the bottom and columns on the
% left to make the dimensions of the image divisible by 2^K. This is
% really handy when students pull their own images off the web and we need
% the dimensions to be divisible by 2^K.
[gray color] = ImageNames('ImageType','All','ListThumbnails','True');
A = ImageRead(gray{2},'PrintInfo','True');
B = ImageRead(color{3},'PrintInfo','True');
A = ImageRead(gray{3},'PrintInfo','True','PowersOfTwo',6);
[rows cols] = size(A);
str=sprintf('The new dimensions of A are %i x %i.',rows,cols);
disp(str);
%%
% Note that B was processed as a color image. This means that B returned
% three matrices - the first matrix is the red portion of the image (with
% values 0 to 255), the second matrix is the green portion of the image,
% and the third matrix is the blue portion of the image.
%
% Notation-wise, we have the matrices B(:,:,1) (red), B(:,:,2) (green),
% and B(:,:,3) (blue), but it might be easier to make the call using
% |Split3D|:
B=ImageRead(color{3},'PrintInfo','True','PowersOfTwo',6);
[red, green, blue]=Split3D(B);
size(red)
%% Plotting a Digital Image
%
% It is easy to plot digital images read with |ImageRead| in Matlab.
% The command is called |ImagePlot|.
%
% Once you have an image loaded via ImageRead, here's what you can do:
ImagePlot(A);
B=Make3D(red,green,blue);
ImagePlot(B);
%%
% You can plot individual channels of a color image if you like. It will
% be grayscale by default, but if you want to see it in its original color,
% simply add the directive |ChannelColor|:
ImagePlot(red);
figure;
ImagePlot(red,'ChannelColor',[1 0 0]); %Plot the red channel
figure;
ImagePlot(green,'ChannelColor',[0 1 0]); %Plot the green channel
figure;
ImagePlot(B);
%% Reading and Plotting Images from the Internet
%
% It is quite easy to read and plot images from the internet. All you need
% is a connection to the internet and a url for the image. You can find
% the url for an image in Internet Explorer by simply right-clicking on it
% and then clicking on Properties:
A=ImageRead('http://math.usf.edu/images/people/faculty/beneteau.jpg','PrintInfo','True','PowersOfTwo',2);
ImagePlot(A); %Plot the image
%%
% *Warning*: Most of our applications will require the dimensions of the
% image to be divisible by some power of 2. Use the |PowersOfTwo|
% directive to appropriately resize internet images. Images that appear
% as grayscale are often stored as color images on the internet. Use the
% |PrintInfo| directive to check this before using internet images in
% applications.
%% Manipulating Images
%
% Once you have an image loaded and know how to plot it, you can do all
% kinds of things with it.
%
% Convert color to grayscale:
color=ImageNames('ImageType','Color','ListThumbnails','True');
A=ImageRead(color{3});
ImagePlot(A);
figure;
[red, green, blue]=Split3D(A);
gray=(red+green+blue)/3;
ImagePlot(gray);
%%
% Darken the image
ImagePlot(A/2);
%%
% Image negative
ImagePlot(255-gray);
%%
% Color negative
ImagePlot(255-A);
%%
% Flip it upside down
ImagePlot(flipud(gray));
%%
% What does the transpose do?
ImagePlot(gray');
%%
% The previous cell is basically a rotation of the image counterclockwise
% 90 degrees. In the cell below, can you write code to rotate the image
% clockwise 90 degrees?
%%
% Place your answer here. To type an answer, start a line with %. Matlab
% commands are entered as usual.
%% Converting Color Images to YCbCr Space
%
% DiscreteWavelets contains a command for converting from RGB color space
% to YCbCr space. The command is called |RGBToYCbCr|. Here is an example
% call:
color=ImageNames('ImageType','Color'); %Get names of all color images included with the toolbox
A=ImageRead(color{5}); %Read a color image
ImagePlot(A); %Plot the image
[R,G,B]=Split3D(A); %Split A into the three color channels
figure
ImagePlot(R,'ChannelColor',[1 0 0]); %Plot the red channel
figure
ImagePlot(G,'ChannelColor',[0 1 0]); %Plot the green channel
figure
ImagePlot(B,'ChannelColor',[0 0 1]); %Plot the blue channel
%%
% Now convert to YCbCr space and plot the resulting images
B=RGBToYCbCr(A,'DisplayMode','True'); %Do the color space conversion
[Y,Cb,Cr]=Split3D(B); %Split B into the three channels
figure
ImagePlot(Y,'Title','Y Channel'); %Plot the Y channel
figure
ImagePlot(Cb,'Title','Cr Channel'); %Plot the Cr channel
figure
ImagePlot(Cr,'Title','Cb Channel'); %Plot the Cb channel
%%
% Note that we have used the directive |DisplayMode| set to |True|. This
% maps the values of the Y, Cb, Cr channels from [0,1], [-1/2,1/2],
% [1/2,1/2] to integer intervals in the range [0,255].
%%
% You can convert back to RGB space using the command |YCbCrToRGB|:
A=YCbCrToRGB(B,'DisplayMode','True'); %Do the color space conversion
[R,G,B]=Split3D(A); %Split A into the three channels
figure
ImagePlot(R,'ChannelColor',[1 0 0]); %Plot the red channel
figure
ImagePlot(G,'ChannelColor',[0 0 1]); %Plot the green channel
figure
ImagePlot(R,'ChannelColor',[0 0 1]); %Plot the blue channel
figure
ImagePlot(A);
%% Exercises
%
%
%%
% *Exercise 1*
%
% In the cell below, find and plot the grayscale image of the clown that
% comes with the DiscreteWavelets Toolbox.
%%
% Place your answer here. To type an answer, start a line with %. Matlab
% commands are entered as usual.
%%
% *Exercise 2*
% In the cell below, find and plot the red, green, and blue color channels
%(when reading the image, name the three matrices red, green, and blue) of
% the image of the goats that comes with the DiscreteWavelets Toolbox.
%%
% Place your answer here. To type an answer, start a line with %. Matlab
% commands are entered as usual.
%%
% *Exercise 3*
%
% In an example above, we converted a color image to grayscale by simply
% averaging the channels. The National Television System Committee
% suggests using a weight average of these three channels for this
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -