Kita melanjutkan pembahasan computer vision dengan mencari kontur image menggunakan OpenCV.
Source code mencari kontur segi empat dan lingkaran adalah seperti pada berikut ini
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import cv2 import numpy as np OPENCV_MAJOR_VERSION = int(cv2.__version__.split( '.' )[0]) img = cv2.pyrDown(cv2.imread( "../images/hammer.jpg" )) asli = cv2.pyrDown(cv2.imread( "../images/hammer.jpg" )) ret, thresh = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY) if OPENCV_MAJOR_VERSION >= 4: # OpenCV 4 or a later version is being used. contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) else : # OpenCV 3 or an earlier version is being used. # cv2.findContours has an extra return value. # The extra return value is the thresholded image, which (in # OpenCV 3.1 or an earlier version) may have been modified, but # we can ignore it. _, contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in contours: # find bounding box coordinates x, y, w, h = cv2.boundingRect(c) cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2) # find minimum area rect = cv2.minAreaRect(c) # calculate coordinates of the minimum area rectangle box = cv2.boxPoints(rect) # normalize coordinates to integers box = np.int0(box) # draw contours cv2.drawContours(img, [box], 0, (0, 0, 255), 3) # calculate center and radius of minimum enclosing circle (x, y), radius = cv2.minEnclosingCircle(c) # cast to integers center = (int(x), int(y)) radius = int(radius) # draw the circle img = cv2.circle(img, center, radius, (0, 255, 0), 2) cv2.imshow( "asli" , asli) cv2.drawContours(img, contours, -1, (255, 0, 0), 1) cv2.imshow( "contours" , img) cv2.waitKey() cv2.destroyAllWindows() |
Source code ini bersumber dari https://github.com/PacktPublishing/Learning-OpenCV-4-Computer-Vision-with-Python-Third-Edition/blob/master/chapter03/contours_2.py .
Script ini akan menghasilkan tampilan seperti berikut ini
Kunjungi www.proweb.co.id untuk menambah wawasan anda.
Mencari Kontur Gambar menggunakan OpenCV