Python ve Açık kaynak kodlu görüntü işleme kütüphanesi OpenCV kullanarak komik aynalar uygulaması yapacağız.
Komik aynalar düzlem aynalar değil, bu aynaların önünde hareket ederken komik görünen bozulma efektleri üreten dışbükey / içbükey yansıtıcı yüzeylerin bir kombinasyonudur.
Yapacağımız komik aynalar uygulamasının örnek uygulamasını alttaki videoda gözleyebilirsiniz.
Bu yazıda, OpenCV kullanarak kendi komik aynalarımızı oluşturmayı öğreneceğiz.
OpenCv ile Komik Aynalar Uygulaması
Image dosyasını kullanarak komik aynalar uygulaması için gerekli kod.
import cv2
import numpy as np
import math
from vcam import vcam,meshGen
paths = ["./data/chess.png"]
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))
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)
Kodumuzu çalıştırdıktan sonra elde edeceğimiz görüntü 6 farklı moddan istediğimizi ayarlayabiliriz.
Kameradan gelen görüntüyü veya bir video dosyasından gelen görüntüyü alarak farklı biçimlere sokmak için alttaki kodu kullanabilirsiniz.
import cv2
import numpy as np
import math
from vcam import vcam,meshGen
import sys
cap = cv2.VideoCapture(0)
ret, img = cap.read()
H,W = img.shape[:2]
fps = 30
# Creating the virtual camera object
c1 = vcam(H=H,W=W)
# Creating the surface object
plane = meshGen(H,W)
mode = int(1)
# 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)
ret, img = cap.read()
while 1:
ret, img = cap.read()
if ret:
output = cv2.remap(img,map_x,map_y,interpolation=cv2.INTER_LINEAR,borderMode=4)
output = cv2.flip(output,1)
out1 = np.hstack((img,output))
out1 = cv2.resize(out1,(700,350))
cv2.imshow("output",out1)
if cv2.waitKey(1)&0xFF == 27:
break
else:
break
Kodların çalışabilmesi için OpenCV kütüphanesinin yanında VCAM kütüphanesininde yüklü olması gerekiyor.
OpenCV Komik Aynalar Uygulaması İçin Daha Fazla Kaynak:
https://github.com/kaustubh-sadekar/FunMirrors
https://github.com/spmallick/learnopencv/tree/master/FunnyMirrors
Python ve OpenCv görüntü işleme kütüphanesi kullanarak hazırladığımız diğer örneklerimizin listesi:
- OpenCV Nedir?
- Python ve OpenCV ile Video Oynatma
- Python ve OpenCV ile Videoyu Renklerine Ayırma
- Python ve OpenCV İle Video Kaydetme
- Python ve OpenCV ile Resim İşlemlerine Giriş
- Python ve OpenCV Kullanarak Yüz Tanıma Uygulaması
- Python ve OpenCV Kullanarak Bilgisayar Kamerasından Görüntü Almak
- Pyton ve OpenCV Kullanarak Kenar Algılama Uygulaması
- Python ve OpenCV Kullanarak Resimlere Elastik Efekti Verme
- Python ve OpenCV Kullanarak SuperPixel İşlemi Uygulaması
- Python Ve OpenCv İle WebCam’den Aldığınız Görüntüyü İşlemek
- Python Ve OpenCV Ile Resimlerinizi JSON Formatına Çevirin
- Python Ve OpenCV Ile Youtube Videolarını Stream Yapma
- Python Ve OpenCV Kullanarak Bir Resmin Tüm Piksellerine Ulaşmak


