Hi i want to do something like in this video. with python API in the V-rep system, Right now I have my robot working and only need to process the image that I want to draw.
To do this I need to have a vector of (x,y) coordinates of the path for the pen. So i'm trying with OpenCV findContours function. here is my code:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('img.jpg', 3)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges,contours,hierarchy = cv2.findContours(gray, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
xList = [x[0][0][0] for x in contours]
yList = [y[0][0][1] for y in contours]
#plot image
plt.subplot(121)
plt.imshow(img,cmap = 'gray')
plt.title('Original Image')
#plot contour
plt.subplot(122)
plt.plot(xList,yList)
plt.title('Edge Image')
plt.show()
here is my example image:
and the result if I use scatter plot to plot each point individually that shows me the contour of the image.
Ok, maybe its not the best example finding contours with OpenCV its my first.
The if I plot the points using plot function, to see the path thath I want to follow I get this:
So wath I want is to find the way that sort all the point on a consecutive order path to draw the image contour.
The issue here is that the point are unsorted so the don have the relation with its previous and the the next point they are just a cloud of points. So I need to get it sorted to construct the path.
Any idea on how to do this?
threshold
to binary image,findContours
on binary-imaget, thendrawContours
is ok. – Kinght 金