Ask Your Question

Revision history [back]

The code above wasn't buiding with Python3. Just in case if this is useful, see the code below with minor corrections.

import cv2 as cv import numpy as np

im = cv.imread("plank.jpg") gray = cv.cvtColor(im, cv.COLOR_BGR2GRAY) gray = cv.GaussianBlur(gray, (5, 5), 0) _, bin = cv.threshold(gray,120,255,1) # inverted threshold (light obj on dark bg) bin = cv.dilate(bin, None) # fill some holes bin = cv.dilate(bin, None) bin = cv.erode(bin, None) # dilate made our shape larger, revert that bin = cv.erode(bin, None) contours, hierarchy = cv.findContours(bin, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)

rc = cv.minAreaRect(contours[0]) box = cv.boxPoints(rc) for p in box: pt = (p[0],p[1]) print(pt) cv.circle(im,pt,5,(200,0,0),2) cv.imshow("plank", im) cv.waitKey()

click to hide/show revision 2
No.2 Revision

The code above wasn't buiding with Python3. Just in case if this is useful, see the code below with minor corrections.

import cv2 as cv
import numpy as np

np

im = cv.imread("plank.jpg") gray = cv.cvtColor(im, cv.COLOR_BGR2GRAY) gray = cv.GaussianBlur(gray, (5, 5), 0) _, bin = cv.threshold(gray,120,255,1) # inverted threshold (light obj on dark bg) bin = cv.dilate(bin, None) # fill some holes bin = cv.dilate(bin, None) bin = cv.erode(bin, None) # dilate made our shape larger, revert that bin = cv.erode(bin, None) contours, hierarchy = cv.findContours(bin, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)

cv.CHAIN_APPROX_SIMPLE)

rc = cv.minAreaRect(contours[0]) box = cv.boxPoints(rc) for p in box: pt = (p[0],p[1]) print(pt) cv.circle(im,pt,5,(200,0,0),2) cv.imshow("plank", im) cv.waitKey()

cv.waitKey()