import numpy as np
import cv2
blank_image = np.zeros((40,40,3), np.uint8)
blank_image.fill(255)
#cv2.imshow('i', blank_image)
#cv2.waitKey(0)
im = cv2.imread('img.png')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[4]
cnts = cv2.drawContours(im,[cnt],0,(255,0,0), -1)
cv2.imshow('i', im)
cv2.waitKey(0)
for a in cnt:
print(a) #this contour is a 3D numpy array
Source image:
I am using this code to:
1. create a white canvas of 40x40 pixels
2. found the contours of number (in this case 5) using Opencv function findContours
.
What I want to do is to copy this shape (please, not the bounding box or rectangle, the blue shape) into the canvas.
After some research I learned that an opencv image is just a numpy array. This array, theoretically, should be translated in the new image (my white canvas..) and then reconstruct the shape using the values inside the array. I am right ?
Someone know how to do that ? Creating a bounding box / rectangle around the numbers would, in some cases, result inaccurate. Please, don't give that as solution. I already did this process in atleast 3-4 different ways and the results are not satisfactory enough.
So, the desired output would be something like this..
Thanks.