1. Presented by:Presented by:
Dr. Moe Moe Myint
moemoemyint@moemyanmar.ml
http://www.slideshare.net/MoeMoeMyint
Information Technology Department
Technological University (Kyaukse)
2. Only Original Owner has full rights reserved for
copied images.
This PPT is only for fair academic use.
3. Converting images to other image types
(Lab 2)
- The Image Processing Toolbox supports four basic types of images:
- Indexed images
- Grayscale images
- Binary images
- True Color images
M. M. Myint
Dr. Moe Moe Myint
Information Technology Department
Technological University (Kyaukse)
4. An indexed image consists of an array and a colormap
matrix.
The pixel values in the array are direct indices into a
colormap.
The colormap matrix is an m-by-3 array of class double
containing floating-point values in the range [0,1]. Each
row of map specifies the red, green, and blue components
of a single color.
M. M. Myint
5.
6. MATLAB stores a grayscale image as a single matrix, with each
element of the matrix corresponding to one image pixel.
The matrix can be of class uint8, uint16, int16, single, or double
in which case it contains values in the range [0,1], i.e, the
intensity ‘0’ represents black and the intensity ‘1’ represents
white.
M. M. Myint
7.
8. In a binary image, each pixel assumes one of only two
discrete values, “0” or “1”.
M. M. Myint
9.
10. A true color image is an image in which each pixel is specified by
three values — one each for the red, blue, and green
components of the pixel's color.
A true color array can be of class uint8, uint16, single, or
double. In a true color array of class single or double, each color
component is a value between 0 and 1.
Graphics file formats store true color images as 24-bit images,
where the red, green, and blue components are 8 bits each.
M. M. Myint
12. To change a color image into a grayscale image
I = rgb2gray(RGB)
grayimage = rgb2gray(‘Dog.jpg’);
figure; imshow(grayimage);
Practice1.m
clear all
close all
clc
[f p]=uigetfile('*.jpg');
I=imread([p f]);
figure;imshow(I);
title('Original Image','FontSize',14);
text(500,37,'Picture from Libraries','FontSize',14,'HorizontalAlignment','left');
grayimage=rgb2gray(I);
figure;imshow(grayimage);title('Grayscale Image');
M. M. Myint
13. Convert grayscale or binary image to indexed image
[X, map] = gray2ind(I, n)
Converts the grayscale image I to an indexed image
X. n specifies the size of the colormap, gray(n). n must
be an integer between 1 and 65536. If n is omitted,
it defaults to 64.
Example 1
I = imread('cameraman.tif');
[X, map] = gray2ind(I, 16);
imshow(X, map);
M. M. Myint
14. Convert grayscale image to indexed image using
multilevel thresholding
grayslice
Example 2
I = imread('snowflakes.png');
X = grayslice(I,16);
imshow(I)
figure, imshow(X,jet(16))
M. M. Myint
16. Convert image to binary image, based on threshold
BW = im2bw(I, level)
Specify level in the range [0,1]
Example 4
load trees
BW = im2bw(I,0.4);
imshow(X,map), figure, imshow(BW)
M. M. Myint
17. Convert indexed image to grayscale image
ind2gray
Example 5
load trees
I = ind2gray(X,map);
imshow(X,map)
figure,imshow(I)
M. M. Myint
18. Convert indexed image to RGB image
ind2rgb
Example 6
Syntax
RGB = ind2rgb(X,map)
M. M. Myint
19. Convert RGB image or colormap to grayscale
rgb2gray
Example 7
I = imread('board.tif');
J = rgb2gray(I);
figure, imshow(I), figure, imshow(J);
Convert the colormap to a grayscale colormap.
[X,map] = imread('trees.tif');
gmap = rgb2gray(map);
figure, imshow(X,map), figure, imshow(X,gmap);
M. M. Myint
20. Practice2.m
clear all, close all, clc;
rgbImage=imread('peppers.png');
figure,imshow(rgbImage);colorbar
title('Original Image');
figure,imshow(rgbImage);
bwImage=im2bw(rgbImage);
imshow(bwImage);
title('Black and White Image');
grayImage=rgb2gray(rgbImage);
figure,imshow(grayImage);
title('Grayscale Image');
indexImage=gray2ind(grayImage);
figure,imshow(indexImage);
title('Index Image');
doubleImage=im2double(rgbImage);
figure,imshow(doubleImage);
title('Double Image');
igrayImage=ind2gray(indexImage,cool);
figure,imshow(igrayImage);
title('Index Gray Image');
rgbindexImage=rgb2ind(rgbImage,cool);
figure,imshow(rgbindexImage);
title('RGB Indexex Image');
M. M. Myint
21. Double (64-bit double-precision floating point)
Single (32-bit single-precision floating point)
Int32 (32-bit signed integer)
Int16 (16-bit signed integer)
Int8 (8-bit signed integer)
Uint32 (32-bit unsigned integer)
Uint16 (16-bit unsigned integer)
Uint8 (8-bit unsigned integer)
M. M. Myint
24. Spatial Transformations(Lab 3)
- Modify the spatial relationship between pixels in an image,
mapping pixel locations in an input image to new locations in an
output image
-Perform certain specialized spatial transformations, such as
resizing and rotating an image
M. M. Myint
Dr. Moe Moe Myint
Information Technology Department
Technological University (Kyaukse)
25. B = imresize(A, scale) returns image B that is scale
times the size of A.
The input image A can be a grayscale, RGB, or binary
image.
If scale is between 0 and 1.0, B is smaller than A. If
scale is greater than 1.0, B is larger than A.
26. To resize an image, use the imresize function
I = imread('circuit.tif');
J = imresize(I,1.25);
imshow(I)
figure, imshow(J)
To create an output image with 100 rows and 150 columns
I = imread('circuit.tif');
J = imresize(I,[100 150]);
imshow(I)
figure, imshow(J)
M. M. Myint
27. B = imrotate(A, angle) rotates image A by angle
degrees in a counterclockwise direction around its
center point.
To rotate the image clockwise, specify a negative value
for angle.
28. To rotate an image, use the imrotate function
I = imread('circuit.tif');
J = imrotate(I,35);
%counterclockwise direction around its center point
imshow(I)
figure, imshow(J)
To rotate an image, use the imrotate function
I = imread('circuit.tif');
J = imrotate(I,-35);
%clockwise direction around its center point
imshow(I)
figure, imshow(J)
M. M. Myint
29. To extract a rectangular portion of an image, use the
imcrop function
I = imread('circuit.tif')
J = imcrop(I);
I = imread('circuit.tif');
J = imcrop(I,[60 40 100 90]);
M. M. Myint
30. B = impyramid(A, direction) computes a Gaussian
pyramid reduction or expansion of A by one level
direction can be 'reduce' or 'expand'.
M. M. Myint
32. Read the image and display it with title ‘Input Image’
Resize the input image with scale greater than 1.0 and
display it with title ‘Resize Image with >1.0’
Also …..with scale between 0 and 1.0 and display it
with title ‘Resize Image with <1.0’
Rotate the ‘Resize image with >1.0’ %Clockwise
direction
Crop the input image with copying the position
Compute a four-level multiresolution pyramid of the
input image
33. Practice3.m
%Shrink by factor of two using the defaults of bicubic interpolation and
antialiasing
I = imread('rice.png');
J = imresize(I, 0.5);
figure, imshow(I), figure, imshow(J)
%Shrink by factor of two using nearest-neighbor interpolation (This is the
fastest method, but it has the lowest quality.)
J2 = imresize(I, 0.5, 'nearest'); %Resize an indexed image
[X, map] = imread('trees.tif');
[Y, newmap] = imresize(X, map, 0.5);
imshow(Y, newmap)
%Resize an RGB image to have 64 rows (The number of columns is
computed automatically.)
RGB = imread('peppers.png');
RGB2 = imresize(RGB, [64 NaN]);
M. M. Myint
34. Practice4.m
%Read a solar spectra image, stored in FITS format, and rotate the image to
bring it into horizontal alignment. A rotation of -1 degree is all that is
required.
I = fitsread('solarspectra.fts');
I = mat2gray(I);
J = imrotate(I,-1,'bilinear','crop');
figure, imshow(I)
figure, imshow(J)
M. M. Myint
35. Image Analysis (Lab 4)
M. M. Myint
Dr. Moe Moe Myint
Information Technology Department
Technological University (Kyaukse)
36. bwboundaries Trace region boundaries in binary image
bwtraceboundary Trace object in binary image
cornermetric Create corner metric matrix from image
edge Find edges in grayscale image
hough Hough transform
houghlines Extract line segments based on Hough transform
houghpeaks Identify peaks in Hough transform
Qtdecomp Quadtree decomposition
qtgetblk Block values in quadtree decomposition
qtsetblk Set block values in quadtree decomposition
37. The Sobel method finds edges using the Sobel approximation to the
derivative. It returns edges at those points where the gradient of I is
maximum.
BW = edge(I,'sobel')
The Prewitt method finds edges using the Prewitt approximation to
the derivative. It returns edges at those points where the gradient of I
is maximum.
BW = edge(I,'prewitt')
The Roberts method finds edges using the Roberts approximation to
the derivative. It returns edges at those points where the gradient of I
is maximum.
BW = edge(I,'roberts')
M. M. Myint
38. The Laplacian of Gaussian method finds edges by looking for
zero crossings after filtering I with a Laplacian of Gaussian filter.
BW = edge(I,'log')
The Canny method finds edges by looking for local maxima of
the gradient of I. The gradient is calculated using the derivative
of a Gaussian filter. The method uses two thresholds, to detect
strong and weak edges, and includes the weak edges in the
output only if they are connected to strong edges. This method
is therefore less likely than the others to be fooled by noise, and
more likely to detect true weak edges.
BW = edge(I,'canny')
39. Examples
Find the edges of an image using the Prewitt and Canny
methods.
I = imread('circuit.tif');
BW1 = edge(I,'prewitt');
BW2 = edge(I,'canny');
imshow(BW1);
figure, imshow(BW2
40. Practice1.m
clc,clear all, close all
I=imread('peppers.png');
imshow(I);title('Input Image');
I=rgb2gray(I);
figure,imshow(I);title('Gray Image');
BW1=edge(I);
figure,imshow(BW1);title('Edge Image');
BW2=edge(I,'sobel');
figure,imshow(BW2);title('Sobel');
BW3=edge(I,'prewitt');
figure,imshow(BW3);title('Prewitt');
BW4=edge(I,'roberts');
figure,imshow(BW4);title('Roberts');
BW5=edge(I,'log');
figure,imshow(BW5);title('Log');
BW7=edge(I,'canny');
figure,imshow(BW7);title('Canny');
M. M. Myint
Editor's Notes
- This example introduces some basic image processing concepts. The example starts by reading an image into the MATLAB workspace. The example then performs some contrast adjustment on the image. Finally, the example writes the adjusted image to a file.
Gray-scale image or Gray level image
For each pixel in the image, the red, green, and blue elements combine to create the pixel’s actual color.
For example, to determine the color of the pixel (112,86), look at the RGB triplet stored in (112,86,1:3). Suppose (112,86,1) contains the value 0.1238, (112,86,2) contains 0.9874, and (112,86,3) contains 0.2543. The color for the pixel at (112,86) is: 0.1238 0.9874 0.2543
An RGB array can be of
class double, in which case it contains values in the range [0,1].
class uint8, in which case the data range is [0,255].
1. Display an image from a file.
2. Display an indexed image.
3. Display a grayscale image.
4. Display the same grayscale image, adjusting the display range.
1. Display an image from a file.
2. Display an indexed image.
3. Display a grayscale image.
4. Display the same grayscale image, adjusting the display range.
1. Display an image from a file.
2. Display an indexed image.
3. Display a grayscale image.
4. Display the same grayscale image, adjusting the display range.
1. Display an image from a file.
2. Display an indexed image.
3. Display a grayscale image.
4. Display the same grayscale image, adjusting the display range.
1. Display an image from a file.
2. Display an indexed image.
3. Display a grayscale image.
4. Display the same grayscale image, adjusting the display range.
1. Display an image from a file.
2. Display an indexed image.
3. Display a grayscale image.
4. Display the same grayscale image, adjusting the display range.
1. Display an image from a file.
2. Display an indexed image.
3. Display a grayscale image.
4. Display the same grayscale image, adjusting the display range.
Image Conversion
gray2ind - intensity image to index image
im2bw - image to binary
im2double - image to double precision
im2uint8 - image to 8-bit unsigned integers
im2uint16 - image to 16-bit unsigned integers
ind2gray - indexed image to intensity image
mat2gray - matrix to intensity image
rgb2gray - RGB image to grayscale
rgb2ind - RGB image to indexed image
Color-Space conversions in ImageTransform
Color transformations in ImageTransform consist of converting a three-layer wave (e.g., RGB) into another three-layer wave (e.g., XYZ).
rgb2gray
rgb2hsl
convert2gray
cmap2rgb
hsl2rgb
rgb2xyz
xyz2rgb
cmyk2rgb
rgb2I123
**************
Color-Space conversions in colorSpaceConversions procedures
The color transformations in procedures are designed to convert a single color at a time. Using optional input parameters you can use the procedures to set function variables to individual components or to print them to the history.
RGB2Lab
Lab2RGB
RGB2XYZ
XYZ2RGB
RGB2XYZccir
XYZccir2RGB
RGB2XYZitu
XYZitu2RGB
RGB2YUV
YUV2RGB
RGB2YIQ
YIQ2RGB
YUV2YIQ
YIQ2YUV
RGB2YCbCr
YCbCr2RGB
HSL2RGB
HSV2RGB
A spatial transformation (also known as a geometric operation) modifies the spatial relationship between pixels in an image, mapping pixel locations in an input image to new locations in an output image. The toolbox includes functions that perform certain specialized spatial transformations, such as resizing and rotating an image. In addition, the toolbox includes functions that you can use to perform many types of 2-D and N-D spatial transformations, including custom transformations.
Resizing an Image
Rotating an Image
Cropping an Image
Performing General 2-D Spatial Transformations
Performing N-Dimensional Spatial Transformations
Example: Performing Image Registration
To resize an image, use the imresize function. When you resize an image, you specify the image to be resized and the magnification factor. To enlarge an image, specify a magnification factor greater than 1. To reduce an image, specify a magnification factor between 0 and 1.
For example, the command below increases the size of an image by 1.25 times.
You can specify the size of the output image by passing a vector that contains the number of rows and columns in the output image. If the specified size does not produce the same aspect ratio as the input image, the output image will be distorted. If you specify one of the elements in the vector as NaN, imresize calculates the value for that dimension to preserve the aspect ratio of the image.
This example creates an output image with 100 rows and 150 columns.
To rotate an image, use the imrotate function. When you rotate an image, you specify the image to be rotated and the rotation angle, in degrees. If you specify a positive rotation angle, imrotate rotates the image counterclockwise; if you specify a negative rotation angle, imrotate rotates the image clockwise.
By default, imrotate creates an output image large enough to include the entire original image. Pixels that fall outside the boundaries of the original image are set to 0 and appear as a black background in the output image. You can, however, specify that the output image be the same size as the input image, using the &apos;crop&apos; argument. Similarly, imrotate uses nearest-neighbor interpolation by default to determine the value of pixels in the output image, but you can specify other interpolation methods. See the imrotate reference page for a list of supported interpolation methods.
This example rotates an image 35° counterclockwise and specifies bilinear interpolation.
To extract a rectangular portion of an image, use the imcrop function. Using imcrop, you can specify the crop region interactively using the mouse or programmatically by specifying the size and position of the crop region.
This example illustrates an interactive syntax. The example reads an image into the MATLAB workspace and calls imcrop specifying the image as an argument. imcrop displays the image in a figure window and waits for you to draw the crop rectangle on the image. When you move the pointer over the image, the shape of the pointer changes to cross hairs . Click and drag the pointer to specify the size and position of the crop rectangle. You can move and adjust the size of the crop rectangle using the mouse. When you are satisfied with the crop rectangle, double-click to perform the crop operation, or right-click inside the crop rectangle and select Crop Image from the context menu. imcrop returns the cropped image in J.
You can also specify the size and position of the crop rectangle as parameters when you call imcrop. Specify the crop rectangle as a four-element position vector, [xmin ymin width height].
In this example, you call imcrop specifying the image to crop, I, and the crop rectangle. imcrop returns the cropped image in J.
To extract a rectangular portion of an image, use the imcrop function. Using imcrop, you can specify the crop region interactively using the mouse or programmatically by specifying the size and position of the crop region.
This example illustrates an interactive syntax. The example reads an image into the MATLAB workspace and calls imcrop specifying the image as an argument. imcrop displays the image in a figure window and waits for you to draw the crop rectangle on the image. When you move the pointer over the image, the shape of the pointer changes to cross hairs . Click and drag the pointer to specify the size and position of the crop rectangle. You can move and adjust the size of the crop rectangle using the mouse. When you are satisfied with the crop rectangle, double-click to perform the crop operation, or right-click inside the crop rectangle and select Crop Image from the context menu. imcrop returns the cropped image in J.
You can also specify the size and position of the crop rectangle as parameters when you call imcrop. Specify the crop rectangle as a four-element position vector, [xmin ymin width height].
In this example, you call imcrop specifying the image to crop, I, and the crop rectangle. imcrop returns the cropped image in J.
A spatial transformation (also known as a geometric operation) modifies the spatial relationship between pixels in an image, mapping pixel locations in an input image to new locations in an output image. The toolbox includes functions that perform certain specialized spatial transformations, such as resizing and rotating an image. In addition, the toolbox includes functions that you can use to perform many types of 2-D and N-D spatial transformations, including custom transformations.
Resizing an Image
Rotating an Image
Cropping an Image
Performing General 2-D Spatial Transformations
Performing N-Dimensional Spatial Transformations
Example: Performing Image Registration
In an image, an edge is a curve that follows a path of rapid change in image intensity. Edges are often associated with the boundaries of objects in a scene. Edge detection is used to identify the edges in an image.