async def fun_mirror(event): path = "dck" reply = await event.get_reply_message() lol = await borg.download_media(reply.media, path) file_name = "mirror.jpg" hehe = path + "/" + file_name img = cv2.imread(lol) H, W = img.shape[:2] c1 = vcam(H=H, W=W) plane = meshGen(H, W) plane.Z += (20 * np.exp(-0.5 * ((plane.X * 1.0 / plane.W) / 0.1)**2) / (0.1 * np.sqrt(2 * np.pi))) pts3d = plane.getPlane() pts2d = c1.project(pts3d) map_x, map_y = c1.getMaps(pts2d) output = cv2.remap(img, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=0) output = cv2.flip(output, 1) out1 = cv2.resize(output, (700, 350)) cv2.imwrite(file_name, out1) await borg.send_file(event.chat_id, file_name) for files in (hehe, lol): if files and os.path.exists(files): os.remove(files) await event.delete()
async def fun(event): if event.fwd_from: return path = "omk" reply = await event.get_reply_message() lol = await bot.download_media(reply.media, path) await eor(event, "Warping In Progress...") file_name = "warped.jpg" hehe = path + "/" + file_name img = cv2.imread(lol) H, W = img.shape[:2] fps = 30 c1 = vcam(H=H, W=W) plane = meshGen(H, W) plane.Z += 20 * np.exp(-0.5 * ( (plane.Y * 1.0 / plane.H) / 0.1)**2) / (0.1 * np.sqrt(2 * np.pi)) pts3d = plane.getPlane() pts2d = c1.project(pts3d) map_x, map_y = c1.getMaps(pts2d) output = cv2.remap(img, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=0) output = cv2.flip(output, 1) out1 = cv2.resize(output, (700, 350)) cv2.imwrite(file_name, out1) await bot.send_file(event.chat_id, file_name) for files in (hehe, lol): if files and os.path.exists(files): os.remove(files) hoi = await event.delete()
def OpenCVCode(imgRGB, depth32f, frameCount): H, W = imgRGB.shape[:2] # Creating the virtual camera object c1 = vcam(H=H, W=W) # Creating the surface object plane = meshGen(H, W) mode = saveMode # We generate a mirror where for each 3D point, its Z coordinate is defined as Z = F(X,Y) if mode == 0: plane.Z += 20 * np.exp(-0.5 * ( (plane.X * 1.0 / plane.W) / 0.1)**2) / (0.1 * np.sqrt(2 * np.pi)) elif mode == 1: plane.Z += 20 * np.exp(-0.5 * ( (plane.Y * 1.0 / plane.H) / 0.1)**2) / (0.1 * np.sqrt(2 * np.pi)) elif mode == 2: plane.Z -= 10 * np.exp(-0.5 * ( (plane.X * 1.0 / plane.W) / 0.1)**2) / (0.1 * np.sqrt(2 * np.pi)) elif mode == 3: plane.Z -= 10 * np.exp(-0.5 * ( (plane.Y * 1.0 / plane.W) / 0.1)**2) / (0.1 * np.sqrt(2 * np.pi)) elif mode == 4: plane.Z += 20 * np.sin(2 * np.pi * ( (plane.X - plane.W / 4.0) / plane.W)) + 20 * np.sin(2 * np.pi * ( (plane.Y - plane.H / 4.0) / plane.H)) elif mode == 5: plane.Z -= 20 * np.sin(2 * np.pi * ( (plane.X - plane.W / 4.0) / plane.W)) - 20 * np.sin(2 * np.pi * ( (plane.Y - plane.H / 4.0) / plane.H)) elif mode == 6: plane.Z += 100 * np.sqrt((plane.X * 1.0 / plane.W)**2 + (plane.Y * 1.0 / plane.H)**2) elif mode == 7: plane.Z -= 100 * np.sqrt((plane.X * 1.0 / plane.W)**2 + (plane.Y * 1.0 / plane.H)**2) else: print("Wrong mode selected") exit(-1) # Extracting the generated 3D plane pts3d = plane.getPlane() # Projecting (Capturing) the plane in the virtual camera pts2d = c1.project(pts3d) # Deriving mapping functions for mesh based warping. map_x, map_y = c1.getMaps(pts2d) output = cv.remap(imgRGB, map_x, map_y, interpolation=cv.INTER_LINEAR, borderMode=4) output = cv.flip(output, 1) out1 = np.hstack((imgRGB, output)) out1 = cv.resize(out1, (700, 350)) cv.imshow(titleWindow, out1) return output, None
def wrap_noise(image: np.ndarray): """扭曲噪声""" img = image h, w = img.shape[:2] camera = vcam(H=h, W=w) plane = meshGen(h, w) plane.Z = np.sin(8 * np.pi * (plane.X / plane.W)) # plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2) plane.Z += 20 * np.exp(-0.5 * ( (plane.X * 1.0 / plane.W) / 0.1)**2) / (0.1 * np.sqrt(2 * np.pi)) pts3d = plane.getPlane() pts2d = camera.project(pts3d) map_x, map_y = camera.getMaps(pts2d) out = cv2.remap(img, map_x, map_y, interpolation=cv2.INTER_LINEAR) out = cv2.flip(out, 1) return out pass
paths = ["./data/chess.png","./data/im2.jpeg","./data/img3.jpg"] for mode in range(8): for i, path in enumerate(paths): # Reading the input image img = cv2.imread(path) img = cv2.resize(img,(300,300)) H,W = img.shape[:2] # Creating the virtual camera object c1 = vcam(H=H,W=W) # Creating the surface object plane = meshGen(H,W) # We generate a mirror where for each 3D point, its Z coordinate is defined as Z = F(X,Y) if mode == 0: plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) elif mode == 1: plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) elif mode == 2: plane.Z -= 10*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) elif mode == 3: plane.Z -= 10*np.exp(-0.5*((plane.Y*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) elif mode == 4: plane.Z += 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) + 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H)) elif mode == 5: plane.Z -= 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) - 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H))
def frameFilter(self, frame): """applies a user selected filter to an image Args: frame : an image being passed in Returns: the filtered image :param frame: :param self: """ output = frame if self.filter == "gray": output = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) elif self.filter == "hsv": output = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) elif self.filter == "canny": # temp = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) r, g, b = cv2.split(frame) cr = cv2.Canny(r, 100, 200) cg = cv2.Canny(g, 100, 200) cb = cv2.Canny(b, 100, 200) output = cv2.merge((cr, cg, cb)) # output = cv2.Canny(temp,100,200) elif self.filter == "cartoon": gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.medianBlur(gray, 5) edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9) # Cartoonization color = cv2.bilateralFilter(frame, 9, 250, 250) output = cv2.bitwise_and(color, color, mask=edges) elif self.filter == "negative": # Negate the original image output = 1 - frame elif self.filter == "laplace": # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # s = cv2.Laplacian(gray,cv2.CV_64F, ksize = 3) r, g, b = cv2.split(frame) lr = cv2.Laplacian(r, cv2.CV_64F, ksize=3) lg = cv2.Laplacian(g, cv2.CV_64F, ksize=3) lb = cv2.Laplacian(b, cv2.CV_64F, ksize=3) # cv2.imshow("split laplace",cv2.convertScaleAbs(np.concatenate((lr,lg,lb),axis = 1))) # cv2.imshow("split", np.concatenate((r,g,b),axis = 1)) output = cv2.merge((lr, lg, lb)) output = cv2.convertScaleAbs(output) elif self.filter == "xyz": output = cv2.cvtColor(frame, cv2.COLOR_BGR2XYZ) elif self.filter == "hls": output = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS) elif self.filter == "pencil": img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) img_blur = cv2.GaussianBlur(img_gray, (21, 21), 0, 0) output = cv2.divide(img_gray, img_blur, scale=256) elif self.filter == "warm": output = warming(frame, self) elif self.filter == "cool": output = cooling(frame, self) elif self.filter == "squares": gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blur = cv2.medianBlur(gray, 7) arr = np.array([[-1, -1, 1], [-1, 9, -1], [-1, -1, -1]]) # arr = arr/sum(arr) filt = cv2.filter2D(blur, -1, arr) ret, thresh = cv2.threshold(filt, 160, 255, cv2.THRESH_BINARY) kernel = np.ones((5, 5), np.uint8) # kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5)) morphed = cv2.morphologyEx(thresh, cv2.MORPH_GRADIENT, kernel) contours, hierarchy = cv2.findContours(morphed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) output = cv2.drawContours(frame, contours, -1, (0, 255, 0), 2) elif self.filter == "mirror1": H, W = frame.shape[:2] # Create a virtual camera object. Here H,W correspond to height and width of the input image frame. c1 = vcam(H=H, W=W) # Create surface object plane = meshGen(H, W) # Change the Z coordinate. By default Z is set to 1 # We generate a mirror where for each 3D point, its Z coordinate is defined as Z = 10*sin(2*pi[x/w]*10) plane.Z = 10 * np.sin((plane.X / plane.W) * 2 * np.pi * 10) # Get modified 3D points of the surface pts3d = plane.getPlane() # Project the 3D points and get corresponding 2D image coordinates using our virtual camera object c1 pts2d = c1.project(pts3d) # Get mapx and mapy from the 2d projected points map_x, map_y = c1.getMaps(pts2d) # Applying remap function to input image (img) to generate the funny mirror effect output = cv2.remap(frame, map_x, map_y, interpolation=cv2.INTER_LINEAR) elif self.filter == "mirror2": H, W = frame.shape[:2] # Creating the virtual camera object c1 = vcam(H=H, W=W) # Creating the surface object plane = meshGen(H, W) # We generate a mirror where for each 3D point, its Z coordinate is defined as Z = 20*exp^((x/w)^2 / 2*0.1*sqrt(2*pi)) plane.Z += 20 * np.exp(-0.5 * ((plane.X * 1.0 / plane.W) / 0.1) ** 2) / (0.1 * np.sqrt(2 * np.pi)) # plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) # plane.Z += 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) + 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H)) # plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2) pts3d = plane.getPlane() pts2d = c1.project(pts3d) map_x, map_y = c1.getMaps(pts2d) output = cv2.remap(frame, map_x, map_y, interpolation=cv2.INTER_LINEAR) elif self.filter == "mirror3": H, W = frame.shape[:2] # Creating the virtual camera object c1 = vcam(H=H, W=W) # Creating the surface object plane = meshGen(H, W) # We generate a mirror where for each 3D point, its Z coordinate is defined as Z = 20*exp^((x/w)^2 / 2*0.1*sqrt(2*pi)) # plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) plane.Z += 20 * np.exp(-0.5 * ((plane.Y * 1.0 / plane.H) / 0.1) ** 2) / (0.1 * np.sqrt(2 * np.pi)) # plane.Z += 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) + 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H)) # plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2) pts3d = plane.getPlane() pts2d = c1.project(pts3d) map_x, map_y = c1.getMaps(pts2d) output = cv2.remap(frame, map_x, map_y, interpolation=cv2.INTER_LINEAR) elif self.filter == "mirror4": H, W = frame.shape[:2] # Creating the virtual camera object c1 = vcam(H=H, W=W) # Creating the surface object plane = meshGen(H, W) # We generate a mirror where for each 3D point, its Z coordinate is defined as Z = 20*exp^((x/w)^2 / 2*0.1*sqrt(2*pi)) # plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) # plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) plane.Z += 20 * np.sin(2 * np.pi * ((plane.X - plane.W / 4.0) / plane.W)) + 20 * np.sin( 2 * np.pi * ((plane.Y - plane.H / 4.0) / plane.H)) # plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2) pts3d = plane.getPlane() pts2d = c1.project(pts3d) map_x, map_y = c1.getMaps(pts2d) output = cv2.remap(frame, map_x, map_y, interpolation=cv2.INTER_LINEAR) elif self.filter == "mirror5": H, W = frame.shape[:2] # Creating the virtual camera object c1 = vcam(H=H, W=W) # Creating the surface object plane = meshGen(H, W) # We generate a mirror where for each 3D point, its Z coordinate is defined as Z = 20*exp^((x/w)^2 / 2*0.1*sqrt(2*pi)) # plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) # plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) # plane.Z += 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) + 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H)) plane.Z -= 100 * np.sqrt((plane.X * 1.0 / plane.W) ** 2 + (plane.Y * 1.0 / plane.H) ** 2) pts3d = plane.getPlane() pts2d = c1.project(pts3d) map_x, map_y = c1.getMaps(pts2d) output = cv2.remap(frame, map_x, map_y, interpolation=cv2.INTER_LINEAR) elif self.filter == "distpre": gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = imutils.contours.sort_contours(cnts) for c in cnts: if cv2.contourArea(c) > 100: output = cv2.drawContours(output, c, -1, (0, 255, 0), 2) elif self.filter == "dist": ImgDistances(frame, self) output = frame # elif self.filter =="": # output = cv2.cvtColor(frame,cv2.COLOR) return output
def main(filename,mode): this_scripts_path = os.path.dirname(os.path.realpath(__file__)) mode = int(mode)-1 # Reading the input image img = cv2.imread(this_scripts_path + "/data/" + filename + ".jpg") #img = cv2.resize(img,(300,300)) H,W = img.shape[:2] # Creating the virtual camera object c1 = vcam(H=H,W=W) # Creating the surface object plane = meshGen(H,W) # We generate a mirror where for each 3D point, its Z coordinate is defined as Z = F(X,Y) if mode == 0: plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) elif mode == 1: plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) elif mode == 2: plane.Z -= 10*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) elif mode == 3: plane.Z -= 10*np.exp(-0.5*((plane.Y*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi)) elif mode == 4: plane.Z += 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) + 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H)) elif mode == 5: plane.Z -= 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) - 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H)) elif mode == 6: plane.Z += 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2) elif mode == 7: plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2) else: print("Wrong mode selected") exit(-1) # Extracting the generated 3D plane pts3d = plane.getPlane() # Projecting (Capturing) the plane in the virtual camera pts2d = c1.project(pts3d) # Deriving mapping functions for mesh based warping. map_x,map_y = c1.getMaps(pts2d) # Generating the output output = cv2.remap(img,map_x,map_y,interpolation=cv2.INTER_LINEAR) output = cv2.flip(output,1) # cv2.imshow("Funny Mirror",output) # #cv2.imshow("Input and output",np.hstack((img,np.zeros((H,2,3),dtype=np.uint8),output))) # # Uncomment following line to save the outputs # # cv2.imwrite("Mirror-effect-%d-image-%d.jpg"%(mode+1,i+1),np.hstack((img,np.zeros((H,2,3),dtype=np.uint8),output))) # cv2.waitKey(0) # Name of saved file filename = "output_image." + str(time.time()) + ".jpg" # Doesn't work for some reason? # # Using cv2.imwrite() method # # Saving the image in filepath # filepath = this_scripts_path + '../../static/output_image.jpg' # cv2.imwrite(os.path.join(filepath , filename), imgMorph) # print(filepath) # print(filename) # print(os.path.join(filepath , filename)) # Using cv2.imwrite() method # Saving the image in site dir cv2.imwrite(filename, output) # Removing any potentially already-existing output_image.jpg file in the static folder try: for fl in glob.glob(this_scripts_path+"/../../static/output_image*.jpg"): #Do what you want with the file os.remove(fl) except Exception as e: print(e) # Removing any potentially already-existing output_image.gif file in the static folder try: for fl in glob.glob(this_scripts_path+"/../../static/output_image*.gif"): #Do what you want with the file os.remove(fl) except Exception as e: pass # Moving saved image to static folder move(this_scripts_path+"/../../"+filename, this_scripts_path+"/../../static/") # Removing any potentially already-existing output_image.jpg/gif file in the folder try: for fl in glob.glob(this_scripts_path+"/../../output_image*.jpg"): #Do what you want with the file os.remove(fl) for fl in glob.glob(this_scripts_path+"/../../output_image*.gif"): #Do what you want with the file os.remove(fl) except Exception as e: pass return filename