## matlab图像处理（如何从像素中获得图像的信息）

2018年05月03日 ⁄ 综合 ⁄ 共 4900字 ⁄ 字号 评论关闭

## Getting Information about Image Pixel Values and Image Statistics

### Getting Image Pixel Values Using impixel

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

 Note  You can also get pixel value information interactively usingthe Image Tool -- see Getting Information about the Pixels in an Image.

This example illustrates how to use impixel toget pixel values.

1. Display an image.

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

`vals = impixel`
3. Select the points you want to examinein the image by clicking the mouse.
impixel placesa star at each point you select.

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

```pixel_values =

0.1294    0.1294    0.1294
0.5176         0         0
0.7765    0.6118    0.4196```

### Creating an Intensity Profile of an Image Using improfile

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

To create anintensity profile, use the
improfile function.This function calculates and plots the intensity values along a linesegment 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 usesnearest-neighbor interpolation, but you can specify a different method.For more information, see
Specifying the Interpolation Method.) improfile worksbest with grayscale and truecolor images.

For a single line segment, improfile plotsthe intensity values in a two-dimensional view. For a multiline path,
improfile plotsthe 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 canthen specify line segments by clicking the endpoints;
improfile drawsa line between each two consecutive points you select. When you finishspecifying the path, press
Return. improfile displaysthe plot in a new figure.

In this example, you call improfile and specifya single line with the mouse. In this figure, the line is shown inred, and is drawn from top to bottom.

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

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

Plot Produced by improfile

The example below shows how improfile workswith an RGB image. Use
imshow
to display the imagein a figure window. Call improfile without anyarguments and trace a line segment in the image interactively. Inthe figure, the black line indicates a line segment drawn from topto 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 ofthe intensity values along the line segment. The plot includes separatelines for the red, green, and blue intensities. In the plot, noticehow low the blue values are at the beginning of the plot where theline
traverses the orange pepper.

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

### Displaying a Contour Plot of Image Data

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

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

1. Reada grayscale image and display it.

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

2. Display a contour plot of the grayscaleimage.

`figure, imcontour(I,3)`

You can use the clabel function to labelthe levels of the contours. See the description of
clabel in the MATLAB Function Referencefor details.

### Creating an Image Histogram Using imhist

An image histogram is a chart that showsthe distribution of intensities in an indexed or grayscale image.You can use the information in a histogram to choose an appropriateenhancement operation. For example, if an image
histogram shows thatthe range of intensity values is small, you can use an intensity adjustmentfunction 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 equallyspaced bins, each representing a range of data values. It then calculatesthe number of pixels within each range.

The following example displays an image of grains of rice anda histogram based on 64 bins. The histogram shows a peak at around100, corresponding to the dark gray background in the image. For informationabout how to modify an image by changing the distribution
of its histogram,see Adjusting Intensity Values to a Specified Range.

1. Read image and display it.

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

2. Display histogram of image.

`figure, imhist(I)`