This example shows how to correct nonuniform illumination in an image to make it easy to identify individual grains of rice in the image. You can then learn about the characteristics of the grains and easily compute statistics for all the grains in the image.
Note that step 2 and step 3 together could be replaced by a single step using imtophat which first calculates the morphological opening and then subtracts it from the original image.
The function bwconncomp finds all the connected components (objects) in the binary image. The accuracy of your results depend on the size of the objects, the connectivity parameter (4,8,or arbitrary), and whether or not any objects are touching (in which case they may be labeled as one object). Some of the rice grains in bw are touching.
cc = bwconncomp(bw, 4)
cc =
Connectivity: 4
ImageSize: [256 256]
NumObjects: 95
PixelIdxList: {1x95 cell}
复制代码
Step 7: Examine One Object
Each distinct object is labeled with the same integer value. Show the grain that is the 50th connected component.
One way to visualize connected components is to create a label matrix and then display it as a pseudo-color indexed image.
Use labelmatrix to create a label matrix from the output of bwconncomp. Note that labelmatrix stores the label matrix in the smallest numeric class necessary for the number of objects.
labeled = labelmatrix(cc);
whos labeled
复制代码
Name Size Bytes Class Attributes
labeled 256x256 65536 uint8
In the pseudo-color image, the label identifying each object in the label matrix maps to a different color in the associated colormap matrix. Use label2rgb to choose the colormap, the background color, and how objects in the label matrix map to colors in the colormap.