Image Processing in MATLAB | Fundamental Operations
Last Updated :
06 Aug, 2021
1. Reading Images
Images are read into the MATLAB Environment using imread() function which takes filename with applicable extension as the argument
For Example:
>> I=imread('nature.jpg');
This will read JPEG image 'nature' into the image array.
Note: The semicolon(;) at the end of command line is used to suppress the output in MATLAB.If ';' is not used at the end, it will show the output of the specified operation.
2.Displaying Images
imshow() function is used to display images in MATLAB. The basic syntax of imshow() is
imshow(f, G);
Here f is image matrix and G is number of intensity level used to display the image. The second Argument in the above syntax is optional. If G is omitted its value defaults to 256 levels.
When we use the syntax
imshow(f, [Low, High]);
It displays all value less than or equal to 'Low' as black and all values greater than or equal to 'High' as white. The values between 'Low' and 'High' are displayed as the intermediate intensity value using the default number of levels.
Examples:
Showing Grayscale Images
>> imshow(f);
This will display the grayscale image f.
Also, we can write
>> imshow(f, [90, 180]);
It will display all value less than or equal to 90 as black and all values greater than or equal to 180 as white. The values between 90 and 180 are displayed as the intermediate intensity value using the default number of levels.
Showing Binary images
>> imshow(BW);
It displays the binary image BW. It displays pixels with the value 0 (zero) as black and pixels with the value 1 as white.
Showing RGB images
>> imshow(f);
It displays the RGB image f.
3. Writing images to disk
Images are written to disk using imwrite() function. The basic syntax of imwrite() is
imwrite(f, 'filename');
Here f is our image and filename is the name of file including a recognized file format extension. Alternatively, we can also specify desired format explicitly with a third argument.
For example
>> imwrite(f, 'nature.jpg');
Alternatively, we can write it as,
>> imwrite(f, 'nature', 'jpg');
The above commands write image f to a filename nature with extension jpg.
Note: The imwrite() function have some optional parameters too, depending upon file format.
For example:
>> imwrite(f, 'filename', 'quality', q);
The above syntax is applicable for only JPEG images. Here q is an integer and can take value between 0 to 100. Lower the value of q higher the degradation due to JPEG compression.
Some Other Important functions
i.) imfinfo
It displays the details of an image file
Example:
>> imfinfo nature.jpg;
Output:
Filename: 'nature.jpg'
FileModDate: '23-Jun-2016 09:57:04'
FileSize: 238290
Format: 'jpg'
FormatVersion: ''
Width: 1920
Height: 1200
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: ''
NumberOfSamples: 3
CodingMethod: 'Huffman'
CodingProcess: 'Sequential'
Comment: {'CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 92?'}
ii.) size()
size() function take image matrix as an argument and give the row and column dimension of an image f. This function is used to determine the size of an image automatically.
Example :
>> size(f)
Output :
ans =
1920 1200
Here 1920 is the number of rows and 1200 is the number of columns in image f.
Note: As we know RGB images are represented using 3-D matrix in MATLAB. So, if f is a RGB image the above function will produce output as
ans =
1920 1200 3
iii.) whos f
This function will show additional detail about image matrix f.
Example :
whos f
Output :
Name Size Bytes Class Attributes
f 300x400x3 360000 uint8
Similar Reads
Python | Morphological Operations in Image Processing (Gradient) | Set-3
In the previous articles, the Opening operation and the Closing operations were specified. In this article, another morphological operation is elaborated that is Gradient. It is used for generating the outline of the image. There are two types of gradients, internal and external gradient. The intern
2 min read
Image Edge Detection Operators in Digital Image Processing
In digital image processing edges are places where the brightness or color in an image changes a lot. These changes usually happen at borders of objects. Detecting edges helps us understand the shape, size and location of different parts in an image. Edge detection is used to recognize patterns, und
5 min read
Image Processing In Java - Get and Set Pixels
Prerequisite - Image Processing in Java - Read and Write In this set, we will learn about the pixels of images, how we can get pixel values of an image and how to set pixel values in an image using Java programming language. Pixels are the smallest unit of an image which consists of four components
3 min read
Negative of an image in MATLAB
The negative of an image is achieved by replacing the intensity 'i' in the original image by 'i-1', i.e. the darkest pixels will become the brightest and the brightest pixels will become the darkest. Image negative is produced by subtracting each pixel from the maximum intensity value. For example i
2 min read
MATLAB - Intensity Transformation Operations on Images
Intensity transformations are among the simplest of all image processing techniques. Approaches whose results depend only on the intensity at a point are called point processing techniques or Intensity transformation techniques. Although intensity transformation and spatial filtering methods span a
4 min read
Python | Morphological Operations in Image Processing (Opening) | Set-1
Morphological operations are used to extract image components that are useful in the representation and description of region shape. Morphological operations are some basic tasks dependent on the picture shape. It is typically performed on binary images. It needs two data sources, one is the input i
3 min read
Boundary Extraction of image using MATLAB
The boundary of the image is different from the edges in the image. Edges represent the abrupt change in pixel intensity values while the boundary of the image is the contour. As the name boundary suggests that something whose ownership changes, in the image when pixel ownership changes from one sur
3 min read
Digital Image Processing Algorithms using MATLAB
Like it is said, "One picture is worth more than ten thousand words "A digital image is composed of thousands and thousands of pixels. An image could also be defined as a two-dimensional function, f(x, y), where x and y are spatial (plane) coordinates and therefore the amplitude of f at any pair of
8 min read
How to Use & and && Operator in MATLAB?
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of
3 min read
How To Use | and || Operator in MATLAB?
In MATLAB, | and || are both logical operators that are used to perform logical OR operations on Boolean variables, however, there is a subtle difference between the two: |||Â The element-wise logical OR operator "|" takes two arrays of the same size and returns an array of the same size where each e
4 min read