[Ch3] Images and MATLAB
3.1 Greyscale images
1) imread
>> w = imread('wombats.tif');
(1) w : A matrix variable (which has grey values of all the pixels in greyscale image)
(2) imread : To read the pixel values from an image file, and return a matrix of all the pixel values
- It ends in a semicolon ( ; ) : not displaying the result of command to screen
The result of imread command is a matrix of size 256x256, or with 65536 elements
- The name wombats.tif is given in single quote marks ( ' ' )
Without them, Matlab would assume that wombats.tif was the name of a variable, rather than the name of a file
2) imshow
>> figure, imshow(w), pixval on
(1) figure : To create a figure window on the screen
(2) imshow(w) : To display the matrix w as an image
(3) pixval on : To turn on the pixel values in our figure
- A display of the grey values of the pixels in the image
- Appeared at the bottom of the figure in the form 'c x r = p' imfinfo
(c, r, p : column, row, grey value of the given pixel)
Ex. 'wombats.tif' : 8-bit greyscale img → pixel values appear as integers in the range 0-255
(4) impixel(matrix, row, col) : returns all 3 same values (g : A single two-dimensional matrix)
3.2 RGB Images
1) imread, imshow 24-bit RGB image
2) size of RGB image : ( number of rows, columns, 'pages' of a )
a : A three-dimensional matrix = multidimensional array = stack of three matrices, each of the same size
>> a=imread('autumn.tif');
>> figure,imshow(a),pixval on
>> size(a)
ans =
206 345 3
3) Indexing to obtain any of the RGB values at a given location
4) impixel function ★ column 먼저 !
>> a(100,200,2) # matrix(row,col,RGB)
ans =
uint8
25
>> a(100,200,:)
1×1×3 uint8 배열
ans(:,:,1) =
75
ans(:,:,2) =
25
ans(:,:,3) =
30
>> impixel(a,200,100) # (matrix, col, row)
ans =
75 25 30
3.3 Indexed color images
1) imread, imshow indexed color image
>> em=imread('emu.tif');
>> figure,imshow(em)
Result : A dark, barely distinguishable image, with single integer grey values
em : a single greyscale image
2) [em, emap]
>> figure,imshow('emu.tif')
>> [em,emap]=imread('emu.tif');
>> figure,imshow(em,emap)
Result : A color image
emap : a color map
emu.tif : An indexed image, consisting of two matrices (color map + index to color map)
MATLAB stores RGB values of an Indexed image as double values between 0 and 1
3) imfinfo
>> imfinfo('emu.tif')
ans =
다음 필드를 포함한 struct:
Filename: 'C:\Users\82104\Downloads\emu.tif'
FileModDate: '28-1-2022 15:00:45'
FileSize: 119804
Format: 'tif'
FormatVersion: []
Width: 331
Height: 384
BitDepth: 8
ColorType: 'indexed'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 8
Compression: 'PackBits'
PhotometricInterpretation: 'RGB Palette'
StripOffsets: [1×16 double]
SamplesPerPixel: 1
RowsPerStrip: 24
StripByteCounts: [1×16 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: [256×3 double]
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 255
MinSampleValue: 0
Thresholding: 1
Offset: 117938
cf) MATLAB does not distinguish between greyscale and binary images
A binary image is just a special case of a greyscale image which has only two intensities
However, we can see that 'text.tif' is a binary image since the number of bits per pixel is only one!
→ 더 간단한 Binary 인데 MATLAB 에서는 Greyscale이랑 구분을 X
3.4 Data types and conversions
1) The most common Data Types in MATLAB
Data Type | Description | Range |
int8 | 8-bit integer | -128 ~ 127 |
uint8 | 8-bit unsigned integer | 0 ~ 255 |
int16 | 16-bit integer | -32768~ 32767 |
uint16 | 16-bit unsigned integer | 0 ~ 65535 |
double | Double precision real number | Machine specific |
2) Conversion
>> a=23;
>> b=uint8(a);
>> b
b =
uint8
23
>> whos a b
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x1 1 uint8
Arithmetic operations
- are not permitted on int8, int16, uint8, uint16 types
- are permitted on only double type!
→ Conversion functions 통해 double type으로 conversion 해야 Arithmetic operations 가능 !
Ex) Grayscale image : uint8 (efficient storage space, since each pixel requires only 1 byte)
→ gray2rgb functions 통해 uint8에서 double type으로 conversion
Function | Use | Format |
ind2gray | Indexed to Greyscale | y=ind2gray(x,map); |
gray2ind | Greyscale to Indexed | [y,map]=gray2ind(x); |
rgb2gray | RGB to Greyscale | y=rgb2gray(x); |
gray2rgb | Greyscale to RGB | y=gray2rgb(x); |
rgb2ind | RGB to Indexed | [y,map]=rgb2ind; |
ind2rgb | Indexed to RGB | y=ind2rgb(x,map); |