0% found this document useful (0 votes)
148 views

Segmentation - Ipynb - Colaboratory

The document discusses image segmentation techniques in Python. It loads an image, converts it to grayscale, and applies thresholding methods like Otsu's thresholding and Niblack thresholding to generate binary images. It also uses active contour models and the Chan-Vese algorithm for segmentation. Plots are generated to visualize the original, processed, and segmented images.

Uploaded by

Manjot Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

Segmentation - Ipynb - Colaboratory

The document discusses image segmentation techniques in Python. It loads an image, converts it to grayscale, and applies thresholding methods like Otsu's thresholding and Niblack thresholding to generate binary images. It also uses active contour models and the Chan-Vese algorithm for segmentation. Plots are generated to visualize the original, processed, and segmented images.

Uploaded by

Manjot Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

09/06/2022, 15:08 Segmentation.

ipynb - Colaboratory

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

from numpy import expand_dims
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt
from matplotlib import pyplot
from skimage.color import rgb2gray
from skimage.color import rgb2hsv
import matplotlib.pyplot as plt

# load the image
img = load_img('Healthy_1.jpeg')
# convert to numpy array
data = img_to_array(img)

plt.subplot(1, 2, 1)
 
# Displaying the sample image
plt.imshow(data)
 
# Converting RGB image to Monochrome
gray_image = rgb2gray(data)
plt.subplot(1, 2, 2)
 
# Displaying the sample image - Monochrome
# Format
plt.imshow(gray_image, cmap="gray")

https://colab.research.google.com/drive/1lSBY_UgQ754UwEr2IPsSd9RPBfhTNxzt#printMode=true 1/8
09/06/2022, 15:08 Segmentation.ipynb - Colaboratory

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

<matplotlib.image.AxesImage at 0x7f5488e37650>

Saved successfully!

hsv_image = rgb2hsv(data)

plt.subplot(1, 2, 2)

# Displaying the sample image - HSV Format

hsv_data_colorbar = plt.imshow(hsv_image)

# Adjusting colorbar to fit the size of the image

plt.colorbar(hsv_data_colorbar, fraction=0.046, pad=0.04)

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

<matplotlib.colorbar.Colorbar at 0x7f548881eb10>

from skimage import filters

from skimage.color import rgb2gray

import matplotlib.pyplot as plt

# Setting plot size to 15, 15

plt.figure(figsize=(15, 15))

https://colab.research.google.com/drive/1lSBY_UgQ754UwEr2IPsSd9RPBfhTNxzt#printMode=true 2/8
09/06/2022, 15:08 Segmentation.ipynb - Colaboratory

# Sample Image of scikit-image package

gray_coffee = rgb2gray(data)

 
Saved successfully!
# Computing Otsu's thresholding value

threshold = filters.threshold_otsu(gray_coffee)

# Computing binarized values using the obtained

# threshold

binarized_coffee = (gray_coffee > threshold)*1

plt.subplot(2,2,1)

plt.title("Threshold: >"+str(threshold))

# Displaying the binarized image

plt.imshow(binarized_coffee, cmap = "gray")

# Computing Ni black's local pixel

# threshold values for every pixel

threshold = filters.threshold_niblack(gray_coffee)

# Computing binarized values using the obtained

# threshold

binarized_coffee = (gray_coffee > threshold)*1

plt.subplot(2,2,2)

plt.title("Niblack Thresholding")

# Displaying the binarized image

plt.imshow(binarized_coffee, cmap = "gray")

# Computing Sauvola's local pixel threshold

# values for every pixel - Not Binarized

threshold = filters.threshold_sauvola(gray_coffee)

plt.subplot(2,2,3)

plt.title("Sauvola Thresholding")

# Displaying the local threshold values

https://colab.research.google.com/drive/1lSBY_UgQ754UwEr2IPsSd9RPBfhTNxzt#printMode=true 3/8
09/06/2022, 15:08 Segmentation.ipynb - Colaboratory

plt.imshow(threshold, cmap = "gray")

# Computing Sauvola's local pixel

# threshold values for every pixel - Binarized

