If you are unfamiliar with this technique, follow this article to understand practically using some examples.
Why Do We Need to Create an Image from an Array?
Displaying images from an array is an important task since it can help you visualize the data, create animations, store images in the array, and much more. Having an image in the form of an array is a convenient way since it allows you to manipulate the image data, which can then later be converted back to the image again after the changes.
How to Display Image from an Array in MATLAB Using image() Function?
Displaying an image from an array can be easily done by using MATLAB’s built-in image() function.
This function accepts an array and creates an image having a 1-pixel color corresponding to each array element.
Syntax
You can implement different syntaxes of image() function in MATLAB as given below:
image(x,y,C)
image('CData',C)
image('XData',x,'YData',y,'CData',C)
Here,
The function image(C) displays an image having different colors corresponding to the given array’s elements. Each element of the given array represents a pixel, and each indicates the intensity of the color taken from the colormap. The displayed image is an m-by-n grid of different colors where m represents C’s rows and n represents C’s columns. Also, the row and column indices of each element identify the center of the corresponding pixel.
The function image(x,y, C) yields to display the image location by specifying x and y as the corner locations from C(1,1) to C(m,n). To specify the location of both corners, x, and y should be vectors having two elements. To specify the location of one corner, x, and y should be scalar values.
The function image(‘CData’,C) inserts the image in the current axis without replacing the existing plot. This syntax represents the low-level version of the function image(C).
The function image(‘XData’,x,’YData’,y,’CData’,C) specifies the location of the image. This syntax represents the low-level version of the function image(x,y,C).
Example
Consider some examples to understand the implementation of the image() function in MATLAB.
Example 1: How to Display an Image from an Array Using the image() Function in MATLAB?
In this MATLAB code, first, we declare a 10-by-10 matrix and use the image() function to display an image having different colors for each pixel corresponding to the values in matrix C.
image(C)
Example 2: How to Display an Image by Controlling its Placement Using the image(x,y,C) Function in MATLAB?
The given example displays an image from a 10-by-10 matrix and controls its location using the vectors x and y.
x = [4 7];
y = [5 9];
image(x,y,C)
Example 3: How to Display an Image From a 3-D Array Using image(C) Function in MATLAB?
In the given example, first, we declare a 3-by-3-by-3 array and use the image() function to display an image having different colors for each pixel corresponding to the values in array C.
C(:,:,3) = randi(100,3);
image(C)
Example 4: How to Add an Image to an Axis in 3-D View Using image(C) Function in MATLAB?
In the MATLAB code, we use the image() function to add an image in 3-D view using the image(C) function.
surf(Z)
hold on
image(Z,'CDataMapping','scaled')
Conclusion
The MATLAB’s built-in image() function is widely used by developers to create an image from an array while developing an application. This function accepts an array and creates an image having different colors for each pixel corresponding to each array element. This guide has provided the basics of the image() function that will help you learn the use of this function in MATLAB.