import cv2
import numpy as np
import os
Load the image
image_path = 'olamiz 03.jpg'
img = cv2.imread(image_path)
Since we don't have a deep learning segmentation model loaded directly,
let's use a standard GrabCut algorithm or simple thresholding/masking if appropriate,
but GrabCut is much better for foreground extraction like a person and a bicycle.
Let's define an initial bounding box around the person and the bicycle.
Image dimensions:
h, w, c = img.shape
The person and bicycle span almost the entire height and width, but let's give a safe box:
top around 10%, bottom 100%, left 10%, right 95%
mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (int(w * 0.05), int(h * 0.05), int(w * 0.92), int(h * 0.95))
Apply GrabCut
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
Modify the mask so that all definite background and probable background pixels are 0,
and all definite foreground and probable foreground pixels are 1.
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
Let's refine the mask a bit with morphological operations to smooth edges
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
mask2 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernel)
mask2 = cv2.morphologyEx(mask2, cv2.MORPH_OPEN, kernel)
Create 4-channel PNG
rgba = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
rgba[:, :, 3] = mask2 * 255
Save the result
output_path = 'personaj_va_velosiped.png'
cv2.imwrite(output_path, rgba)
print(f"File saved to {output_path}")
import cv2
import numpy as np
import os
Load the image
image_path = 'olamiz 03.jpg'
img = cv2.imread(image_path)
Since we don't have a deep learning segmentation model loaded directly,
let's use a standard GrabCut algorithm or simple thresholding/masking if appropriate,
but GrabCut is much better for foreground extraction like a person and a bicycle.
Let's define an initial bounding box around the person and the bicycle.
Image dimensions:
h, w, c = img.shape
The person and bicycle span almost the entire height and width, but let's give a safe box:
top around 10%, bottom 100%, left 10%, right 95%
mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (int(w * 0.05), int(h * 0.05), int(w * 0.92), int(h * 0.95))
Apply GrabCut
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)
Modify the mask so that all definite background and probable background pixels are 0,
and all definite foreground and probable foreground pixels are 1.
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
Let's refine the mask a bit with morphological operations to smooth edges
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
mask2 = cv2.morphologyEx(mask2, cv2.MORPH_CLOSE, kernel)
mask2 = cv2.morphologyEx(mask2, cv2.MORPH_OPEN, kernel)
Create 4-channel PNG
rgba = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
rgba[:, :, 3] = mask2 * 255
Save the result
output_path = 'personaj_va_velosiped.png'
cv2.imwrite(output_path, rgba)
print(f"File saved to {output_path}")