# import cv2
from cv2 import cv2 as cv
import numpy as np
import Tracker_HSV as tr
from PIL import Image
import os
# cv2.namedWindow('mask', cv2.WINDOW_NORMAL)
cv.namedWindow('mask',cv.WINDOW_NORMAL)
cv.resizeWindow('mask',(500,500))
filename= 'flower.jpg'
cwd = os.getcwd()
name_file=os.path.splitext(filename)[0]
path_save_temp=os.path.join(cwd,'Data')
path_save_folder=os.path.join(path_save_temp,f'{name_file}_blur_data')
if not os.path.exists(path_save_folder):
os.makedirs(path_save_folder)
img = cv.imread("D:/....../test_Bokeh/Bokeh_Demo/flower.jpg")
blur = cv.GaussianBlur(img,(5,5),0)
img_hsv=cv.cvtColor(img,cv.COLOR_BGR2HSV)
file_save_blur= os.path.join(path_save_folder,'blur.png')
im_blur = cv.GaussianBlur(img,(81,81),0)
cv.imwrite(file_save_blur,im_blur)
xs,ys,w,h = cv.selectROI('mask',img)
crop_img=crop_img_true=crop_img_contour=img[ys:ys+h, xs:xs+w]
if not crop_img_true.shape[0]> 1:
crop_img_true=img
x,y,z,a,b,c=(tr.tracker(crop_img_true))
crop_img_true=cv.cvtColor(crop_img_true,cv.COLOR_BGR2HSV)
file_save_mask_inrange= os.path.join(path_save_folder,'mask inRange.png')
mask_inRange=cv.inRange(crop_img_true,(x,y,z),(a,b,c))
cv.imwrite(file_save_mask_inrange,mask_inRange)
_, threshold = cv.threshold(mask_inRange, 250, 255, cv.THRESH_BINARY)
Gauss_threshold =cv.adaptiveThreshold(threshold,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY_INV,101,10)
blank_space_black= np.zeros((crop_img_true.shape[0],crop_img_true.shape[1]),np.uint8)
blank_space_black[:]=(0)
_,contours,_ = cv.findContours(Gauss_threshold, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
maxi=cv.contourArea(contours[0])
c=[]
for cnt in contours:
if cv.contourArea(cnt)>=maxi:
maxi=cv.contourArea(cnt)
## print(cv2.contourArea(cnt))
c= cnt
file_save_contour= os.path.join(path_save_folder,'Contour.png')
cv.drawContours(crop_img_contour, c, -1, (0, 255, 0), 5)
cv.imwrite(file_save_contour,crop_img_contour)
file_save_poly= os.path.join(path_save_folder,'mask fill poly.png')
mask_poly=cv.fillConvexPoly(blank_space_black,c,(255,255,255))
cv.imwrite(file_save_poly,mask_poly)
crop_img_true=cv.cvtColor(crop_img_true,cv.COLOR_HSV2BGR)
file_save_mask_bitwise= os.path.join(path_save_folder,'mask bitwise and.png')
mask_bitwise_and = cv.bitwise_and(crop_img_true,crop_img_true,mask=mask_poly)
cv.imwrite(file_save_mask_bitwise,mask_bitwise_and)
im2= Image.open(file_save_mask_bitwise)
im2=im2.convert('RGBA')
datas=im2.getdata()
newdata=[]
for data in datas:
if data[0]== 0 and data[1]== 0 and data[2]== 0:
newdata.append((255,255,255,0))
else:
newdata.append(data)
file_save_transparent= os.path.join(path_save_folder,'transparent.png')
im2.putdata(newdata)
im2.save(file_save_transparent)
im_blur= Image.open(file_save_blur)
file_save_final= os.path.join(path_save_folder,'final.png')
im_blur.paste(im2,(xs,ys),im2)
im_blur.save(file_save_final)
im_final= Image.open(file_save_final)
im_final.show('Final Result')
cv.imshow('GaussianBlur',blur)
cv.waitKey(0)
cv.destroyAllWindows()