finding corner point of each object

asked 2018-09-13 05:54:58 -0500

tejas gravatar image

image description

I have found contour of each object and now i want to find corner point of each object. For example triangle will have 3 corner point ,square will have 4 corner point etc.

this is the code in python

# -- coding: utf-8 -- """ Created on Mon May 21 17:16:01 2018

@author: TEJAS KUTHE """

import cv2 import numpy as np import imutils import time

from shapedetector import ShapeDetector image = cv2.imread('E:/AT/TVS_EPL_IPS/Master/new.jpeg',0) MIN_THRESH=5000

blurred = cv2.GaussianBlur(image, (5, 5), 0) ret, thresh = cv2.threshold(blurred, 155, 255, 0)

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = ShapeDetector()

i=0 for c in cnts:

if cv2.contourArea(c) > MIN_THRESH:
    M = cv2.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    shape = sd.detect(c)

    cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
    i+=1
    j = 0

    for i2 in c:
        if j<100:
            for x,y in i2:
                print(f"{x},{y}")


        j+=1
    print(f"({cX},{cY}) object {i}")
cv2.circle(image, (cX, cY), 7, (0, 255, 255), -1)

cv2.circle(image,(cX,cY), 63, (0,0,255), 0)
str1 ="Object"+ str(i)
cv2.putText(image, shape, (cX - 20, cY - 20),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 2)

image = cv2.resize(image,(700,700)) cv2.imshow("image",image)

cv2.imwrite("newImage.jpeg",image) img2 = cv2.resize(image,(500,500))

cv2.waitKey(0) cv2.destroyAllWindows() #

edit retag flag offensive close merge delete

Comments

@tejas Check approxPolyDp opencv fuction.

ak1 gravatar imageak1 ( 2018-09-17 01:55:05 -0500 )edit