Ask Your Question

Revision history [back]

storing centers of contours

Hi everyone, I´m kind of new to programming and openCV so I apologise if I´m asking something obvious to you :)

I´m writing a script for object detection as a part of my thesis.

image description

I use contours to detect the holes on this object which works fine and then I find the center of each contour which I print. However, I need to store these values so that I can work with them further on. So is there a way to store the value of each center of contour separately as a variable? Further on I will be measuring the distance between all the centers to determine whether all the holes are in a correct place.

image description

Any help would be highly appreciated :)

storing centers of contours

Hi everyone, I´m kind of new to programming and openCV so I apologise if I´m asking something obvious to you :)

I´m writing a script for object detection as a part of my thesis.

image description

I use contours to detect the holes on this object which works fine and then I find the center of each contour which I print. However, I need to store these values so that I can work with them further on. So is there a way to store the value of each center of contour separately as a variable? Further on I will be measuring the distance between all the centers to determine whether all the holes are in a correct place.

image descriptionimage description

Any help would be highly appreciated :)

storing centers of contours

Hi everyone, I´m kind of new to programming and openCV so I apologise if I´m asking something obvious to you :)

I´m writing a script for object detection as a part of my thesis.

image description

I use contours to detect the holes on this object which works fine and then I find the center of each contour which I print. However, I need to store these values so that I can work with them further on. So is there a way to store the value of each center of contour separately as a variable? Further on I will be measuring the distance between all the centers to determine whether all the holes are in a correct place.

import numpy as np
import cv2

image = imread("test_picture.jpeg")

image = cv2.resize(image, (640,480))

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

blurred = cv2.GaussianBlur(gray, (5,5), 0)

thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)

edged = cv2.Canny(blurred, 50, 80)

contours = cv2.findContours(thresh, cv2.RETR_LIST,CV2_CHAIN_APPROX_SIMPLE)


for c in contours:

    M=cv2.moments(c)
    cX=int(M["m10"] / M["m00"])
    cY=int(M["m01"] / M["m00"])

    area=cv2.ContourArea(c)


    print([cX,cY])
    print(area)

    cv2.drawContours(image, [c], -1 (255,255,255), 2)


cv2.imshow("Image",image)

cv2.waitKey(0)
cv2.destroyAllWindows()

image description

Any help would be highly appreciated :)