0% found this document useful (0 votes)
15 views9 pages

Numerical & Symbolic Computing Lab 03

The document outlines a series of lab tasks for a software engineering course at Ziauddin University, focusing on image processing using Python and OpenCV. Students are required to perform tasks such as loading images, drawing shapes, writing text, and creating negative images. Additionally, there is a car detection simulation included as part of the coursework.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Numerical & Symbolic Computing Lab 03

The document outlines a series of lab tasks for a software engineering course at Ziauddin University, focusing on image processing using Python and OpenCV. Students are required to perform tasks such as loading images, drawing shapes, writing text, and creating negative images. Additionally, there is a car detection simulation included as part of the coursework.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ZIAUDDIN UNIVERSITY

Faculty Of Engineering Science & Technology


(ZUFESTM)
Department of Software Engineering
Course Title: ASSESSMENT ACTIVITY: Semester:
Numerical & Symbolic Lab -3 FA 2025 (Semester-VI)
Computing
Deadline: OBE Target: Weight of Marks:
CLO-3 and PLO-5

Student Name: Ali Haider Teacher: Sir Abdul Basit Score:


Student ID: 4-38/2023/041
ZIAUDDIN UNIVERSITY
Faculty Of Engineering Science & Technology
(ZUFESTM)
Department of Software Engineering
Lab Tasks
1. Load your own image and print its shape.
Code:
import cv2
import [Link] as plt
from [Link] import files

# Step 1: Load Image (read as RGB instead of BGR)


uploaded = [Link]()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = [Link](filename)
image = [Link](image, cv2.COLOR_BGR2RGB)

print("Image shape:", [Link]) # (rows, cols, channels)


print("Sample pixel [0,0]:", image[0,0]) # first pixel RGB values

# Step 2: Show image with grid


[Link](image)
[Link]("Original Image with Grid")
Output:
ZIAUDDIN UNIVERSITY
Faculty Of Engineering Science & Technology
(ZUFESTM)
Department of Software Engineering
2. Draw a diagonal line across the image.
Code:
import cv2
import [Link] as plt
from [Link] import files

# Step 1: Load Image (read as RGB instead of BGR)


uploaded = [Link]()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = [Link](filename)
image = [Link](image, cv2.COLOR_BGR2RGB)

print("Image shape:", [Link]) # (rows, cols, channels)


print("Sample pixel [0,0]:", image[0,0]) # first pixel RGB values

# Step 2: Show image with grid


[Link](image)
[Link]("Original Image with Grid")
[Link](True) # show grid lines
[Link]()

[Link](image, (100,200),(300,200),(300,0,0),3) # Corrected BGR color value


[Link](image)
[Link]('image with line drawn')
[Link]()
Output:
ZIAUDDIN UNIVERSITY
Faculty Of Engineering Science & Technology
(ZUFESTM)
Department of Software Engineering
3. Draw a rectangle around a selected object (e.g., face, table).
Code:
import cv2
import [Link] as plt
from [Link] import files

# Step 1: Load Image (read as RGB instead of BGR)


uploaded = [Link]()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = [Link](filename)
image = [Link](image, cv2.COLOR_BGR2RGB)

print("Image shape:", [Link]) # (rows, cols, channels)


print("Sample pixel [0,0]:", image[0,0]) # first pixel RGB values

# Step 2: Show image with grid


[Link](image)
[Link]("Original Image with Grid")
[Link](True) # show grid lines
[Link]()

[Link](image, (40,46),(95,93), (0,255, 220), 1)


[Link](image)
[Link]('image with box drawn')
[Link]()
Output:
ZIAUDDIN UNIVERSITY
Faculty Of Engineering Science & Technology
(ZUFESTM)
Department of Software Engineering
4. Draw a circle at the center of the image.
Code:
import cv2
import [Link] as plt
from [Link] import files

# Step 1: Load Image (read as RGB instead of BGR)


uploaded = [Link]()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = [Link](filename)
image = [Link](image, cv2.COLOR_BGR2RGB)

print("Image shape:", [Link]) # (rows, cols, channels)


print("Sample pixel [0,0]:", image[0,0]) # first pixel RGB values

# Step 2: Show image with grid


[Link](image)
[Link]("Original Image with Grid")
[Link](True) # show grid lines
[Link]()

[Link](image, (154, 152), 20, (0, 255, 0), 2)


