Answered step by step
Verified Expert Solution
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 of
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,
Circle, Blue,
Circle, Cyan,
Circle, Black,
Circle, Yellow,
Triangle, Green,
Triangle, Red
Triangle, Blue,
Triangle, Cyan,
Rectangle, Yellow,
Rectangle, Cyan,
Rectangle, Black,
Rectangle, Red,
Rectangle, Magenta,
Square, Green, Please implement a program recognize simple objects and basic shapes input
image shown the figures below. Consider the following regulations:
The input: A colored image.
The output:
The same input image with a label above each object identify from the
list below
House and its type Class Tree and its type
Class Car and Truck
Print the name, the color and the number basic shapes the image
triangle, rectangle square the command prompt a list
display them the image
Each student must complete the assignment individually.
Only Python programming languages can used, conjunction
with OpenCV.
Simple objects: import cv
import numpy as np
# Load the image
imagepath rC:UsersHpOneDriveDesktopinput Copy.png # Replace with the actual path to your image
image cvimreadimagepath
# Convert to grayscale
gray cvcvtColorimage cvCOLORBGRGRAY
# Apply binary thresholding
thresh cvthresholdgray cvTHRESHBINARYINV
# Find contours
contours, cvfindContoursthresh cvRETREXTERNAL, cvCHAINAPPROXSIMPLE
# Function to detect shape
def detectshapecontour:
shape "unidentified"
peri cvarcLengthcontour True
approx cvapproxPolyDPcontour peri, True
if lenapprox:
shape "triangle"
elif lenapprox:
x y w h cvboundingRectapprox
aspectratio w floath
shape "square" if aspectratio else "rectangle"
else:
shape "circle"
return shape
# Function to detect color
def detectcolorimage contour:
mask npzerosimageshape: dtype"uint
cvdrawContoursmaskcontour
mean cvmeanimage maskmask:
color "unknown"
if mean and mean and mean:
color "red"
elif mean and mean and mean:
color "green"
elif mean and mean and mean:
color "blue"
elif mean and mean and mean:
color "yellow"
elif mean and mean and mean:
color "magenta"
elif mean and mean and mean:
color "cyan"
elif mean and mean and mean:
color "white"
elif mean and mean and mean:
color "black"
return color
# Dictionary to store the number of each shape
shapescount circle: "triangle": "square": "rectangle":
# Iterate through contours and classify shapes
detectedshapes
for contour in contours:
shape detectshapecontour
color detectcolorimage contour
# Increase the shape count
if shape in shapescount:
shapescountshape
# Find the center of the contour for labeling
M cvmomentscontour
if Mm:
cX intMm Mm
cY intMm Mm
else:
cX cY
detectedshapes.appendshape color, contour, cX cY
# Helper function to check if contours are close
def arecontoursclosecnt cnt maxdist:
for pt in cnt:
for pt in cnt:
dist nplinalg.normpt pt
if dist maxdist:
return True
return False
def isequilateralcontour:
peri cvarcLengthcontour True
approx cvapproxPolyDPcontour peri, True
if lenapprox:
# Calculate the lengths of each side
side nplinalg.normapprox approx
side nplinalg.normapprox approx
side nplinalg.normapprox approx
# Check if all sides are nearly equal
return absside side side and absside side side and absside side side
return False
def isinsidecontour contour:
x y w h cvboundingRectcontour
return cvpointPolygonTestcontourx w y h False
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started