Documentation Center

  • Trial Software
  • Product Updates

Image Pixel Values and Image Statistics

Determine Values of Individual Image Pixels

To determine the values of one or more pixels in an image and return the values in a variable, use the impixel function. You can specify the pixels by passing their coordinates as input arguments or you can select the pixels interactively using a mouse. impixel returns the value of specified pixels in a variable in the MATLAB® workspace.

This example illustrates how to use impixel to get pixel values.

  1. Display an image.

    imshow canoe.tif
  2. Call impixel. When called with no input arguments, impixel associates itself with the image in the current axes.

    pixel_values = impixel
  3. Select the points you want to examine in the image by clicking the mouse. impixel places a star at each point you select.

  4. When you are finished selecting points, press Return. impixel returns the pixel values in an n-by-3 array, where n is the number of points you selected. The stars used to indicate selected points disappear from the image.

    pixel_values =
    
    0.1294    0.1294    0.1294
    0.5176         0         0
    0.7765    0.6118    0.4196

Create Intensity Profile of Image

The intensity profile of an image is the set of intensity values taken from regularly spaced points along a line segment or multiline path in an image. For points that do not fall on the center of a pixel, the intensity values are interpolated.

To create an intensity profile, use the improfile function. This function calculates and plots the intensity values along a line segment or a multiline path in an image. You define the line segment (or segments) by specifying their coordinates as input arguments. You can define the line segments using a mouse. (By default, improfile uses nearest-neighbor interpolation, but you can specify a different method. For more information, see Specify the Interpolation Method.) improfile works best with grayscale and truecolor images.

For a single line segment, improfile plots the intensity values in a two-dimensional view. For a multiline path, improfile plots the intensity values in a three-dimensional view.

If you call improfile with no arguments, the cursor changes to crosshairs when it is over the image. You can then specify line segments by clicking the endpoints; improfile draws a line between each two consecutive points you select. When you finish specifying the path, press Return. improfile displays the plot in a new figure.

In this example, you call improfile and specify a single line with the mouse. In this figure, the line is shown in red, and is drawn from top to bottom.

I = fitsread('solarspectra.fts');
imshow(I,[]);
improfile

improfile displays a plot of the data along the line. Notice the peaks and valleys and how they correspond to the light and dark bands in the image.

Plot Produced by improfile

The example below shows how improfile works with an RGB image. Use imshow to display the image in a figure window. Call improfile without any arguments and trace a line segment in the image interactively. In the figure, the black line indicates a line segment drawn from top to bottom. Double-click to end the line segment.

imshow peppers.png
improfile

RGB Image with Line Segment Drawn with improfile

The improfile function displays a plot of the intensity values along the line segment. The plot includes separate lines for the red, green, and blue intensities. In the plot, notice how low the blue values are at the beginning of the plot where the line traverses the orange pepper.

Plot of Intensity Values Along a Line Segment in an RGB Image

Display Contour Plot of Image Data

You can use the toolbox function imcontour to display a contour plot of the data in a grayscale image. A contour is a path in an image along which the image intensity values are equal to a constant. This function is similar to the contour function in MATLAB, but it automatically sets up the axes so their orientation and aspect ratio match the image.

This example displays a grayscale image of grains of rice and a contour plot of the image data:

  1. Read a grayscale image and display it.

    I = imread('rice.png');
    imshow(I)

  2. Display a contour plot of the grayscale image.

    figure, imcontour(I,3)

You can use the clabel function to label the levels of the contours. See the description of clabel in the MATLAB Function Reference for details.

Create Image Histogram

An image histogram is a chart that shows the distribution of intensities in an indexed or grayscale image. You can use the information in a histogram to choose an appropriate enhancement operation. For example, if an image histogram shows that the range of intensity values is small, you can use an intensity adjustment function to spread the values across a wider range.

To create an image histogram, use the imhist function. This function creates a histogram plot by making n equally spaced bins, each representing a range of data values. It then calculates the number of pixels within each range.

The following example displays an image of grains of rice and a histogram based on 64 bins. The histogram shows a peak at around 100, corresponding to the dark gray background in the image. For information about how to modify an image by changing the distribution of its histogram, see Adjust Intensity Values to Specified Range.

  1. Read image and display it.

    I = imread('rice.png');
    imshow(I)

  2. Display histogram of image.

    figure, imhist(I)

Image Mean, Standard Deviation, and Correlation Coefficent

You can compute standard statistics of an image using the mean2, std2, and corr2 functions. mean2 and std2 compute the mean and standard deviation of the elements of a matrix. corr2 computes the correlation coefficient between two matrices of the same size.

These functions are two-dimensional versions of the mean, std, and corrcoef functions described in the MATLAB Function Reference.

Calculate Properties of Image Regions

You can use the regionprops function to compute properties for image regions. For example, regionprops can measure such properties as the area, center of mass, and bounding box for a region you specify. See the reference page for regionprops for more information.

Was this topic helpful?