import cv2 import numpy #read an image I_origin = cv2.imread("C:/Users/student/Downloads/flag_green.jpg",1) #converting into the hsv space I_hsv = cv2.cvtColor(I_origin,cv2.COLOR_RGB2HSV) h,s,v = cv2.split(I_hsv) #creating mask H1 = h >= 30 H2 = h <= 80 mask = numpy.bitwise_and(H1,H2) inverse_mask = numpy.logical_not(mask) mask = numpy.asarray(mask,dtype=numpy.uint8) inverse_mask = numpy.asarray(inverse_mask,dtype=numpy.uint8) #creating both of sharped and unsharped versions value = 11 sigma_value = 1.5 sharped = cv2.GaussianBlur(I_origin, (value, value),sigma_value) unsharped = cv2.addWeighted(I_origin, 3.5, sharped, -2.5, 0) #masking both of object and background regions correctly area_object = numpy.zeros(I_origin.shape,dtype=numpy.uint8) background_object = numpy.zeros(I_origin.shape,dtype=numpy.uint8) for i in range(0,3): area_object[:,:,i] = unsharped[:,:,i]*mask background_object[:,:,i] = sharped[:,:,i]*inverse_mask result = area_object + background_object cv2.imshow("result",result) cv2.waitKey()