Package: a-cv2-shape-finder
Detecting shapes with OpenCV, and getting all the important information in a DataFrame$pipinstalla-cv2-shape-finderfroma_cv2_shape_finderimportget_shapes_using_ADAPTIVE_THRESH_GAUSSIAN_C,get_shapes_using_ADAPTIVE_THRESH_MEAN_C,get_shapes_using_THRESH_OTSUimportcv2importpandasaspdfroma_cv2_imshow_threadimportadd_imshow_thread_to_cv2froma_cv_imwrite_imread_plusimportadd_imwrite_plus_imread_plus_to_cv2importnumpyasnpadd_imwrite_plus_imread_plus_to_cv2()add_imshow_thread_to_cv2()image2=cv2.imread_plus(r"http://clipart-library.com/img/2000719.png")#method1 (best results)df,bw_pic=get_shapes_using_ADAPTIVE_THRESH_GAUSSIAN_C(im=image2.copy(),method=cv2.CHAIN_APPROX_SIMPLE,approxPolyDPvar=0.02,constant_subtracted=2,block_size=11,return_bw_pic=True,)#method2 (good results)df,bw_pic=get_shapes_using_ADAPTIVE_THRESH_MEAN_C(im=image2.copy(),method=cv2.CHAIN_APPROX_SIMPLE,approxPolyDPvar=0.04,constant_subtracted=2,block_size=11,return_bw_pic=True,)#method3 (not always good results)df,bw_pic=get_shapes_using_THRESH_OTSU(im=image2.copy(),method=cv2.CHAIN_APPROX_SIMPLE,approxPolyDPvar=0.01,kernel=(1,1),start_thresh=50,end_thresh=255,return_bw_pic=True,)aa_arcLengthaa_isContourConvexaa_center_xaa_center_yaa_areaaa_convexHullaa_len_convexHullaa_len_cntsaa_shapeaa_rotated_rectangleaa_minEnclosingCircle_centeraa_minEnclosingCircle_radiusaa_fitEllipseaa_fitLineaa_h0aa_h1aa_h2aa_h3aa_bound_start_xaa_bound_start_yaa_bound_end_xaa_bound_end_yaa_bound_widthaa_bound_height01994.000000True286212242952.0[[[573,0]],[[573,424]],[[0,424]],[[0,0]]]44rectangle[[0,0],[573,0],[573,424],[0,424]](286,212)356<NA>((573,212),(0,212))-1-11-100574425574425117.656854True51139823.0[[[509,397]],[[510,396]],[[513,396]],[[514,397]],[[514,400]],[[513,401]],[[510,401]],[[509,400]]]88circle[[509,396],[514,396],[514,401],[509,401]](511,398)2((511.5,398.5),(5.830951690673828,5.830951690673828),0.0)((573,398),(0,398))2-1-1050939651540266267.213203False402395128.0[[[396,388]],[[406,387]],[[409,399]],[[405,401]],[[397,401]]]57oval[[395,388],[407,386],[409,400],[397,401]](402,394)8((401.52850341796875,394.2469482421875),(13.484291076660156,29.70796012878418),84.56861877441406)((573,1267),(0,-1646))31-103963874104021415388.183766False543396172.5[[[538,386]],[[551,389]],[[550,401]],[[543,401]],[[535,400]]]510oval[[535,400],[538,386],[552,389],[549,403]](542,394)9((540.5974731445312,393.8035888671875),(14.401185989379883,27.078874588012695),93.7881088256836)((573,561),(0,-2567))42-105353865524021716442.384776True530393122.5[[[530,386]],[[532,386]],[[535,389]],[[535,397]],[[533,400]],[[528,401]],[[525,398]],[[525,392]],[[527,388]]]99oval[[525,401],[525,386],[535,386],[535,401]](530,393)7((530.2174682617188,393.71160888671875),(11.07934856414795,15.688089370727539),10.645519256591797)((573,-138),(0,6796))63505253865364021116512.242641False5303937.5[[[531,395]],[[530,396]],[[529,395]],[[529,393]],[[530,391]],[[531,391]]]67oval[[529,391],[531,391],[531,396],[529,396]](530,393)2((530.2325439453125,393.48016357421875),(2.533412456512451,5.182924270629883),12.179041862487793)((573,-82),(0,6120))-1-1-1452939153239736653.941125False520393102.0[[[521,386]],[[525,389]],[[525,396]],[[522,400]],[[519,401]],[[515,398]],[[514,394]],[[516,389]]]812oval[[511,395],[519,384],[528,391],[520,402]](520,393)7((519.7500610351562,393.70819091796875),(10.821428298950195,15.00129222869873),12.044055938720703)((573,263),(0,1652))74-1051438652640212167124.911687False475395252.5[[[471,386]],[[477,386]],[[488,389]],[[487,398]],[[480,410]],[[475,409]],[[466,398]],[[467,392]]]815oval[[463,405],[469,383],[489,389],[483,411]](477,397)13((477.4767761230469,396.48944091796875),(20.678686141967773,25.679677963256836),21.347288131713867)((573,781),(0,-1472))96804663864894112325810.242641True4823937.5[[[484,394]],[[483,395]],[[482,395]],[[481,394]],[[481,393]],[[482,392]],[[484,392]]]77circle[[481,392],[484,392],[484,395],[481,395]](482,393)1((482.6419372558594,393.3580627441406),(3.061401844024658,3.7628066539764404),45.0)((573,302),(0,876))-1-1-1748139248539644981.112698False459399212.5[[[465,386]],[[466,405]],[[463,410]],[[458,410]],[[454,403]],[[455,391]],[[458,387]]]711oval[[453,386],[465,386],[466,409],[454,410]](461,398)12((460.94775390625,397.08856201171875),(13.2537202835083,25.697481155395508),2.9813687801361084)((573,2400),(0,-7608))1171004543864674111325#Let's draw the results from the second picture#There is nothing better than Pandas to process data.image=image2.copy()forname,groupindf.groupby("aa_h3"):ifname==0:continuefabb=(np.random.randint(50,250),np.random.randint(50,250),np.random.randint(50,250),)forkey,itemingroup.loc[(group.aa_area>200)&(group.aa_shape.isin(['rectangle','triangle','circle','pentagon','hexagon']))].iterrows():image=cv2.drawContours(image,item.aa_convexHull,-1,color=fabb,thickness=5,lineType=cv2.LINE_AA)image=cv2.rectangle(image,(item.aa_bound_start_x,item.aa_bound_start_y),(item.aa_bound_end_x,item.aa_bound_end_y),(0,0,0),3,)image=cv2.rectangle(image,(item.aa_bound_start_x,item.aa_bound_start_y),(item.aa_bound_end_x,item.aa_bound_end_y),fabb,2,)image=cv2.putText(image,f'{str(item.aa_shape)}-{name}',(item.aa_bound_start_x,item.aa_bound_start_y),cv2.FONT_HERSHEY_SIMPLEX,0.4,(0,0,0),2,cv2.LINE_AA,)image=cv2.putText(image,f'{str(item.aa_shape)}-{name}',(item.aa_bound_start_x,item.aa_bound_start_y),cv2.FONT_HERSHEY_SIMPLEX,0.4,fabb,1,cv2.LINE_AA,)cv2.imshow_thread([image,bw_pic])