0% found this document useful (0 votes)
17 views18 pages

OPENCV Lab1

The document provides an introduction to OpenCV, detailing its capabilities for computer vision and image processing, along with exercises for practical application. It includes instructions for creating a chessboard, designing the letter 'B', rotating images, and manipulating pixel values in images using Python and OpenCV. The document serves as a lab session guide for students to learn and apply OpenCV techniques.

Uploaded by

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

OPENCV Lab1

The document provides an introduction to OpenCV, detailing its capabilities for computer vision and image processing, along with exercises for practical application. It includes instructions for creating a chessboard, designing the letter 'B', rotating images, and manipulating pixel values in images using Python and OpenCV. The document serves as a lab session guide for students to learn and apply OpenCV techniques.

Uploaded by

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

COMPUTER VISION AND IMAGE PROCESSING

LAB SESSION
INTRODUCTION TO OPENCV

COURSE INSTRUCTOR: Dr. Roopa B S


The OpenCV library
• Open Computer Vision Library: a collection of open source
algorithms for computer vision and image processing
• Originally developed by Intel, then funded and supported by Willow
Garage. Currently a non-profit foundation (www.opencv.org)
• Code is released under the BSD license – free for both academicals
and commercial use
• Main language: C/C++, with optimized routines (multi-thread,
SIMD, ..)
• Current version: 4.9.0
• Freely downloadable from:
• http://sourceforge.net/projects/opencvlibrary
• Available for Windows, Linux, iOS. Currently supporting also Android
Exercise 1 – “invert grey”
• Compute the “negative” of a grayscale image
• Given a grayscale image (range of each pixel between [0 255]),
substitute each pixel having intensity I with the value: 255-I
Exercise 2 – “invert RGB”
• Same as before, but in this case we want to compute the negative of a
color image.
• The image has 3 channels, representing the 3 RGB values
• The intensity of each channel ranges between [0 255]
• For each image pixel, we need to substitute the (B,G,R) triplet with its
«inverse» (255-B, 255-G, 255-R)
Exercise 3 – Image difference
• Build a new project which performs the following:
• loads 2 images (Image 1, I1 and Image 2, I2)
• computes the pixel-wise difference between the two images:
• computes an output image where each pixel of coordinates (x,y) contains the
absolute difference of the corresponding pixels on I1 and I2:
• Out(x,y) = abs(I1(x,y) – I2(x,y))
• Displays on a window the output image
Create a chess table using numpy and 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

# Step 1: Initialize the chessboard size


board_size = 8
square_size = 50 # Size of each square in pixels
board = np.zeros((board_size, board_size, 3),
dtype=np.uint8)
Create a chess table using numpy and opencv.

# Step 2: Color the squares


for i in range(board_size):
for j in range(board_size):
if (i + j) % 2 == 0:
board[i, j] = [255, 255, 255] # White square
else:
board[i, j] = [0, 0, 0] # Black square

# Step 3: Resize the image to a larger size for better


visualization
board_large = cv2.resize(board, (board_size *
square_size, board_size * square_size),
interpolation=cv2.INTER_NEAREST)
Create a chess table using numpy and opencv.

# Step 4: Display the image using OpenCV


cv2.imshow("Chessboard", board_large)
# Display the image using cv2_imshow
cv2_imshow(board_large)

cv2.waitKey(0)
cv2.destroyAllWindows()
Design letter-B using numpy and open-cv.

· Initialize the array: img_arr is a flat array of


values from 0 to 89999. np.reshape converts it
into a 300x300 matrix.
· Set the image to white: img[:,:]=255 sets all
elements to 255 (white).
· Create black regions: Specific slices of the
array are set to 0 (black) to form the desired
pattern.
· Save and display: The image is saved as
result_7.png, and cv2_imshow (specific to
Google Colab) is used to display the image.
Design letter-B using numpy and open-cv.
import cv2
import numpy as np
from google.colab.patches import cv2_imshow

img_arr = np.arange(0, 90000, 1, np.uint8)


img = np.reshape(img_arr, (300, 300))
height, width = img.shape

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

# Display the image using


cv2_imshow
cv2_imshow(img)
Rotate Image
· Read the image: The image is read in grayscale mode
(cv2.IMREAD_GRAYSCALE), ensuring a single channel (0-255 values).
· Check for successful image loading: Adding a check to ensure the image
was loaded successfully to avoid errors.
· Initialize and reshape the array: This step is not strictly necessary since we
will overwrite all the values in the next step, but it shows the intent clearly.
· Invert the image: The nested loops iterate through each pixel of the image
and assign it to the corresponding position in the inverted image.
· Save the inverted image: The cv2.imwrite function saves the resulting
image.
· cv2.flip(img, -1) flips the image both vertically and horizontally, which is
equivalent to reversing both rows and columns as in the original code.
· This approach is more concise and leverages OpenCV's optimized functions
for better performance.
Rotate Image
Rotate Image
import cv2
import numpy as np

# Step 1: Read the image in grayscale mode


img = cv2.imread('img/3.jpg', 0)

# Check if the image was successfully loaded


if img is None:
print("Error loading img/3.jpg")
else:
# Step 2: Invert the image by flipping it vertically and horizontally
rev_img = cv2.flip(img, -1)

# Step 3: Save the inverted image to a file


cv2.imwrite('result_3.png', rev_img)
print("Inverted image saved as result_3.png")
Corner Line

· Read the image: The image is read in color


mode (BGR).
· Get dimensions: We retrieve the height,
width, and number of color channels (ignored)
of the image.
· Pixel manipulation:
The loops iterate over a range of rows (j) and
columns (k), up to a maximum of 400 and 85
respectively.
The condition if 300 - j + k < width ensures that
the calculated index does not exceed the
image boundaries.
For each valid index, the pixel value is set to
black ([0, 0, 0]).
· Save the modified image: The modified
image is saved to a file named result_5.png.
Corner Line

import cv2

img = cv2.imread('img/polar_bear.jpg', 1)

height, width, _ = img.shape

for j in range(400): #height


for k in range(85): #width
if not 300-j+k<0:
img[j][300-j+k]= 0

cv2.imwrite('result_5.png', img)

You might also like