Matlab图像处理高级操作
Step 1: Read and Display an Image
clear, close all, imtool close all
Clear the MATLAB workspace of any variables, close open figure windows, and close all open Image Tools.
I = imread('rice.png'); Read and display the grayscale image rice.png.
imshow(I)
Step 2: Estimate the Value of Background Pixels
calls the imopen function to perform the morphological opening operation and then calls就the imshow function to view the results.
background = imopen(I,strel('disk',15)); calls the strel function to create a disk-shaped structuring element with a radius of 15.
figure, imshow(background)
Step 3: View the Background Approximation as a Surface
Use the surf command to create a surface display of the background approximation background. The surf command creates colored parametric surfaces that enable you to view mathematical functions over a rectangular region. The surf function requires data of class double, however, so you first need to convert background using the double command.
figure, surf(double(background(1:8:end,1:8:end))),zlim([0 255]);
set(gca,'ydir','reverse');
reverses the y-axis of the display to provide a better view of the data (the pixels at the bottom of the image appear at the front of the surface plot).
Step 4: Create an Image with a Uniform Background
To create a more uniform background, subtract the background image, background, from the original image, I.
I2 = imsubtract(I,background);
Display the image with its more uniform background.
figure, imshow(I2)
Step 5: Adjust the Contrast in the Processed Image
After subtraction, the image has a uniform background but is now a bit too dark. Use imadjust to adjust the contrast of the image.
I3 = imadjust(I2);
imadjust increases the contrast of the image by saturating 1% of the data at both low and high intensities of I2 and by stretching the intensity values to fill the uint8 dynamic range.
Display the adjusted image I3.
figure, imshow(I3);
Step 6: Create a Binary Version of the Image
Create a binary version of the image by using thresholding. The function graythresh automatically computes an appropriate threshold to use to convert the grayscale image to binary. The im2bw function performs the conversion.
level = graythresh(I3);
bw = im2bw(I3,level);
figure, imshow(bw)
Step 7: Determine the Number of Objects in the Image
After converting the image to a binary image, you can use the bwlabel function to determine the number of grains of rice in the image. The bwlabel function labels all the components in the binary image bw and returns the number of components it finds in the image in the output value, numObjects.
[labeled,numObjects] = bwlabel(bw,4);
Step 8: Examine the Label Matrix
To better understand the label matrix returned by the bwlabel function, this step explores the pixel values in the image. There are several ways to get a close-up view of pixel values. For example, you can use imcrop to select a small portion of the image. Another way is to use toolbox Pixel Region tool to examine pixel values. Display the label matrix, using imshow,
figure, imshow(labeled);
Start the Pixel Region tool.
Impixelregion
Step 9: Display the Label Matrix as a Pseudocolor Indexed Image
A good way to view a label matrix is to display it as a pseudocolor indexed image. In the pseudocolor image, the number that identifies each object in the label matrix maps to a different color in the associated colormap matrix. The colors in the image make objects easier to distinguish.
To view a label matrix in this way, use the label2rgb function. Using this function, you can specify the colormap, the background color, and how objects in the label matrix map to colors in the colormap.
pseudo_color = label2rgb(labeled, @spring, 'c', 'shuffle');
imshow(pseudo_color);
Step 10: Measure Object Properties in the Image
The regionprops command measures object or region properties in an image and returns them in a structure array. When applied to an image with labeled components, it creates one structure element for each component.
The following example uses regionprops to create a structure array containing some basic properties for labeled. When you set the properties parameter to 'basic', the regionprops function returns three commonly used measurements: area, centroid (or center of mass), and bounding box. The bounding box represents the smallest rectangle that can contain a region, or in this case, a grain of rice.
graindata = regionprops(labeled,'basic')
To find the area of the 51st labeled component, access the Area field in the 51st element in the graindata structure array. Note that structure field names are case sensitive.
graindata(51).Area
To find the smallest possible bounding box and the centroid (center of mass) for the same component, use this code:
graindata(51).BoundingBox, graindata(51).Centroid
Step 11: Compute Statistical Properties of Objects in the Image

您当前的位置: