-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimgToContourDEMO.py
31 lines (24 loc) · 1023 Bytes
/
imgToContourDEMO.py
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
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def imageToPoints (image):
ret, discrImage = cv.threshold (image, 127, 255, 0)
height, width = discrImage.shape
# Add a line of white values to the top of the image.
# This ensures that the binary outer contour works.
# It is removed later before writing to point cloud.
# Order is [y,x] i.e. [row, column]
discrImage[0,:] = 255 # 1st row, and the whole x range
img, vectImage, heirarchy = cv.findContours(discrImage, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
return vectImage
if __name__ == '__main__':
image = cv.imread('/home/pi/Desktop/ScannerDev/TestImage.png', cv.IMREAD_GRAYSCALE)
contImage = imageToPoints(image)
newImage = cv.drawContours(np.zeros(image.shape), contImage, -1, (255,255,255), 1)
cv.imshow ('original', image)
cv.imshow ('Contoured', newImage)
while (1):
c = cv.waitKey(5)
if 'q' == chr(c & 255):
break
cv.destroyAllWindows()