全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 数据分析与数据挖掘
2156 1
2016-03-14

MATLAB课程:代码示例之Image Processing and Computer Vision(二)


Correcting Nonuniform Illumination


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.


Step 1: Read ImageI = imread('rice.png');imshow(I)


Step 2: Use Morphological Opening to Estimate the Background

Notice that the background illumination is brighter in the center of the image than at the bottom. Use imopen to estimate the background illumination.

background = imopen(I,strel('disk',15));% Display the Background Approximation as a Surfacefiguresurf(double(background(1:8:end,1:8:end))),zlim([0 255]);ax = gca;ax.YDir = 'reverse';


Step 3: Subtract the Background Image from the Original ImageI2 = I - background;imshow(I2)


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.

I2 = imtophat(I,strel('disk',15));

Step 4: Increase the Image ContrastI3 = imadjust(I2);imshow(I3);


Step 5: Threshold the Image

Create a new binary image by thresholding the adjusted image. Remove background noise with bwareaopen.

bw = imbinarize(I3);bw = bwareaopen(bw, 50);imshow(bw)


Step 6: Identify Objects in the 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.

grain = false(size(bw));grain(cc.PixelIdxList{50}) = true;imshow(grain);


Step 8: View All Objects

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.

RGB_label = label2rgb(labeled, @spring, 'c', 'shuffle');imshow(RGB_label)


Step 9: Compute Area of Each Object

Each rice grain is one connected component in the cc structure. Use regionprops on cc to get the area.

graindata = regionprops(cc,'basic')


graindata = 95x1 struct array with fields:    Area    Centroid    BoundingBox


To find the area of the 50th component, use dot notation to access the Area field in the 50th element of graindata structure array.

graindata(50).Area


ans =   194


Step 10: Compute Area-based Statistics

Create a new vector grain_areas, which holds the area measurement for each grain.

grain_areas = [graindata.Area];


Find the grain with the smallest area.

[min_area, idx] = min(grain_areas)grain = false(size(bw));grain(cc.PixelIdxList{idx}) = true;imshow(grain);


min_area =    61idx =    16


Step 11: Create Histogram of the Areanbins = 20;figure, hist(grain_areas,nbins)title('Histogram of Rice Grain Area');




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-3-14 11:58:08
果然有用,太赞了!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群