Digital Image Processing Segmntation Lab With Python
Digital Image Processing Segmntation Lab With Python
Basic Imports
import numpy as np
import matplotlib.pyplot as pltimport skimage.data as data
import skimage.segmentation as seg
import skimage.filters as filters
import skimage.draw as draw
import skimage.color as color
Image
text = data.page()
image_show(text)
Supervised thresholding
image_show(text_segmented);
Unsupervised thresholding
Scikit-image has a number of automatic thresholding methods, which require no
input in choosing an optimal threshold. Some of the methods are:
In the case of local, we also need to specify the block_size . Offset helps to tune the
image for better results.
text_threshold = filters.threshold_local(text,block_size=51, offset=10)
image_show(text > text_threshold);
However, in our case, the image is not very noisy, so we will take it as it is. Next
step would be to convert the image to grayscale with rgb2gray.
The skimage data module contains some inbuilt example data sets which are
generally stored in jpeg or png format.
from skimage import data
import numpy as np
import matplotlib.pyplot as pltimage = data.binary_blobs()
plt.imshow(image, cmap='gray')
Saving images
#Saving file as ‘logo.png’
io.imsave('logo.png', logo)
Active Contour segmentation also called snakes and is initialized using a user-
defined contour or line, around the area of interest, and this contour then slowly
contracts and is attracted or repelled from light and edges.
For our example image, let’s draw a circle around the person’s head to initialize the
snake.
def circle_points(resolution, center, radius): """
Generate points which define a circle on an image.Centre refers to the centre of
the circle
"""
radians = np.linspace(0, 2*np.pi, resolution) c = center[1] +
radius*np.cos(radians)#polar co-ordinates
r = center[0] + radius*np.sin(radians)
return np.array([c, r]).T# Exclude last point because a closed path should not
have duplicate points
points = circle_points(200, [80, 250], 80)[:-1]
The above calculations calculate x and y co-ordinates of the points on the periphery
of the circle. Since we have given the resolution to be 200, it will calculate 200
such points.
In this method, a user interactively labels a small number of pixels which are known
as labels. Each unlabeled pixel is then imagined to release a random walker and
one can then determine the probability of a random walker starting at each
unlabeled pixel and reaching one of the prelabeled pixels. By assigning each pixel
to the label for which the greatest probability is calculated, high-quality image
segmentation may be obtained.
All we’re doing is just setting each sub-image or sub-region that we have found, to
the average of that region which makes it look less like a patchwork of randomly
assigned colors and more like an image that has been decomposed into areas that
are kind of similar.
# label2rgb replaces each discrete label with the average interior color
image_show(color.label2rgb(image_slic, image, kind='avg'));
Felzenszwalb
image_felzenszwalb=seg.felzenszwalb(image)
image_show(image_felzenszwalb);
Region-based Segmentation
One simple way to segment different objects could be to use their pixel values. An
important point to note – the pixel values will be different for the objects and the
image’s background if there’s a sharp contrast between them.
In this case, we can set a threshold value. The pixel values falling below or above
that threshold can be classified accordingly (as an object or the background). This
technique is known as Threshold Segmentation.
If we want to divide the image into two regions (object and background), we define
a single threshold value. This is known as the global threshold.
If we have multiple objects along with the background, we must define multiple
thresholds. These thresholds are collectively known as the local threshold.
Note:
We will take the mean of the pixel values and use that as a threshold. If the
pixel value is more than our threshold, we can say that it belongs to an object. If
the pixel value is less than the threshold, it will be treated as the background.
There is always an edge between two adjacent regions with different grayscale
values (pixel values). The edges can be considered as the discontinuous local
features of an image.
We can make use of this discontinuity to detect edges and hence define a boundary
of the object. This helps us in detecting the shapes of multiple objects present in a
given image.
The values of the weight matrix define the output of the convolution.
Steps to be followed:
Clustering is the task of dividing the population (data points) into a number of
groups, such that data points in the same groups are more similar to other data
points in that same group than those in other groups. These groups are known as
clusters.
One of the most commonly used clustering algorithms is k-means. Here, the k
represents the number of clusters (not to be confused with k-nearest neighbor).
Let’s understand how k-means works:
The key advantage of using k-means algorithm is that it is simple and easy to
understand. We are assigning the points to the clusters which are closest to them.
Fromsklearn.cluster
importKMeans
kmeans = KMeans(n_clusters=5, random_state=0).fit(pic_n)
pic2show = kmeans.cluster_centers_[kmeans.labels_]
cluster_pic = pic2show.reshape(pic.shape[0], pic.shape[1], pic.shape[2])
plt.imshow(cluster_pic)
k-means works really well when we have a small dataset. It can segment the objects
in the image and give impressive results. But the algorithm hits a roadblock when
applied on a large dataset (more number of images).
It looks at all the samples at every iteration, so the time taken is too high. Hence,
it’s also too expensive to implement. And since k-means is a distance-based
algorithm, it is only applicable to convex datasets and is not suitable for
clustering non-convex clusters.
Mask R-CNN
Its class
The bounding box coordinates
Mask R-CNN adds a third branch to this which outputs the object mask as well.
Steps to be followed:
1. We take an image as input and pass it to the ConvNet, which returns the
feature map for that image
2. Region proposal network (RPN) is applied on these feature maps. This
returns the object proposals along with their objectness score
3. A RoI pooling layer is applied on these proposals to bring down all the
proposals to the same size
4. Finally, the proposals are passed to a fully connected layer to classify and
output the bounding boxes for objects. It also returns the mask for each
proposal
Mask R-CNN is the current state-of-the-art for image segmentation and runs at 5
fps.