Answered step by step
Verified Expert Solution
Link Copied!

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 cv2
import numpy as np
import argparse
def angle(pt1, pt2, pt0):
# pt1 ve pt0 arasndaki kenarn x'i (pt1x - pt0x)
dx1= pt1[0]- pt0[0]
# pt1 ve pt0 arasndaki kenarn y'si (pt1y - pt0y)
dy1= pt1[1]- pt0[1]
# pt2 ve pt0 arasndaki kenarn x'i (pt2x - pt0x)
dx2= pt2[0]- pt0[0]
# pt2 ve pt0 arasndaki kenarn y'si (pt2y - pt0y)
dy2= pt2[1]- pt0[1]
return (dx1* dx2+ dy1* dy2)/ np.sqrt((dx1* dx1+ dy1* dy1)*(dx2* dx2+ dy2* dy2))
def main(input_image):
input_path ="C:/Users/burak/Desktop/Shape Recognition & Detection/data/input/"
output_path ="C:/Users/burak/Desktop/Shape Recognition & Detection/data/output"
image = cv2.imread(input_path + input_image)
if image is None:
print("Resim alamad ya da yklenemedi.")
return
# gri
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite(output_path +"\\output_gray.png", gray)
# blurlama
blurred = cv2.GaussianBlur(gray,(5,5),0)
cv2.imwrite(output_path +"\\output_blurred.png", blurred)
# kenar alglama
edged = cv2.Canny(blurred,100,200)
cv2.imwrite(output_path +"\\output_edged.png", edged)
# kontor bulma
contours, _= cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# ekil alglama
for contour in contours:
shape =""
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour,0.02* peri, True)
if len(approx)==3:
shape = "triangle"
elif len(approx)==4:
max_cosine =0
for j in range(2,5):
cosine = abs(angle(approx[j %4][0], approx[j -2][0], approx[j -1][0]))
max_cosine = max(max_cosine, cosine)
if max_cosine 0.3:
# kare
edges =[]
for i in range(4):
pt1= approx[i][0]
pt2= approx[(i +1)%4][0]
edge_length = np.sqrt((pt1[0]- pt2[0])**2+(pt1[1]- pt2[1])**2)
edges.append(edge_length)
# kenarlar eit mi
if max(edges)- min(edges)10:
shape = "square"
else:
shape = "rectangle"
else:
edges =[]
for i in range(4):
pt1= approx[i][0]
pt2= approx[(i +1)%4][0]
edge_length = np.sqrt((pt1[0]- pt2[0])**2+(pt1[1]- pt2[1])**2)
edges.append(edge_length)
edges = sorted(edges)
if abs(edges[0]- edges[1])50 and abs(edges[2]- edges[3])50:
shape = "parallelogram"
elif abs(edges[0]- edges[1])50 or abs(edges[2]- edges[3])50:
shape = "trapezoid"
elif len(approx)>4:
shape = "circle"
cv2.drawContours(image,[contour],-1,(0,255,0),2)
M = cv2.moments(contour)
cX = int(M["m10"]/ M["m00"])
cY = int(M["m01"]/ M["m00"])
cv2.putText(image, shape, (cX -50, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255,0,0),1)
# output
cv2.imwrite(output_path +"\\output_shapes.png", image)
cv2.imshow("Shapes", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__=="__main__":
parser = argparse.ArgumentParser(description="Shape detection in an image.")
parser.add_argument("input_image", type=str, help="Input'un dosya yolu.")
args = parser.parse_args()
main(args.input_image)
#python sekilalgilama.py resimad
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

Students also viewed these Databases questions