OPENCV Lab1
OPENCV Lab1
LAB SESSION
INTRODUCTION TO OPENCV
To create a chessboard using numpy and OpenCV, we can follow these steps:
Initialize the board: Create an 8x8 array where each element represents a square on
the chessboard.
Color the squares: Assign alternating colors to the squares to mimic a real chessboard.
Resize and display the image: Use OpenCV to scale the numpy array to a larger size and
display it.
Step 1: We initialize a numpy array of zeros to represent an 8x8 chessboard with 3
color channels (RGB).
Step 2: We use a nested loop to iterate over each element of the array and assign it a
color based on its position. If the sum of the row and column indices is even, the square
is white; otherwise, it is black.
Step 3: We resize the array to make each square larger for better visualization using
cv2.resize.
Step 4: Finally, we display the chessboard using OpenCV's imshow function.
Create a chess table using numpy and opencv.
Create a chess table using numpy and opencv.
import numpy as np
import cv2
# Import the cv2_imshow function
from google.colab.patches import cv2_imshow
cv2.waitKey(0)
cv2.destroyAllWindows()
Design letter-B using numpy and open-cv.
img[:,:]=255
img[50:250, 50:100]=0
img[50:90, 100:160]=0
img[210:250, 100:160]=0
img[130:170, 100:160]=0
img[70:140, 160:210]=0
img[160:230, 160:210]=0
Design letter-B using numpy and open-cv.
cv2.imwrite('result_7.png', img)
# Import the cv2_imshow function
from google.colab.patches import
cv2_imshow
import cv2
img = cv2.imread('img/polar_bear.jpg', 1)
cv2.imwrite('result_5.png', img)