COE 593.051 ML Tutorial 01
Please use cut-and paste to create the m-files.
Name the file imgproctutorial01.m
Create a directory and call it Imgs from the current working directory where the m-file is stored.
Copy image files from CD-1 (or any other source) into the Imgs directory to use for the experimentation.
-------- Please CUT between the lines:
function imgproctutorial01(FigNo, ImgPath, ImgFileName) % Online help for the function (when you type: help imgproctutorial01) % This tutorial shows how to read an image in a supported image file format % (e.g., TIFF, JPG, BMP). Different processing of the image is also shown. % Arguments: % FigNo: an integer (1, 2, …) for the starting image number for the figures generated % by the program. This helps keep multiple figures from different runs, for possible % future comparison. % ImgFileName: The image file name, enclosed in single quotes. % Example: % >> imgproctutorial01(1, ‘flowers.tif’); % >> k=1; ImgFileName = ‘flowers.tif’; imgproctutorial01(k, ImgFileName); % The second way is helpful for batch processing: % Include several such statements in a Matlab m-file, call it runtutorial01, % with a group of files for automatic processing with one Matlab command. % Author: Dr. Atef Jawad Al-Najjar % 2005-10-11, KFUPM, Dhahran, Saudi Arabia. % % Reading the image: % ImgFileName = 'flowers.tif'; % Read an image, and store the pixel data into matrix I LengIPN = length(ImgPath); if (LengIPN == 0) ImgPath = '.\Imgs\'; end %if-len.(ImgPath) LengIFN = length(ImgFileName); if (LengIFN == 0) % Argument is '', the NULL string ImgFileName = 'flower.bmp'; % Default image file name end % if-len.(IFN) ImgFN = [ImgPath, ImgFileName]; % Abreviation, or alias! IFN = ImgFileName; % Used as titles in figures I = imread(ImgFN); % I can be a 2-D (gray-scale) or 3-D matric (color) image. [Nr, Nc, Ncp] = size(I); % Nr = # rows, Nc = # Columns, Ncp = # color planes Nrby2 = ceil(Nr/2); Ncby2 = ceil(Nc/2); % Check the image. It is gray-scale if Ncp == 1. It is RGB if Ncp == 3 if Ncp == 1 % Gray-scale image Igray = I; % Copy image data to Igray Icolor = zeros(Nr, Nc, 3); % Make all planes zeros --> Black elseif Ncp == 3 % RGB-Color image Icolor = I; % Copy image data to Icolor Igray = rgb2gray(I); % Gray-color version of RGB-image end % if-Ncp currentFigNo = FigNo; figure(currentFigNo); % First use of FigNo fNr= 2; fNc = 2; % Figure # or rows & columns, respectively KK=1; subplot(fNr, fNc, KK); imshow(I), title(['RAW image: ', IFN]); KK=2; subplot(fNr, fNc, KK); imshow(Igray), title(['Gray image: ', IFN]); KK=3; subplot(fNr, fNc, KK); imshow(Icolor), title(['Color image: ', IFN]); dI = double(I); KK=4; subplot(fNr, fNc, KK); imhist(Igray), title(['Image histogram: ', IFN]), text(10, 2000, 'Converted to'), text(10, 1600, 'Gray-Scale'), text(10, 1200, 'representation'); % To process the image: Gray-scale % Break image into 4 quadrants: if Ncp == 1 % Gray-color image IgQ11 = Igray(1:Nrby2, 1:Ncby2); IgQ21 = Igray(Nrby2+1:end, 1:Ncby2); IgQ12 = Igray(1:Nrby2, Ncby2+1:end); IgQ22 = Igray(Nrby2+1:end, Ncby2+1:end); % Plot quadrants currentFigNo = currentFigNo + 1; figure(currentFigNo); fNr= 2; fNc = 2; KK=1; subplot(fNr, fNc, KK); imshow(IgQ11), title(['IgQ11: ', IFN]); KK=2; subplot(fNr, fNc, KK); imshow(IgQ12), title(['IgQ12: ', IFN]); KK=3; subplot(fNr, fNc, KK); imshow(IgQ21), title(['IgQ21: ', IFN]); KK=4; subplot(fNr, fNc, KK); imshow(IgQ22), title(['IgQ22: ', IFN]); end % if-Ncp==1 % To process the image: Color if Ncp == 3 % RGB-color image % First, color separation: Red, Green, Blue % Intialize Plane-Matrices R = zeros(Nr, Nc, Ncp); G = zeros(Nr, Nc, Ncp); B = zeros(Nr, Nc, Ncp); k=1; R(:,:,k) = Icolor(:,:,k); k=2; G(:,:,k) = Icolor(:,:,k); k=3; B(:,:,k) = Icolor(:,:,k); currentFigNo = currentFigNo + 1; figure(currentFigNo); fNr= 2; fNc = 2; KK=1; subplot(fNr, fNc, KK); imshow(Icolor), title(['RAW-Color image: ', IFN]); KK=2; subplot(fNr, fNc, KK); imshow(uint8(R)), title(['R-Plane: ', IFN]); KK=3; subplot(fNr, fNc, KK); imshow(uint8(G)), title(['G-Plane: ', IFN]); KK=4; subplot(fNr, fNc, KK); imshow(uint8(B)), title(['B-Plane: ', IFN]); % Break image into 4 quadrants: IcQ11 = Icolor(1:Nrby2, 1:Ncby2, :); IcQ21 = Icolor(Nrby2+1:end, 1:Ncby2, :); IcQ12 = Icolor(1:Nrby2, Ncby2+1:end, :); IcQ22 = Icolor(Nrby2+1:end, Ncby2+1:end, :); % Plot quadrants currentFigNo = currentFigNo + 1; figure(currentFigNo); fNr= 2; fNc = 2; KK=1; subplot(fNr, fNc, KK); % , 'align'); imshow(uint8(IcQ11)), title(['IcQ11: ', IFN]); KK=2; subplot(fNr, fNc, KK); %, 'align'); imshow(uint8(IcQ12)), title(['IcQ12: ', IFN]); KK=3; subplot(fNr, fNc, KK); %, 'align'); imshow(uint8(IcQ21)), title(['IcQ21: ', IFN]); KK=4; subplot(fNr, fNc, KK); % , 'align'); imshow(uint8(IcQ22)), title(['IcQ22: ', IFN]); % Planes as 2-D matrices: R2d = Icolor(:,:,1); G2d = Icolor(:,:,2); B2d = Icolor(:,:,3); currentFigNo = currentFigNo + 1; figure(currentFigNo); fNr= 2; fNc = 2; KK=1; subplot(fNr, fNc, KK); imshow(uint8(Icolor)), title(['RAW-Color image: ', IFN]); KK=2; subplot(fNr, fNc, KK); imshow(uint8(R2d)), title(['R-Plane 2D: ', IFN]); KK=3; subplot(fNr, fNc, KK); imshow(uint8(G2d)), title(['G-Plane 2D: ', IFN]); KK=4; subplot(fNr, fNc, KK); imshow(uint8(B2d)), title(['B-Plane 2D: ', IFN]); % Histograms of each color plane: currentFigNo = currentFigNo + 1; figure(currentFigNo); fNr= 3; fNc = 2; KK=1; subplot(fNr, fNc, KK); imshow(uint8(R2d)), title(['R-Plane 2D: ', IFN]); KK=2; subplot(fNr, fNc, KK); imhist(uint8(R2d)), title(['R-Hist 2D: ', IFN]); KK=3; subplot(fNr, fNc, KK); imshow(uint8(G2d)), title(['G-Plane 2D: ', IFN]); KK=4; subplot(fNr, fNc, KK); imhist(uint8(G2d)), title(['G-Hist 2D: ', IFN]); KK=5; subplot(fNr, fNc, KK); imshow(uint8(B2d)), title(['B-Plane 2D: ', IFN]); KK=6; subplot(fNr, fNc, KK); imhist(B2d), title(['B-Hist 2D: ', IFN]); % Edge detection; For each Plane as 2D: % Use R2d, G2d, and B2d EdgeR2d = edge(R2d, 'canny'); EdgeG2d = edge(G2d, 'canny'); EdgeB2d = edge(B2d, 'canny'); currentFigNo = currentFigNo + 1; figure(currentFigNo); fNr= 3; fNc = 2; KK=1; subplot(fNr, fNc, KK); imshow(uint8(R2d)), title(['R-Plane 2D: ', IFN]); KK=2; subplot(fNr, fNc, KK); imshow(uint8(EdgeR2d)), title(['R-Edge; canny 2D: ', IFN]); KK=3; subplot(fNr, fNc, KK); imshow(uint8(G2d)), title(['G-Plane 2D: ', IFN]); KK=4; subplot(fNr, fNc, KK); imshow(uint8(EdgeG2d)), title(['G-Edge; canny 2D: ', IFN]); KK=5; subplot(fNr, fNc, KK); imshow(uint8(B2d)), title(['B-Plane 2D: ', IFN]); KK=6; subplot(fNr, fNc, KK); imshow(uint8(EdgeB2d)), title(['B-Edge; canny 2D: ', IFN]); % The HSV color model % Use rgb2hsv() function to transform a color-image Ihsv = rgb2hsv(Icolor); Ihue = Ihsv(:,:,1); Isat = Ihsv(:,:,2); Ival = Ihsv(:,:,3); % Plot RGB plannes vs. HSV plances: currentFigNo = currentFigNo + 1; figure(currentFigNo); fNr= 3; fNc = 2; KK=1; subplot(fNr, fNc, KK); imshow(R2d), title(['R-Plane 2D: ', IFN]); KK=2; subplot(fNr, fNc, KK); imshow(Ihue), title(['I-Hue; 2D: ', IFN]); KK=3; subplot(fNr, fNc, KK); imshow(G2d), title(['G-Plane 2D: ', IFN]); KK=4; subplot(fNr, fNc, KK); imshow(Isat), title(['I-Saturation; 2D: ', IFN]); KK=5; subplot(fNr, fNc, KK); imshow(B2d), title(['B-Plane 2D: ', IFN]); KK=6; subplot(fNr, fNc, KK); imshow(Ival), title(['I-Value; 2D: ', IFN]); Iyiq = rgb2ntsc(Icolor); % The HSV color model % Use rgb2ntsc() function to transform a color-image IYntsc = Iyiq(:,:,1); IIntsc = Iyiq(:,:,2); IQntsc = Iyiq(:,:,3); % Plot RGB plannes vs. YIQ_NTSC plances: currentFigNo = currentFigNo + 1; figure(currentFigNo); fNr= 3; fNc = 2; KK=1; subplot(fNr, fNc, KK); imshow(R2d), title(['R-Plane 2D: ', IFN]); KK=2; subplot(fNr, fNc, KK); imshow(IYntsc), title(['IYntsc; 2D: ', IFN]); KK=3; subplot(fNr, fNc, KK); imshow(G2d), title(['G-Plane 2D: ', IFN]); KK=4; subplot(fNr, fNc, KK); imshow(IIntsc), title(['IIntsc; 2D: ', IFN]); KK=5; subplot(fNr, fNc, KK); imshow(B2d), title(['B-Plane 2D: ', IFN]); KK=6; subplot(fNr, fNc, KK); imshow(IQntsc), title(['IQntsc; 2D: ', IFN]); % YCBCR = rgb2ycbcr(RGB): Y: Luminance, Cb and Cr: Color components I_YCbCr = rgb2ycbcr(Icolor); % Plot the image in RGB, HSV, and YIQ_NTSC currentFigNo = currentFigNo + 1; figure(currentFigNo); fNr= 4; fNc = 1; KK=1; subplot(fNr, fNc, KK); imshow(Icolor), title(['RGB: ', IFN]); KK=2; subplot(fNr, fNc, KK); imshow(Ihsv), title(['HSV: ', IFN]); KK=3; subplot(fNr, fNc, KK); imshow(uint8(Iyiq)), title(['YIQ_{NTSC}: ', IFN]); KK=4; subplot(fNr, fNc, KK); imshow(I_YCbCr), title(['IYCbCr: ', IFN]); end % if-Ncp==3