Experiment 8
Experiment 8
Introduction
Rescale operation resizes an image by a given scaling factor. The scaling factor can
either be a single floating point value, or multiple values -one along each axis.
Resize serves the same purpose, but allows to specify an output image shape
instead of a scaling factor. Note that when down-sampling an image, resize and
rescale should perform Gaussian smoothing to avoid aliasing artifacts. See the
anti_aliasing and anti_aliasing_sigma arguments to these functions. Downscale
serves the purpose of down-sampling an n-dimensional image by integer factors
using the local mean on the elements of eachblock of the size factors given as a
parameter to the function.
import cv2
import numpy as np
import matplotlib.pyplot as plt
#image=files.upload()
img=cv2.imread('img.jpg')
#img = cv2.resize(img, (300,300))
matrix=np.ones(img.shape, dtype="uint8")*70
img_brighter=cv2.add(img,matrix)
img_darker=cv2.subtract(img,matrix)
plt.subplot(141);
plt.imshow(img)
plt.title("original image")
plt.subplot(142);
plt.imshow(img_brighter)
plt.title("brighter image")
plt.subplot(143);
plt.imshow(img_darker)
plt.title("darker image");
Filtering regional maxima
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
from skimage import data
from skimage import img_as_float
from skimage.morphology import reconstruction
image = img_as_float(data.coins())
image = gaussian_filter(image, 1)
seed = np.copy(image)
seed[1:-1, 1:-1] = image.min()
mask = image
dilated = reconstruction(seed, mask, method='dilation')
fig, (ax0, ax1, ax2) = plt.subplots(nrows=1,
ncols=3,
figsize=(8, 2.5),
sharex=True,
sharey=True)
ax0.imshow(image, cmap='gray')
ax0.set_title('original image 22102088')
ax0.axis('off')
ax1.imshow(dilated, vmin=image.min(), vmax=image.max(), cmap='gray')
ax1.set_title('dilated 22102088')
ax1.axis('off')
ax2.imshow(image - dilated, cmap='gray')
ax2.set_title('image - dilated 22102088')
ax2.axis('off')
fig.tight_layout()
Although the features (i.e. the coins) are clearly isolated, the coins surrounded
by a bright background in the original image are dimmer in thesubtracted
image.
We can attempt to correct this using a different seed image. Instead of creating
a seed image with maxima along the image border, we can use the features of
the image itself to seed the reconstructionprocess. Here, the seed image is the
original image minus a fixed value, h
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
from skimage import data
from skimage import img_as_float
from skimage.morphology import reconstruction
h = 0.4
seed = image - h
dilated = reconstruction(seed, mask, method='dilation')
hdome = image - dilated
fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(8, 2.5))
yslice = 197
ax0.plot(mask[yslice], '0.5', label='mask')
ax0.plot(seed[yslice], 'k', label='seed')
ax0.plot(dilated[yslice], 'r', label='dilated')
ax0.set_ylim(-0.2, 2)
ax0.set_title('image slice 22102088')
ax0.set_xticks([])
ax0.legend()
ax1.imshow(dilated, vmin=image.min(), vmax=image.max(), cmap='gray')
ax1.axhline(yslice, color='r', alpha=0.4)
ax1.set_title('dilated 22102088')
ax1.axis('off')
ax2.imshow(hdome, cmap='gray')
ax2.axhline(yslice, color='r', alpha=0.4)
ax2.set_title('image - dilated 22102088')
ax2.axis('off')
fig.tight_layout()
plt.show()
As you can see in the image slice, each coin is given a different baseline
intensity in the reconstructed image; this is because we used the local
intensity (shifted by h ) as a seed value.
As a result, the coins in the subtracted image have similar pixel intensities.The
final result is known as the h-dome of an image since this tends to isolate
regional maxima of height h . This operation is particularly useful when your
images are unevenly illuminated.
This example compares the following mean filters of the rank filter package:
local mean: all pixels belonging to the structuring element to compute average
gray level.
percentile mean: only use values between percentiles p0 and p1 (here 10% and
90%).
bilateral mean: onlyuse pixels of the structuring element having a gray level
situated inside g-s0 and g+s1 (here g-500 and g+500)
Percentile and usual mean give here similar results, these filters smooth the
complete image (background and details). Bilateral means exhibits a high
filtering rate for continuous area (i.e. background) while higher image
frequencies remain untouched.
plt.tight_layout()
plt.show()
In Lab Exercise :
Edge operators are used in image processing within edge detection
algorithms. They are discrete differentiation operators, computing an
approximation of the gradient of the image intensity function.
import numpy as np
import matplotlib.pyplot as plt
from skimage import filters
from skimage.data import camera
from skimage.util import compare_images
image = camera()
edge_roberts = filters.roberts(image)
edge_sobel = filters.sobel(image)
fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True,
figsize=(8, 4))
axes[0].imshow(edge_roberts, cmap=plt.cm.gray)
axes[0].set_title('Roberts Edge Detection 22102088')
axes[1].imshow(edge_sobel, cmap=plt.cm.gray)
axes[1].set_title('Sobel Edge Detection 22102088')
for ax in axes:
ax.axis('off')
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from skimage import filters
from skimage.data import camera
from skimage.util import compare_images
x, y = np.ogrid[:100, :100]
image_rot = np.exp(1j * np.hypot(x, y) ** 1.3 / 20.).real
edge_sobel = filters.sobel(image_rot)
edge_scharr = filters.scharr(image_rot)
edge_prewitt = filters.prewitt(image_rot)
diff_scharr_prewitt = compare_images(edge_scharr, edge_prewitt)
diff_scharr_sobel = compare_images(edge_scharr, edge_sobel)
max_diff = np.max(np.maximum(diff_scharr_prewitt, diff_scharr_sobel))
fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True,
figsize=(8, 8))
axes = axes.ravel()
axes[0].imshow(image_rot, cmap=plt.cm.gray)
axes[0].set_title('Original image 22102088')
axes[1].imshow(edge_scharr, cmap=plt.cm.gray)
axes[1].set_title('Scharr Edge Detection 22102088')
axes[2].imshow(diff_scharr_prewitt, cmap=plt.cm.gray, vmax=max_diff)
axes[2].set_title('Scharr - Prewitt 22102088')
axes[3].imshow(diff_scharr_sobel, cmap=plt.cm.gray, vmax=max_diff)
axes[3].set_title('Scharr - Sobel 22102088')
for ax in axes:
ax.axis('off')
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from skimage import filters
from skimage.data import camera
from skimage.util import compare_images
x, y = np.mgrid[-10:10:255j, -10:10:255j]
image_rotinv = np.sin(x ** 2 + y ** 2)
image_x = 2 * x * np.cos(x ** 2 + y ** 2)
image_y = 2 * y * np.cos(x ** 2 + y ** 2)
def angle(dx, dy):
return np.mod(np.arctan2(dy, dx), np.pi)
true_angle = angle(image_x, image_y)
angle_farid = angle(filters.farid_h(image_rotinv),
filters.farid_v(image_rotinv))
angle_sobel = angle(filters.sobel_h(image_rotinv),
filters.sobel_v(image_rotinv))
angle_scharr = angle(filters.scharr_h(image_rotinv),
filters.scharr_v(image_rotinv))
angle_prewitt = angle(filters.prewitt_h(image_rotinv),
filters.prewitt_v(image_rotinv))
def diff_angle(angle_1, angle_2):
return np.minimum(np.pi - np.abs(angle_1 - angle_2),
np.abs(angle_1 - angle_2))
diff_farid = diff_angle(true_angle, angle_farid)
diff_sobel = diff_angle(true_angle, angle_sobel)
diff_scharr = diff_angle(true_angle, angle_scharr)
diff_prewitt = diff_angle(true_angle, angle_prewitt)
fig, axes = plt.subplots(nrows=3, ncols=2, sharex=True, sharey=True,
figsize=(8, 8))
axes = axes.ravel()
axes[0].imshow(image_rotinv, cmap=plt.cm.gray)
axes[0].set_title('Original image 22102088')
axes[1].imshow(true_angle, cmap=plt.cm.hsv)
axes[1].set_title('Analytical gradient angle 22102088')
axes[2].imshow(diff_sobel, cmap=plt.cm.inferno, vmin=0, vmax=0.02)
axes[2].set_title('Sobel error 22102088')
axes[3].imshow(diff_prewitt, cmap=plt.cm.inferno, vmin=0, vmax=0.02)
axes[3].set_title('Prewitt error 22102088')
axes[4].imshow(diff_scharr, cmap=plt.cm.inferno, vmin=0, vmax=0.02)
axes[4].set_title('Scharr error 22102088')
color_ax = axes[5].imshow(diff_farid, cmap=plt.cm.inferno, vmin=0,
vmax=0.02)
axes[5].set_title('Farid error 22102088')
fig.subplots_adjust(right=0.8)
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, img_as_float
from skimage.filters import gaussian
from skimage.morphology import reconstruction
image = img_as_float(io.imread('kitten.jpg'))
if image.ndim == 3:
image = np.mean(image, axis=2)
smoothed = gaussian(image, sigma=1)
seed = np.copy(smoothed)
seed[1:-1, 1:-1] = smoothed.min()
mask = smoothed
dilated = reconstruction(seed, mask, method='dilation')
foreground = smoothed - dilated
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
axes[0].imshow(smoothed, cmap='gray')
axes[0].set_title('Smoothed Image 2210208')
axes[0].axis('off')
axes[1].imshow(dilated, cmap='gray')
axes[1].set_title('Dilated Background 2210208')
axes[1].axis('off')
axes[2].imshow(foreground, cmap='gray')
axes[2].set_title('Foreground Extraction 2210208')
axes[2].axis('off')
plt.tight_layout()
plt.show()
Results: Image quality was successfully enhanced using processing techniques. Rescaling
and resizing preserved clarity, while adjustments to brightness and contrast improved
visibility. Morphological reconstruction isolated key features, and edge detection methods
highlighted structural details, with each operator proving effective in different contexts.