Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 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)
6 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
You are on page 1/ 9

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 matplotlib.pyplot as plt
from google.colab import files

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


uploaded = files.upload()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = cv2.imread(filename)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

print("Image shape:", image.shape) # (rows, cols, channels)


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

# Step 2: Show image with grid


plt.imshow(image)
plt.title("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 matplotlib.pyplot as plt
from google.colab import files

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


uploaded = files.upload()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = cv2.imread(filename)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

print("Image shape:", image.shape) # (rows, cols, channels)


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

# Step 2: Show image with grid


plt.imshow(image)
plt.title("Original Image with Grid")
plt.grid(True) # show grid lines
plt.show()

cv2.line(image, (100,200),(300,200),(300,0,0),3) # Corrected BGR color value


plt.imshow(image)
plt.title('image with line drawn')
plt.show()
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 matplotlib.pyplot as plt
from google.colab import files

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


uploaded = files.upload()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = cv2.imread(filename)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

print("Image shape:", image.shape) # (rows, cols, channels)


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

# Step 2: Show image with grid


plt.imshow(image)
plt.title("Original Image with Grid")
plt.grid(True) # show grid lines
plt.show()

cv2.rectangle(image, (40,46),(95,93), (0,255, 220), 1)


plt.imshow(image)
plt.title('image with box drawn')
plt.show()
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 matplotlib.pyplot as plt
from google.colab import files

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


uploaded = files.upload()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = cv2.imread(filename)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

print("Image shape:", image.shape) # (rows, cols, channels)


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

# Step 2: Show image with grid


plt.imshow(image)
plt.title("Original Image with Grid")
plt.grid(True) # show grid lines
plt.show()

cv2.circle(image, (154, 152), 20, (0, 255, 0), 2)


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

# Step 1: Upload image


uploaded = files.upload()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = cv2.imread(filename)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert BGR to RGB

print("Image shape:", image.shape) # (rows, cols, channels)


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

# Step 2: Show original image with grid


plt.imshow(image)
plt.title("Original Image with Grid")
plt.grid(True) # Show grid lines
plt.show()

# Step 3: Write your name on the image


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

# Step 5: Show the image with name


plt.imshow(image)
plt.title("Image with Name")
# plt.grid(True) # Show grid lines
plt.show()
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 matplotlib.pyplot as plt
from google.colab import files

# Step 1: Upload image


uploaded = files.upload()
filename = next(iter(uploaded)) # ✅ get uploaded file name
image = cv2.imread(filename)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert BGR to RGB

print("Image shape:", image.shape) # (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


plt.imshow(negative_image)
plt.title("Negative Image")
plt.grid(True) # Show grid lines
plt.show()
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 matplotlib.pyplot 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 = np.zeros((img_height, img_width, 3), dtype=np.uint8)
cv2.line(img, (0, line_y), (img_width, line_y), (0, 255, 0), 2)
cv2.circle(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)
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 255), 2)
cropped = img[y1:y2, x1:x2]
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title("Original Image with Rectangle Around Object")
plt.axis("off")
plt.show()
plt.imshow(cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB))
plt.title("Cropped Rectangle (Object)")
plt.axis("off")
plt.show()
Output:

Car Detection:
ZIAUDDIN UNIVERSITY
Faculty Of Engineering Science & Technology
(ZUFESTM)
Department of Software Engineering
Code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from IPython.display 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 = np.zeros((img_height, img_width, 3), dtype=np.uint8)
# Draw the line (green)
cv2.line(img, (0, line_y), (img_width, line_y), (0, 255, 0), 2)
# Draw the moving circle (blue)
cv2.circle(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
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis("off")
clear_output(wait=True)
plt.show()
time.sleep(0.5)
Output:

You might also like