Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am posting the same question for the second time. please don't give copy and paste answers. this code is for shape detection. it doesn't
I am posting the same question for the second time. please don't give copy and paste answers. this code is for shape detection. it doesn't work because there are unnecessary lines and dots in the image I uploaded. can you improve this code and try it on the image I uploaded? import cv import numpy as np import argparse def anglept pt pt: # pt ve pt arasndaki kenarn xi ptx ptx dx pt pt # pt ve pt arasndaki kenarn y'si pty pty dy pt pt # pt ve pt arasndaki kenarn xi ptx ptx dx pt pt # pt ve pt arasndaki kenarn y'si pty pty dy pt pt return dx dx dy dy npsqrtdx dx dy dydx dx dy dy def maininputimage: inputpath C:UsersburakDesktopShape Recognition & Detectiondatainput outputpath C:UsersburakDesktopShape Recognition & Detectiondataoutput image cvimreadinputpath inputimage if image is None: printResim alamad ya da yklenemedi return # gri gray cvcvtColorimage cvCOLORBGRGRAY cvimwriteoutputpath outputgray.png gray # blurlama blurred cvGaussianBlurgray cvimwriteoutputpath outputblurred.png blurred # kenar alglama edged cvCannyblurred cvimwriteoutputpath outputedged.png edged # kontor bulma contours, cvfindContoursedged cvRETREXTERNAL, cvCHAINAPPROXSIMPLE # ekil alglama for contour in contours: shape peri cvarcLengthcontour True approx cvapproxPolyDPcontour peri, True if lenapprox: shape "triangle" elif lenapprox: maxcosine for j in range: cosine absangleapproxj approxj approxj maxcosine maxmaxcosine, cosine if maxcosine : # kare edges for i in range: pt approxi pt approxi edgelength npsqrtpt ptpt pt edges.appendedgelength # kenarlar eit mi if maxedges minedges: shape "square" else: shape "rectangle" else: edges for i in range: pt approxi pt approxi edgelength npsqrtpt ptpt pt edges.appendedgelength edges sortededges if absedges edges and absedges edges: shape "parallelogram" elif absedges edges or absedges edges: shape "trapezoid" elif lenapprox: shape "circle" cvdrawContoursimagecontour M cvmomentscontour cX intMm Mm cY intMm Mm cvputTextimage shape, cX cY cvFONTHERSHEYSIMPLEX, # output cvimwriteoutputpath outputshapes.png image cvimshowShapes image cvwaitKey cvdestroyAllWindows if namemain: parser argparse.ArgumentParserdescription"Shape detection in an image." parser.addargumentinputimage", typestr help"Input'un dosya yolu." args parser.parseargs mainargsinputimage #python sekilalgilama.py resimad
I am posting the same question for the second time. please don't give copy and paste answers. this code is for shape detection. it doesn't work because there are unnecessary lines and dots in the image I uploaded. can you improve this code and try it on the image I uploaded?
import cv
import numpy as np
import argparse
def anglept pt pt:
# pt ve pt arasndaki kenarn xi ptx ptx
dx pt pt
# pt ve pt arasndaki kenarn y'si pty pty
dy pt pt
# pt ve pt arasndaki kenarn xi ptx ptx
dx pt pt
# pt ve pt arasndaki kenarn y'si pty pty
dy pt pt
return dx dx dy dy npsqrtdx dx dy dydx dx dy dy
def maininputimage:
inputpath C:UsersburakDesktopShape Recognition & Detectiondatainput
outputpath C:UsersburakDesktopShape Recognition & Detectiondataoutput
image cvimreadinputpath inputimage
if image is None:
printResim alamad ya da yklenemedi
return
# gri
gray cvcvtColorimage cvCOLORBGRGRAY
cvimwriteoutputpath outputgray.png gray
# blurlama
blurred cvGaussianBlurgray
cvimwriteoutputpath outputblurred.png blurred
# kenar alglama
edged cvCannyblurred
cvimwriteoutputpath outputedged.png edged
# kontor bulma
contours, cvfindContoursedged cvRETREXTERNAL, cvCHAINAPPROXSIMPLE
# ekil alglama
for contour in contours:
shape
peri cvarcLengthcontour True
approx cvapproxPolyDPcontour peri, True
if lenapprox:
shape "triangle"
elif lenapprox:
maxcosine
for j in range:
cosine absangleapproxj approxj approxj
maxcosine maxmaxcosine, cosine
if maxcosine :
# kare
edges
for i in range:
pt approxi
pt approxi
edgelength npsqrtpt ptpt pt
edges.appendedgelength
# kenarlar eit mi
if maxedges minedges:
shape "square"
else:
shape "rectangle"
else:
edges
for i in range:
pt approxi
pt approxi
edgelength npsqrtpt ptpt pt
edges.appendedgelength
edges sortededges
if absedges edges and absedges edges:
shape "parallelogram"
elif absedges edges or absedges edges:
shape "trapezoid"
elif lenapprox:
shape "circle"
cvdrawContoursimagecontour
M cvmomentscontour
cX intMm Mm
cY intMm Mm
cvputTextimage shape, cX cY cvFONTHERSHEYSIMPLEX,
# output
cvimwriteoutputpath outputshapes.png image
cvimshowShapes image
cvwaitKey
cvdestroyAllWindows
if namemain:
parser argparse.ArgumentParserdescription"Shape detection in an image."
parser.addargumentinputimage", typestr help"Input'un dosya yolu."
args parser.parseargs
mainargsinputimage
#python sekilalgilama.py resimad
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