0

I need to know how do i draw lines between four points? all points are detected by contours. There are four red points.as shown below these points are detected using cv2.findContours. After I need to draw lines on each of points like a rectangle. Can someone please help me to solve this ? Also I used convexhull to do this but i couldn't sort it out.

enter image description here

here is the code that i used

ret, frame = cap.read()
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) 
RedMask = cv2.inRange(hsv,(0,100,100),(10,255,255))
contours1, _ = cv2.findContours(RedMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 
for cnt1 in contours1: 
   #area = cv2.contourArea(cnt1) 
   #Draw it 
   #cv2.drawContours(frame,[cnt],0,(255,0,0),2)

   hull = cv2.convexHull(cnt1)
   cv2.drawContours(frame,hull,3,(255,0,0),2)
Nim4eng
  • 79
  • 1
  • 7

2 Answers2

0

As it says here, you can use it in the following way.

cv2.drawContours(img, contours, index, color, thickness)

If you pass index=-1, it will draw all the contours. If you pass index=3, it will draw the fourth contour in the array of contours. In this, if your convexHull array consist of only one contour, you can pass index=-1

Froyo
  • 17,947
  • 8
  • 45
  • 73
0

Try cv2.polylines:

#first we need to change the shape of the array
(count,_,_) = hull.shape
hull.ravel()
hull.shape = (count,2)

#some blank img to drow on
blank = np.zeros_like(frame)

#drawing
cv2.polylines(blank,np.int32([hull]),True,255)

Note that we need to convert data type of the array (based on this answer)

Community
  • 1
  • 1
BPiek
  • 174
  • 11