I previously posted here
And I've seen this post
Despite the great information provided by the community, I have been unable to smoothly trace an image using cv2.findContours(). While in my previous post I asked about generating splines to smoothly trace curves, my focus now is to get a smooth trace of an object, regardless of how many points are generated for the contour. I consistently get results with jagged edges:
My desired output would be something similar to this, which I've manually created in Adobe Illustrator:
I have experimented extensively with blurring and thresholding, and have been unable to get a smooth outline. I am running openCV version 3.3.0.
import numpy as np
import cv2
import math
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
print(cv2.__version__)
im = cv2.imread('img.jpg')
# orient the image properly
# grab the dimensions of the image and calculate the center
# of the image
(h, w) = im.shape[:2]
center = (w / 2, h / 2)
# rotate the image 180 degrees
M = cv2.getRotationMatrix2D(center, 180, 1.0)
rotated = cv2.warpAffine(im, M, (w, h))
# flip the image across
flippedColor = cv2.flip(rotated, 1) #for testing
imgray = cv2.cvtColor(rotated, cv2.COLOR_BGR2GRAY)
flipped = cv2.flip(imgray, 1)
(thresh, binRed) = cv2.threshold(flipped, 180, 255, cv2.THRESH_BINARY)
_, Rcontours, hier_r = cv2.findContours(binRed,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
r_areas = [cv2.contourArea(c) for c in Rcontours]
max_rarea = np.argmax(r_areas)
CntExternalMask = np.ones(binRed.shape[:2], dtype="uint8") * 255
contour= Rcontours[max_rarea]
cv2.drawContours(flippedColor,[contour],-1,(255,0,0),1)