Basic Image Import Lab 1
Basic Image Import Lab 1
Improve the contrast in an image, using the histeq function. Histogram equalization spreads the
intensity values over the full range of the image. Display the image. (The toolbox includes several
other functions that perform contrast adjustment, including imadjust and adapthisteq, and
interactive tools such as the Adjust Contrast tool, available in the Image Viewer.)
I2 = histeq(I);
figure
imshow(I2)
Call the imhist function again to create a histogram of the equalized image I2 . If you compare the
two histograms, you can see that the histogram of I2 is more spread out over the entire range than
the histogram of I .
figure
imhist(I2)
Step 4: Write the Adjusted Image to a Disk File
Write the newly adjusted image I2 to a disk file, using the imwrite function. This example includes
the filename extension '.png' in the file name, so the imwrite function writes the image to a file in
Portable Network Graphics (PNG) format, but you can specify other formats.
imwrite (I2, 'pout2.png');
This example shows how to use imfindcircles to automatically detect circles or circular objects in
an image. It also shows the use of viscircles to visualize the detected circles.
[]
radii =
[]
Note that the outputs centers and radii are empty, which means that no circles were found. This
happens frequently because imfindcircles is a circle detector, and similar to most
detectors, imfindcircles has an internal detection threshold that determines its sensitivity. In simple
terms it means that the detector's confidence in a certain (circle) detection has to be greater than a
certain level before it is considered a valid detection. imfindcircles has a parameter 'Sensitivity'
which can be used to control this internal threshold, and consequently, the sensitivity of the algorithm.
A higher 'Sensitivity' value sets the detection threshold lower and leads to detecting more circles. This
is similar to the sensitivity control on the motion detectors used in home security systems.
146.1895 198.5824
328.8132 135.5883
130.3134 43.8039
175.2698 297.0583
312.2831 192.3709
327.1316 297.0077
243.9893 166.4538
271.5873 280.8920
radii = 8×1
23.1604
22.5710
22.9576
23.7356
22.9551
22.9995
22.9055
23.0298
This time imfindcircles found some circles - eight to be precise. centers contains the locations of
circle centers and radii contains the estimated radii of those circles.
The circle centers seem correctly positioned and their corresponding radii seem to match well to the
actual chips. But still quite a few chips were missed. Try increasing the 'Sensitivity' even more, to
0.92.
[centers,radii] = imfindcircles(rgb,[20 25],'ObjectPolarity','dark', ...
'Sensitivity',0.92);
length(centers)
ans = 16
So increasing 'Sensitivity' gets us even more circles. Plot these circles on the image again.
delete(h) % Delete previously drawn circles
h = viscircles(centers,radii);
delete(h)
h = viscircles(centers,radii);
The two-stage method is detecting more circles, at the Sensitivity of 0.92. In general, these two
method are complementary in that have they have different strengths. The Phase coding method is
typically faster and slightly more robust to noise than the two-stage method. But it may also need
higher 'Sensitivity' levels to get the same number of detections as the two-stage method. For
example, the phase coding method also finds the same chips if the 'Sensitivity' level is raised higher,
say to 0.95.
[centers,radii] = imfindcircles(rgb,[20 25],'ObjectPolarity','dark', ...
'Sensitivity',0.95);
delete(h)
viscircles(centers,radii);
Note that both the methods in imfindcircles find the centers and radii of the partially visible
(occluded) chips accurately.
delete(hBright)
hBright = viscircles(centersBright, radiiBright,'Color','b');
Step 11: Draw 'Dark' and 'Bright' Circles Together
Now imfindcircles finds all of the yellow ones, and a green one too. Draw these chips in blue,
together with the other chips that were found earlier (with 'ObjectPolarity' set to 'dark'), in red.
h = viscircles(centers,radii);
All the circles are detected. A final word - it should be noted that changing the parameters to be more
aggressive in detection may find more circles, but it also increases the likelihood of detecting false
circles. There is a trade-off between the number of true circles that can be found (detection rate) and
the number of false circles that are found with them (false alarm rate).
Happy circle hunting!
This example shows how to enhance an image as a preprocessing step before analysis. In this
example, you correct the nonuniform background illumination and convert the image into a binary
image to make it easy to identify foreground objects (individual grains of rice). You can then analyze
the objects, such as finding the area of each grain of rice, and you can compute statistics for all
objects in the image.
The background illumination is brighter in the center of the image than at the bottom. Preprocess the
image to make the background illumination more uniform.
As a first step, remove all the foreground (rice grains) using morphological opening. The opening
operation removes small objects that cannot completely contain the structuring element. Define a
disk-shaped structuring element with a radius of 15, which does fit entirely inside a single grain of rice.
se = strel('disk',15)
se =
strel is a disk shaped structuring element with properties:
Neighborhood: [29x29 logical]
Dimensionality: 2
To perform the morphological opening, use imopen with the structuring element.
background = imopen(I,se);
imshow(background)
Subtract the background approximation image, background, from the original image, I, and view the
resulting image. After subtracting the adjusted background image from the original image, the
resulting image has a uniform background but is now a bit dark for analysis.
I2 = I - background;
imshow(I2)
Use imadjust to increase the contrast of the processed image I2 by saturating 1% of the data at
both low and high intensities and by stretching the intensity values to fill the uint8dynamic range.
I3 = imadjust(I2);
imshow(I3)
Note that the prior two steps 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));
Create a binary version of the processed image so you can use toolbox functions for analysis. Use
the imbinarize function to convert the grayscale image into a binary image. Remove background
noise from the image with the bwareaopen function.
bw = imbinarize(I3);
bw = bwareaopen(bw,50);
imshow(bw)
cc.NumObjects
ans = 95
View the rice grain that is labeled 50 in the image.
grain = false(size(bw));
grain(cc.PixelIdxList{50}) = true;
imshow(grain)
Visualize all the connected components in the image by creating a label matrix and then displaying it
as a pseudocolor 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
Create a new vector grain_areas, which holds the area measurement for each grain.
grain_areas = [graindata.Area];
Find the area of the 50th component.
grain_areas(50)
ans = 194
Find and display the grain with the smallest area.
[min_area, idx] = min(grain_areas)
min_area = 61
idx = 16
grain = false(size(bw));
grain(cc.PixelIdxList{idx}) = true;
imshow(grain)
Use the histogram command to create a histogram of rice grain areas.
histogram(grain_areas)
title('Histogram of Rice Grain Area')
This example shows how to use MATLAB® array arithmetic to process images and plot image data.
In particular, this example works with a three-dimensional image array where the three planes
represent the image signal from different parts of the electromagnetic spectrum, including the visible
red and near-infrared (NIR) channels.
Image data differences can be used to distinguish different surface features of an image, which have
varying reflectivity across different spectral channels. By finding differences between the visible red
and NIR channels, the example identifies areas containing significant vegetation.
The Seine River appears very dark in the NDVI image. The large light area near the left edge of the
image is the park (Bois de Boulogne) noted earlier.
subplot(1,2,2)
imshow(q)
colormap([0 0 1; 0 1 0]);
title('NDVI with Threshold Applied')