Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Simp!a nhianto - Page 1 of 2 An example of an input image: The output image with the corresponding text: An example of an input

Simp!a nhianto-
Page 1 of 2
An example of an input image:
The output image with the corresponding text: An example of an input image:
The output image with the corresponding text:
Circle, Magenta, 1
Circle, Blue, 1
Circle, Cyan, 1
Circle, Black, 2
Circle, Yellow, 2
Triangle, Green, 1
Triangle, Red 1
Triangle, Blue, 2
Triangle, Cyan, 1
Rectangle, Yellow, 2
Rectangle, Cyan, 2
Rectangle, Black, 1
Rectangle, Red, 1
Rectangle, Magenta, 1
Square, Green, 1Please implement a program to recognize simple objects and basic shapes inan input
image as shown in the figures below. Consider the following regulations:
The input: A colored image.
The output:
The same input image with a label above each object to identify it from the
list below
House and its type (Class1or Class 2), Tree and its type (Class1or
Class 2), Car and Truck
Print the name, the color and the number of basic shapes in the image
(Circle, triangle, rectangle or square)on the command prompt as a list
(Don't display them on the image).
Each student must complete the assignment individually.
Only C,C++,or Python programming languages can be used, in conjunction
with OpenCV.
Simple objects: import cv2
import numpy as np
# Load the image
image_path = r'C:\Users\Hp\OneDrive\Desktop\input1- Copy.png' # Replace with the actual path to your image
image = cv2.imread(image_path)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply binary thresholding
_, thresh = cv2.threshold(gray,240,255, cv2.THRESH_BINARY_INV)
# Find contours
contours, _= cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Function to detect shape
def detect_shape(contour):
shape = "unidentified"
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour,0.04* peri, True)
if len(approx)==3:
shape = "triangle"
elif len(approx)==4:
x, y, w, h = cv2.boundingRect(approx)
aspect_ratio = w / float(h)
shape = "square" if 0.95= aspect_ratio =1.05 else "rectangle"
else:
shape = "circle"
return shape
# Function to detect color
def detect_color(image, contour):
mask = np.zeros(image.shape[:2], dtype="uint8")
cv2.drawContours(mask,[contour],-1,255,-1)
mean = cv2.mean(image, mask=mask)[:3]
color = "unknown"
if mean[2]>150 and mean[1]100 and mean[0]100:
color = "red"
elif mean[2]100 and mean[1]>150 and mean[0]100:
color = "green"
elif mean[2]100 and mean[1]100 and mean[0]>150:
color = "blue"
elif mean[2]>200 and mean[1]>200 and mean[0]100:
color = "yellow"
elif mean[2]>150 and mean[1]100 and mean[0]>150:
color = "magenta"
elif mean[2]100 and mean[1]>150 and mean[0]>150:
color = "cyan"
elif mean[2]>200 and mean[1]>200 and mean[0]>200:
color = "white"
elif mean[2]50 and mean[1]50 and mean[0]50:
color = "black"
return color
# Dictionary to store the number of each shape
shapes_count ={"circle": 0, "triangle": 0, "square": 0, "rectangle": 0}
# Iterate through contours and classify shapes
detected_shapes =[]
for contour in contours:
shape = detect_shape(contour)
color = detect_color(image, contour)
# Increase the shape count
if shape in shapes_count:
shapes_count[shape]+=1
# Find the center of the contour for labeling
M = cv2.moments(contour)
if M["m00"]!=0:
cX = int(M["m10"]/ M["m00"])
cY = int(M["m01"]/ M["m00"])
else:
cX, cY =0,0
detected_shapes.append((shape, color, contour, cX, cY))
# Helper function to check if contours are close
def are_contours_close(cnt1, cnt2, max_dist=50):
for pt1 in cnt1:
for pt2 in cnt2:
dist = np.linalg.norm(pt1- pt2)
if dist max_dist:
return True
return False
def is_equilateral(contour):
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour,0.04* peri, True)
if len(approx)==3:
# Calculate the lengths of each side
side1= np.linalg.norm(approx[0]- approx[1])
side2= np.linalg.norm(approx[1]- approx[2])
side3= np.linalg.norm(approx[2]- approx[0])
# Check if all sides are nearly equal
return abs(side1- side2)0.05* side1 and abs(side2- side3)0.05* side2 and abs(side3- side1)0.05* side3
return False
def is_inside(contour1, contour2):
x, y, w, h = cv2.boundingRect(contour2)
return cv2.pointPolygonTest(contour1,(x + w /2, y + h /2), False)>
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions

Question

=+Is this metric really applicable to what I want to accomplish?

Answered: 1 week ago

Question

=+How does this metric connect to my objectives?

Answered: 1 week ago