[Link]("Circular Image with Grid")
[Link](image)
[Link](True) # show grid lines
[Link]()
Output
ZIAUDDIN UNIVERSITY
Faculty Of Engineering Science & Technology
(ZUFESTM)
Department of Software Engineering
5. Write your name on the image.
Code:
import cv2
import [Link] as plt
from [Link] import files

# Step 1: Upload image


uploaded = [Link]()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = [Link](filename)
image = [Link](image, cv2.COLOR_BGR2RGB) # Convert BGR to RGB

print("Image shape:", [Link]) # (rows, cols, channels)


print("Sample pixel [0,0]:", image[0,0]) # First pixel RGB values

# Step 2: Show original image with grid


[Link](image)
[Link]("Original Image with Grid")
[Link](True) # Show grid lines
[Link]()

# Step 3: Write your name on the image


[Link](image, "Ali Haider", (50, 110), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,
0, 0), 6, cv2.LINE_AA)

# Step 5: Show the image with name


[Link](image)
[Link]("Image with Name")
# [Link](True) # Show grid lines
[Link]()
Output:
ZIAUDDIN UNIVERSITY
Faculty Of Engineering Science & Technology
(ZUFESTM)
Department of Software Engineering
6. Bonus: Convert the image to negative (255 - pixel).
Code:
import cv2
import [Link] as plt
from [Link] import files

# Step 1: Upload image


uploaded = [Link]()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = [Link](filename)
image = [Link](image, cv2.COLOR_BGR2RGB) # Convert BGR to RGB

print("Image shape:", [Link]) # (rows, cols, channels)


print("Sample pixel [0,0]:", image[0,0]) # First pixel RGB values

# Step 2: Convert the image to its negative


negative_image = 255 - image # Subtract each pixel value from 255

# Step 3: Show the negative image


[Link](negative_image)
[Link]("Negative Image")
[Link](True) # Show grid lines
[Link]()
Output:

7. Bonus: crop the specific object on image.


ZIAUDDIN UNIVERSITY
Faculty Of Engineering Science & Technology
(ZUFESTM)
Department of Software Engineering
Code:
import cv2
import numpy as np
import [Link] as plt
img_height, img_width = 400, 400
line_y = 200 # line position
cx, cy = 150, 180 # center of the circle
radius = 30 # circle radius
img = [Link]((img_height, img_width, 3), dtype=np.uint8)
[Link](img, (0, line_y), (img_width, line_y), (0, 255, 0), 2)
[Link](img, (cx, cy), radius, (255, 0, 0), -1)
x1 = max(cx - radius, 0)
y1 = max(cy - radius, 0)
x2 = min(cx + radius, img_width)
y2 = min(cy + radius, img_height)
[Link](img, (x1, y1), (x2, y2), (0, 255, 255), 2)
cropped = img[y1:y2, x1:x2]
[Link]([Link](img, cv2.COLOR_BGR2RGB))
[Link]("Original Image with Rectangle Around Object")
[Link]("off")
[Link]()
[Link]([Link](cropped, cv2.COLOR_BGR2RGB))
[Link]("Cropped Rectangle (Object)")
[Link]("off")
[Link]()
Output:

Car Detection:
ZIAUDDIN UNIVERSITY
Faculty Of Engineering Science & Technology
(ZUFESTM)
Department of Software Engineering
Code:
import cv2
import numpy as np
import [Link] as plt
from [Link] import clear_output
import time
# Blank black image
img_height, img_width = 400, 400
# Car detected
line_y = 200 # y-axis line
# Car not detected
# line_y = 300 # y-axis line
for x in range(0, 400, 20): # move circle step by step
# Circle center
cx, cy = x, 180
radius = 20
# Create a new blank image each frame
img = [Link]((img_height, img_width, 3), dtype=np.uint8)
# Draw the line (green)
[Link](img, (0, line_y), (img_width, line_y), (0, 255, 0), 2)
# Draw the moving circle (blue)
[Link](img, (cx, cy), radius, (255, 0, 0), -1)
# Detection check (circle touches or crosses the line)
if cy + radius >= line_y:
print("Car Detected!")
else:
print("No Car")
# Show the frame
[Link]([Link](img, cv2.COLOR_BGR2RGB))
[Link]("off")
clear_output(wait=True)
[Link]()
[Link](0.5)
Output:

You might also like