-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrabCut.py
79 lines (57 loc) · 2.09 KB
/
grabCut.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import numpy as np
import os
from tqdm import tqdm
def get_rect(image_path):
image = Image.open(image_path)
image_orig = np.array(image)
if len(image_orig.shape) == 2:
gray_image = image_orig # Grayscale image already
else:
gray_image = cv2.cvtColor(image_orig, cv2.COLOR_BGR2GRAY)
contours, _ = cv2.findContours(np.uint8(gray_image), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
best_rect = None
best_rect_area = 0
for contour in contours:
rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box_area = rect[1][0] * rect[1][1]
if box_area > best_rect_area:
best_rect = box
best_rect_area = box_area
min_x = int(min(best_rect[:,0]))
min_y = int(min(best_rect[:,1]))
max_x = int(max(best_rect[:,0]))
max_y = int(max(best_rect[:,1]))
w = max_x - min_x
h = max_y - min_y
x = min_x
y = min_y
x = max(0, x) # Ensure x is not negative
y = max(0, y) # Ensure y is not negative
rect = (x , y, w, h)
return rect
def grabCut(folder_path, output_folder):
image_files = os.listdir(folder_path)
for img in tqdm(image_files, desc="Processing images", unit="image"):
image_path = folder_path + img
rect = get_rect(image_path)
# 2. implement GrabCut
# grabcut needs an image opened in cv2
image2 = cv2.imread(image_path)
mask = np.zeros(image2.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
try:
# 6 is the iteration,
cv2.grabCut(image2, mask, rect, bgdModel, fgdModel, 6, cv2.GC_INIT_WITH_RECT)
mask = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
result = image2 * mask[:, :, np.newaxis]
except cv2.error as e:
# Handle the error here
print(f"An error occurred during GrabCut: {e}")
result = image2
cv2.imwrite(output_folder + img, result)
pass