1

So, I have already detected all the edges of an object but the problem is that I can't find the two points of each edge, that is, the starting point and the ending point with its coordinates.

Actually I am trying to find the measurements of an object but I am stuck at this problem.The image is regarding the ROI of the image.

import cv2
import numpy as np
from matplotlib import pyplot as plt 

#Read Image of the Object
img = cv2.imread("C:\\Users\\Desktop\\Project\\captured.jpg")
cv2.imshow('Original Image', img)
cv2.waitKey(0)



#Convert Image To GrayScale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray', gray)
cv2.waitKey(0)


#Binary Thresholding
ret, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('Binary Image', thresh)
cv2.waitKey(0)

#Crop Image
cropped = thresh[150:640, 150:500]
cv2.imshow('Cropped Image', cropped)
cv2.waitKey(0)

#Edge Detection
edges = cv2.Canny(cropped, 100, 200)
cv2.imshow('Edges', edges)
cv2.waitKey(0)

#find contours
ctrs, hier = cv2.findContours(cropped, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

#Sort Contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0] + cv2.boundingRect(ctr)[1] * cropped.shape[1])


#ROI
for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = cropped[y:y + h, x:x + w]
    # show ROI
    # cv2.imshow('segment no:'+str(i),roi)
    cv2.rectangle(cropped , (x, y), (x + w, y + h), (150, 0, 255), 2)
cv2.imshow('marked areas', cropped)
cv2.waitKey(0)

Original Image

These are 5 points and the five edges that I need with coordinates so I can calculate the distance between them for the measurement.

Harris Corner Output.

3
  • Can you attach sample input image along with expected output ? Commented Feb 8, 2019 at 9:47
  • I have edited the question with the changes. Can you look at it now? Commented Feb 8, 2019 at 9:55
  • @JimitVaghela hii i have calculated the edge length of sides of image. Now, i want to know how to know represent it like imgur.com/a/e2CVGxA any idea how to achieve this Commented Jun 26, 2021 at 7:47

1 Answer 1

1

Try using Harris Corner Detection instead:

import cv2
import numpy as np

def find_centroids(dst):
    ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
    dst = np.uint8(dst)

    # find centroids
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
    # define the criteria to stop and refine the corners
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 
                0.001)
    corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5), 
              (-1,-1),criteria)
    return corners

image = cv2.imread("corner.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)

dst = cv2.cornerHarris(gray, 2, 3, 0.04)

dst = cv2.dilate(dst, None)

# Threshold for an optimal value, it may vary depending on the image.
# image[dst > 0.01*dst.max()] = [0, 0, 255]

# Get coordinates
corners= find_centroids(dst)
# To draw the corners
for corner in corners:
    image[int(corner[1]), int(corner[0])] = [0, 0, 255]
cv2.imshow('dst', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

Detected Corners

You might need to fine tune the parameters for cornerHarris.

Sign up to request clarification or add additional context in comments.

14 Comments

Thank you for responding. Can I actually store these point values to a variable so I can use them to calculate the distance between them?
Yes you can. Just edited the answer. They are just close estimates to the found corners since it find multiple corners at the same corner
Also, the suggestion you have provided is giving me an error: IndexError: boolean index did not match indexed array along dimension 0; dimension is 330 but corresponding boolean dimension is 480
I have tried the code and it works. What error are you getting?
At line 32 : NameError: name 'corners' is not defined I am having this error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.