0% found this document useful (0 votes)
2 views24 pages

C065 CV Exp1

The document outlines a series of tasks involving the application of the Sobel filter for edge detection in images. It includes steps for applying the filter, calculating gradients, displaying results, and analyzing edge orientations. Additionally, it explores the effects of changing the filter size on the resulting edge images.

Uploaded by

yashoza1203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

C065 CV Exp1

The document outlines a series of tasks involving the application of the Sobel filter for edge detection in images. It includes steps for applying the filter, calculating gradients, displaying results, and analyzing edge orientations. Additionally, it explores the effects of changing the filter size on the resulting edge images.

Uploaded by

yashoza1203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

1/9/25, 10:01 PM C065_CV_Exp1

Yash Oza

C065

task 1: Identify edges of image using


sobel filter of size 3x3

task 2: apply same algorithm on any


other image

task 3: increase size of sobel filter and


observe the effects on filtered image
In [ ]: import cv2
import numpy as np
from skimage import data
import matplotlib.pyplot as plt

Task1: Apply Sobel filter to determine


gradients using Sobel filter of size 3x3
In [ ]: image = data.camera()

display image
In [ ]: image

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 1/24
1/9/25, 10:01 PM C065_CV_Exp1

Out[ ]: ndarray (512, 512) show data

In [ ]: plt.imshow(image,cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 2/24
1/9/25, 10:01 PM C065_CV_Exp1

checking image shape


In [ ]: image.shape

Out[ ]: (512, 512)

In [ ]: rown,coln = image.shape

In [ ]: help(cv2.Sobel)

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 3/24
1/9/25, 10:01 PM C065_CV_Exp1

Help on built-in function Sobel:

Sobel(...)
Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]]) ->
dst
. @brief Calculates the first, second, third, or mixed image derivatives us
ing an extended Sobel operator.
.
. In all cases except one, the \f$\texttt{ksize} \times \texttt{ksize}\f$ s
eparable kernel is used to
. calculate the derivative. When \f$\texttt{ksize = 1}\f$, the \f$3 \times
1\f$ or \f$1 \times 3\f$
. kernel is used (that is, no Gaussian smoothing is done). `ksize = 1` can
only be used for the first
. or the second x- or y- derivatives.
.
. There is also the special value `ksize = #FILTER_SCHARR (-1)` that corres
ponds to the \f$3\times3\f$ Scharr
. filter that may give more accurate results than the \f$3\times3\f$ Sobel.
The Scharr aperture is
.
. \f[\vecthreethree{-3}{0}{3}{-10}{0}{10}{-3}{0}{3}\f]
.
. for the x-derivative, or transposed for the y-derivative.
.
. The function calculates an image derivative by convolving the image with
the appropriate kernel:
.
. \f[\texttt{dst} = \frac{\partial^{xorder+yorder} \texttt{src}}{\partial
x^{xorder} \partial y^{yorder}}\f]
.
. The Sobel operators combine Gaussian smoothing and differentiation, so th
e result is more or less
. resistant to the noise. Most often, the function is called with ( xorder
= 1, yorder = 0, ksize = 3)
. or ( xorder = 0, yorder = 1, ksize = 3) to calculate the first x- or y- i
mage derivative. The first
. case corresponds to a kernel of:
.
. \f[\vecthreethree{-1}{0}{1}{-2}{0}{2}{-1}{0}{1}\f]
.
. The second case corresponds to a kernel of:
.
. \f[\vecthreethree{-1}{-2}{-1}{0}{0}{0}{1}{2}{1}\f]
.
. @param src input image.
. @param dst output image of the same size and the same number of channels
as src .
. @param ddepth output image depth, see @ref filter_depths "combinations";
in the case of
. 8-bit input images it will result in truncated derivatives.
. @param dx order of the derivative x.
. @param dy order of the derivative y.
. @param ksize size of the extended Sobel kernel; it must be 1, 3, 5, or 7.
. @param scale optional scale factor for the computed derivative values; by
default, no scaling is
. applied (see #getDerivKernels for details).
. @param delta optional delta value that is added to the results prior to s
toring them in dst.
. @param borderType pixel extrapolation method, see #BorderTypes. #BORDER_W

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 4/24
1/9/25, 10:01 PM C065_CV_Exp1

RAP is not supported.


. @sa Scharr, Laplacian, sepFilter2D, filter2D, GaussianBlur, cartToPolar

Task2: Determine magnitude and angle of


each edge point
calculating the gradients accros x and y direction
In [ ]: grad_x = cv2.Sobel(image,ddepth = cv2.CV_32F,dx = 1,dy=0,ksize=3)
grad_y = cv2.Sobel(image,cv2.CV_32F,0,1,ksize=3)

display gradient accross x direction

In [ ]: plt.imshow(grad_x,cmap='gray')
plt.show();

display gradient accross y direction

In [ ]: plt.imshow(grad_y,cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 5/24
1/9/25, 10:01 PM C065_CV_Exp1

In [ ]: plt.imshow(abs(grad_x),cmap='gray')
plt.show();

convert to absolute using cv2


In [ ]: grad_x_abs = cv2.convertScaleAbs(grad_x)
grad_y_abs = cv2.convertScaleAbs(grad_y)

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 6/24
1/9/25, 10:01 PM C065_CV_Exp1

displaying abs gradx and grady


In [ ]: plt.imshow(grad_x_abs,cmap='gray')
plt.show();

In [ ]: plt.imshow(grad_y_abs,cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 7/24
1/9/25, 10:01 PM C065_CV_Exp1

comparing the image and abs grad images


In [ ]: plt.figure(figsize=(15,10))
plt.subplot(1,3,1)
plt.title('Original image')
plt.imshow(image,cmap='gray')
plt.subplot(1,3,2)
plt.title('Absolute Grad X image')
plt.imshow(cv2.convertScaleAbs(grad_x),cmap='gray')
plt.subplot(1,3,3)
plt.imshow(cv2.convertScaleAbs(grad_y),cmap='gray')
plt.title('Absolute Grad Y image')
plt.axis('off') # command for hiding the axis.
plt.show();

Task3: Show magnitude of edge pixels


In [ ]: plt.figure(figsize=(15,10))

edge_img = np.sqrt(np.square(grad_x) + np.square(grad_y))


plt.subplot(1,2,1)
plt.title('Original image')
plt.imshow(image,cmap='gray')
plt.subplot(1,2,2)
plt.imshow(edge_img,cmap='gray')
plt.title('Edge image')

Out[ ]: Text(0.5, 1.0, 'Edge image')

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 8/24
1/9/25, 10:01 PM C065_CV_Exp1

In [ ]: angle = np.arctan2(grad_y,grad_x) * 180/np.pi % 180

In [ ]: plt.imshow(angle,cmap='gray')
plt.show();

Task4: Plot histogram of angles and


identify angles with maximum frequency
In [ ]: angle_1D = np.reshape(angle,(rown*coln,1))

In [ ]: plt.hist(angle_1D,bins=180,color = "lightblue")
plt.xlabel('Orientation of edge')
plt.ylabel('pixel count')

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 9/24
1/9/25, 10:01 PM C065_CV_Exp1

plt.title('Histogram of edge orientation')


plt.show();

Angles having maximum frequency are noted in conclusion

Task5: Increase the size of Sobel filter and


observe the effect (changing size of filter
to 31x31)
In [ ]: image = data.camera()

In [ ]: grad_x = cv2.Sobel(image,ddepth = cv2.CV_32F,dx = 1,dy=0,ksize=31)


grad_y = cv2.Sobel(image,cv2.CV_32F,0,1,ksize=31)

display gradient accross x direction

In [ ]: plt.imshow(grad_x,cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 10/24
1/9/25, 10:01 PM C065_CV_Exp1

display gradient accross y direction

In [ ]: plt.imshow(grad_y,cmap='gray')
plt.show();

In [ ]: plt.imshow(abs(grad_x),cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 11/24
1/9/25, 10:01 PM C065_CV_Exp1

convert to absolute using cv2


In [ ]: grad_x_abs = cv2.convertScaleAbs(grad_x)
grad_y_abs = cv2.convertScaleAbs(grad_y)

displaying abs gradx and grady


In [ ]: plt.imshow(grad_x_abs,cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 12/24
1/9/25, 10:01 PM C065_CV_Exp1

In [ ]: plt.imshow(grad_y_abs,cmap='gray')
plt.show();

comparing the image and abs grad images


In [ ]: plt.figure(figsize=(15,10))
plt.subplot(1,3,1)

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 13/24
1/9/25, 10:01 PM C065_CV_Exp1

plt.title('Original image')
plt.imshow(image,cmap='gray')
plt.subplot(1,3,2)
plt.title('Absolute Grad X image')
plt.imshow(cv2.convertScaleAbs(grad_x),cmap='gray')
plt.subplot(1,3,3)
plt.imshow(cv2.convertScaleAbs(grad_y),cmap='gray')
plt.title('Absolute Grad Y image')
plt.axis('off') # command for hiding the axis.
plt.show();

calculate the edge magnitude to detect the edges


of the image
In [ ]: plt.figure(figsize=(15,10))

edge_img = np.sqrt(np.square(grad_x) + np.square(grad_y))


plt.subplot(1,2,1)
plt.title('Original image')
plt.imshow(image,cmap='gray')
plt.subplot(1,2,2)
plt.imshow(edge_img,cmap='gray')
plt.title('Edge image')

<ipython-input-48-9bc4f4b1d66b>:3: RuntimeWarning: overflow encountered in add


edge_img = np.sqrt(np.square(grad_x) + np.square(grad_y))
Out[ ]: Text(0.5, 1.0, 'Edge image')

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 14/24
1/9/25, 10:01 PM C065_CV_Exp1

In [ ]: angle = np.arctan2(grad_y,grad_x) * 180/np.pi % 180

In [ ]: plt.imshow(angle,cmap='gray')
plt.show();

In [ ]: angle_1D = np.reshape(angle,(rown*coln,1))

In [ ]: plt.hist(angle_1D,bins=180,color = "lightblue")
plt.xlabel('Orientation of edge')
plt.ylabel('pixel count')
plt.title('Histogram of edge orientation')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 15/24
1/9/25, 10:01 PM C065_CV_Exp1

Running sobel of size (3x3) on new image


In [ ]: image = cv2.imread('/content/drive/MyDrive/CV_sem12/Exp1/edge image.jpg',0)

In [ ]: image

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 16/24
1/9/25, 10:01 PM C065_CV_Exp1

Out[ ]: ndarray (980, 980) show data

In [ ]: image.shape

Out[ ]: (980, 980)

In [ ]: plt.imshow(image,cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 17/24
1/9/25, 10:01 PM C065_CV_Exp1

checking image shape


In [ ]: image.shape

Out[ ]: (980, 980)

In [ ]: rown,coln = image.shape

calculating the gradients of the image using sobel

calculating the gradients accros x and y direction


In [ ]: grad_x = cv2.Sobel(image,ddepth = cv2.CV_32F,dx = 1,dy=0,ksize=3)
grad_y = cv2.Sobel(image,cv2.CV_32F,0,1,ksize=3)

display gradient accross x direction

In [ ]: plt.imshow(grad_x,cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 18/24
1/9/25, 10:01 PM C065_CV_Exp1

display gradient accross y direction

In [ ]: plt.imshow(grad_y,cmap='gray')
plt.show();

In [ ]: plt.imshow(abs(grad_x),cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 19/24
1/9/25, 10:01 PM C065_CV_Exp1

convert to absolute using cv2


In [ ]: grad_x_abs = cv2.convertScaleAbs(grad_x)
grad_y_abs = cv2.convertScaleAbs(grad_y)

displaying abs gradx and grady


In [ ]: plt.imshow(grad_x_abs,cmap='gray')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 20/24
1/9/25, 10:01 PM C065_CV_Exp1

In [ ]: plt.imshow(grad_y_abs,cmap='gray')
plt.show();

comparing the image and abs grad images


In [ ]: plt.figure(figsize=(15,10))
plt.subplot(1,3,1)

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 21/24
1/9/25, 10:01 PM C065_CV_Exp1

plt.title('Original image')
plt.imshow(image,cmap='gray')
plt.subplot(1,3,2)
plt.title('Absolute Grad X image')
plt.imshow(cv2.convertScaleAbs(grad_x),cmap='gray')
plt.subplot(1,3,3)
plt.imshow(cv2.convertScaleAbs(grad_y),cmap='gray')
plt.title('Absolute Grad Y image')
plt.axis('off') # command for hiding the axis.
plt.show();

calculate the edge magnitude to detect the edges


of the image
In [ ]: plt.figure(figsize=(15,10))

edge_img = np.sqrt(np.square(grad_x) + np.square(grad_y))


plt.subplot(1,2,1)
plt.title('Original image')
plt.imshow(image,cmap='gray')
plt.subplot(1,2,2)
plt.imshow(edge_img,cmap='gray')
plt.title('Edge image')

Out[ ]: Text(0.5, 1.0, 'Edge image')

In [ ]: angle = np.arctan2(grad_y,grad_x) * 180/np.pi % 180

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 22/24
1/9/25, 10:01 PM C065_CV_Exp1

In [ ]: plt.imshow(angle,cmap='gray')
plt.show();

In [ ]: angle_1D = np.reshape(angle,(rown*coln,1))

In [ ]: plt.hist(angle_1D,bins=180,color = "lightblue")
plt.xlabel('Orientation of edge')
plt.ylabel('pixel count')
plt.title('Histogram of edge orientation')
plt.show();

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 23/24
1/9/25, 10:01 PM C065_CV_Exp1

Conclusion:
1. sobel filter of size 3x3 detects the small edges of given image.
2. About 20K pixels have edges with angle of 0 degree and 17,500 have angle of 45
and 135
3. Max no. of pixels have angle of about 0,45,85 and 135
4. If size of sobel filter is increased from 3x3 to 15x15 and 31x31 then filtered image
shows that large edges are detected and small edges disappear
5. Histogram of angle show that filter with large size can detect much more edges &
angles.

file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 24/24

You might also like