Monday 25 November 2019

Polygon-Shape-Detection-counting

               Polygon-Shape-Detection-Counting


Input Image: 




Output Images:









                                                                Source Code :
Main code 1 :

import cv2
import numpy as np
import os

im = cv2.imread('input1.jpg')


imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 220, 255, 0)
edges = cv2.Canny(thresh,150,180,apertureSize = 3)

ret, labels = cv2.connectedComponents(~thresh)

x=0

[m,n]=np.shape(thresh)


c=1
cwd = os.getcwd()
msk=np.zeros(np.shape(thresh))
while c
    msk=np.zeros(np.shape(thresh))
    msk[(labels==c)]=[255]
    msk=np.array(msk,dtype=np.uint8)
    msk_img=str(c)+'.jpg'
    filename = os.path.join(cwd, msk_img)
    cv2.imwrite(filename,msk)
    c=c+1

print("Total Polygon Objects ", c)



Code 2 :

Main code 2:

import cv2
import numpy as np
import os
im = cv2.imread('input1.jpg')


imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 220, 255, 0)
edges = cv2.Canny(thresh,150,180,apertureSize = 3)

ret, labels = cv2.connectedComponents(~thresh)

x=0

[m,n]=np.shape(thresh)


c=1
cwd = os.getcwd()
msk=np.zeros(np.shape(thresh))
while c    msk=np.zeros(np.shape(thresh))
    msk[(labels==c)]=[255]
    msk=np.array(msk,dtype=np.uint8)
    edges = cv2.Canny(msk,150,180,apertureSize = 3)
    image2, contours, _ = cv2.findContours(edges    , cv2.CHAIN_APPROX_NONE, cv2.CHAIN_APPROX_SIMPLE)
    cnt=contours[-1]
    msk_img=str(c)+'.jpg'
    filename = os.path.join(cwd, msk_img)

    ## Objects side detection using controus and defects
    ## Objets sides are mentioned red color marker
    import obj_sid
    # obj_sid.sides(cnt,im)
    obj_sid.equl_side(cnt,im,msk,filename)
    cv2.namedWindow('image contours', cv2.WINDOW_GUI_NORMAL)
    cv2.drawContours(im, contours[-1],-1, (0, 255, 0), 3)
    cv2.imshow('image contours',im)
    cv2.waitKey(0)
    # cv2.imwrite(filename,msk)
    c=c+1
print("Total Polygon Objects ", c)



                                                                 Function code:

import cv2
import numpy as np
import os
im = cv2.imread('input1.jpg')


imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(imgray, 220, 255, 0)
edges = cv2.Canny(thresh,150,180,apertureSize = 3)

ret, labels = cv2.connectedComponents(~thresh)

x=0

[m,n]=np.shape(thresh)


c=1
cwd = os.getcwd()
msk=np.zeros(np.shape(thresh))
while c    msk=np.zeros(np.shape(thresh))
    msk[(labels==c)]=[255]
    msk=np.array(msk,dtype=np.uint8)
    edges = cv2.Canny(msk,150,180,apertureSize = 3)
    image2, contours, _ = cv2.findContours(edges    , cv2.CHAIN_APPROX_NONE,       cv2.CHAIN_APPROX_SIMPLE)
    cnt=contours[-1]
    msk_img=str(c)+'.jpg'
    filename = os.path.join(cwd, msk_img)

    ## Objects side detection using controus and defects
    ## Objets sides are mentioned red color marker
    import obj_sid
    # obj_sid.sides(cnt,im)
    obj_sid.equl_side(cnt,im,msk,filename)
    cv2.namedWindow('image contours', cv2.WINDOW_GUI_NORMAL)
    cv2.drawContours(im, contours[-1],-1, (0, 255, 0), 3)
    cv2.imshow('image contours',im)
    cv2.waitKey(0)

    # cv2.imwrite(filename,msk)
    c=c+1
print("Total Polygon Objects ", c)



                                                   Installation Pacakges
1.Python
2.Opencv