C065 CV Exp1
C065 CV Exp1
Yash Oza
C065
display image
In [ ]: image
file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 1/24
1/9/25, 10:01 PM C065_CV_Exp1
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
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
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
In [ ]: plt.imshow(grad_x,cmap='gray')
plt.show();
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();
file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 6/24
1/9/25, 10:01 PM C065_CV_Exp1
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
file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 8/24
1/9/25, 10:01 PM C065_CV_Exp1
In [ ]: plt.imshow(angle,cmap='gray')
plt.show();
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
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
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
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();
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();
file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 14/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 15/24
1/9/25, 10:01 PM C065_CV_Exp1
In [ ]: image
file:///C:/Users/hp/Downloads/C065_CV_Exp1.html 16/24
1/9/25, 10:01 PM C065_CV_Exp1
In [ ]: image.shape
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
In [ ]: rown,coln = image.shape
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
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
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();
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();
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