Aim:
2B. OpenCV Drawing Functions, Eroding an Image, Blurring an Image, Create Border around Images,
Grayscaling of Images, Scaling, Rotating, Erosion and Dilation of images.
Theory:
1) OpenCV Drawing Functions:
Drawing functions allow you to create shapes, lines, text, and other annotations on images, which is
helpful for highlighting regions or creating custom graphics.
●Uses: Marking objects, labeling images, designing overlays.
●Common Functions:
[Link]() → Draws a straight line.
[Link]() → Draws rectangles.
[Link]() → Draws circles.
[Link]() → Adds text to an image.
2) Blurring an Image:
Blurring smooths out details in an image by averaging pixel values, reducing noise and sharp edges.
●Uses: Noise reduction, background smoothing, preprocessing for edge detection.
●Function: [Link]() (common for natural smoothing), [Link](), or [Link]().
●Working: Applies a filter kernel (e.g., 3×3) that averages pixel values in its neighborhood.
3) Create Border around Images:
Adding a border (padding) around an image is useful for framing or resizing purposes.
●Uses: Creating margins, preparing datasets for uniform size, design effects.
●Function: [Link]().
●Border Types:
Constant (solid color border)
Reflect (mirror image border)
Replicate (repeat edge pixels)
4) Grayscaling of Image:
Grayscale converts a color image (with RGB channels) into shades of gray (single channel).
●Uses: Simplifies image processing, reduces computation, common in machine learning.
●Function: [Link](image, cv2.COLOR_BGR2GRAY).
●Working: Each pixel’s color is converted to intensity based on weighted RGB values.
5) Erosion and Dilation of Images
These are morphological operations applied to binary or grayscale images to modify object shapes.
●Erosion: Removes pixels on object boundaries, making objects smaller.
●Dilation: Adds pixels to object boundaries, making objects larger.
●Uses: Removing noise, closing small holes, refining shapes.
●Functions:
[Link]() → Erosion
[Link]() → Dilation
●Working: Uses a kernel (structuring element) to slide over the image and apply min/max operations.
Code:
import cv2
import numpy as np
from [Link] import cv2_imshow
img = [Link]("/content/image_smiley.jpg")
resized_img = [Link](img,(400,300))
resized_img1 = [Link](img,(400,300))
resized_img2 = [Link](img,(400,300))
resized_img3 = [Link](img,(400,300))
rectange_imag = [Link](resized_img1,(50,20),(350,280),(0,0,0),3)
lined_img = [Link](resized_img2,(50,20),(350,280),(0,0,0),3)
circle_img = [Link](resized_img3,(200,150),130,(0,0,0),3)
cv2_imshow(rectange_imag)
cv2_imshow(lined_img)
cv2_imshow(circle_img)
grey_img = [Link](resized_img,cv2.COLOR_BGR2GRAY)
cv2_imshow(grey_img)
blur = [Link](grey_img,(5,5),0)
cv2_imshow(blur)
border_img = [Link](resized_img,10,10,10,10,cv2.BORDER_CONSTANT,value=[0,0,0])
cv2_imshow(border_img)
erosion_img = [Link](resized_img,(5,5),iterations=1)
cv2_imshow(erosion_img)
dilation_img = [Link](resized_img,(5,5),iterations=1)
cv2_imshow(dilation_img)
Output:
Conclusion:
Basic OpenCV operations like drawing, blurring, adding borders, grayscaling, and
morphological transformations are essential for image enhancement, preprocessing, and analysis,
forming the foundation for advanced computer vision tasks.