-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-painter.py
More file actions
27 lines (23 loc) · 834 Bytes
/
Copy pathsingle-painter.py
File metadata and controls
27 lines (23 loc) · 834 Bytes
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
import cv2
import numpy as np
import os
from matplotlib import pyplot as plt
fig = cv2.imread('test.png')
fig = cv2.cvtColor(fig, cv2.COLOR_BGR2RGB)
lower_mask = np.array([45, 25, 153])
upper_mask = np.array([60, 62, 205])
hsv = cv2.cvtColor(fig, cv2.COLOR_RGB2HSV)
mask = cv2.inRange(hsv, lower_mask, upper_mask)
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
areas = []
for c in range(len(contours)):
areas.append(cv2.contourArea(contours[c]))
max_id = areas.index(max(areas))
x, y, w, h = cv2.boundingRect(contours[max_id])
fig = cv2.drawContours(fig, contours, -1, (255, 255, 255), 4)
fig = cv2.rectangle(fig, (x, y), (x + w, y + h), (255, 0, 0), 4)
plt.title('rectangle', style='italic')
plt.imshow(fig)
plt.axis('off')
plt.savefig('single-4.pdf')
plt.show()