binarized_coffee = (gray_coffee > threshold)*1

Saved successfully!
plt.subplot(2,2,4)

plt.title("Sauvola Thresholding - Converting to 0's and 1's")

# Displaying the binarized image

plt.imshow(binarized_coffee, cmap = "gray")

https://colab.research.google.com/drive/1lSBY_UgQ754UwEr2IPsSd9RPBfhTNxzt#printMode=true 4/8
09/06/2022, 15:08 Segmentation.ipynb - Colaboratory

<matplotlib.image.AxesImage at 0x7f5482016050>

Saved successfully!

import numpy as np

import matplotlib.pyplot as plt

from skimage.color import rgb2gray

from skimage import data

from skimage.filters import gaussian

from skimage.segmentation import active_contour

# Sample Image of scikit-image package

img = load_img('Healthy_1.jpeg')

# convert to numpy array

data = img_to_array(img)

gray_astronaut = rgb2gray(data)

# Applying Gaussian Filter to remove noise

gray_astronaut_noiseless = gaussian(gray_astronaut, 1)

# Localising the circle's center at 220, 110

x1 = 220 + 100*np.cos(np.linspace(0, 2*np.pi, 500))

x2 = 100 + 100*np.sin(np.linspace(0, 2*np.pi, 500))

https://colab.research.google.com/drive/1lSBY_UgQ754UwEr2IPsSd9RPBfhTNxzt#printMode=true 5/8
09/06/2022, 15:08 Segmentation.ipynb - Colaboratory

# Generating a circle based on x1, x2

snake = np.array([x1, x2]).T

# Computing the Active Contour for the given image

astronaut_snake = active_contour(gray_astronaut_noiseless,

Saved successfully!
                                 snake)

fig = plt.figure(figsize=(10, 10))

# Adding subplots to display the markers

ax = fig.add_subplot(111)

# Plotting sample image

ax.imshow(gray_astronaut_noiseless)

# Plotting the face boundary marker

ax.plot(astronaut_snake[:, 0],

        astronaut_snake[:, 1],

        '-b', lw=5)

# Plotting the circle around face

ax.plot(snake[:, 0], snake[:, 1], '--r', lw=5)

[<matplotlib.lines.Line2D at 0x7f547d577ed0>]

import matplotlib.pyplot as plt

from skimage.color import rgb2gray

from skimage import data, img_as_float

from skimage.segmentation import chan_vese

fig, axes = plt.subplots(1, 3, figsize=(10, 10))

# Sample Image of scikit-image package

# Sample Image of scikit-image package

https://colab.research.google.com/drive/1lSBY_UgQ754UwEr2IPsSd9RPBfhTNxzt#printMode=true 6/8
09/06/2022, 15:08 Segmentation.ipynb - Colaboratory

img = load_img('Healthy_1.jpeg')

# convert to numpy array

data = img_to_array(img)

gray_astronaut = rgb2gray(data)

 
Saved successfully!
# Computing the Chan VESE segmentation technique

chanvese_gray_astronaut = chan_vese(gray_astronaut,

                                    max_iter=100,

                                    extended_output=True)

ax = axes.flatten()

# Plotting the original image

ax[0].imshow(gray_astronaut, cmap="gray")

ax[0].set_title("Original Image")

# Plotting the segmented - 100 iterations image

ax[1].imshow(chanvese_gray_astronaut[0], cmap="gray")

title = "Chan-Vese segmentation - {} iterations".format(len(chanvese_gray_astronaut[2]))

ax[1].set_title(title)

# Plotting the final level set

ax[2].imshow(chanvese_gray_astronaut[1], cmap="gray")

ax[2].set_title("Final Level Set")

plt.show()

https://colab.research.google.com/drive/1lSBY_UgQ754UwEr2IPsSd9RPBfhTNxzt#printMode=true 7/8
09/06/2022, 15:08 Segmentation.ipynb - Colaboratory

Saved successfully!

https://colab.research.google.com/drive/1lSBY_UgQ754UwEr2IPsSd9RPBfhTNxzt#printMode=true 8/8

You might also like