Python ve OpenCV Kullanarak Komik Aynalar Uygulaması Geliştirmek

0 773

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 dersleri

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

LearnOpenCV

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:

  1. OpenCV Nedir?
  2. Python ve OpenCV ile Video Oynatma
  3. Python ve OpenCV ile Videoyu Renklerine Ayırma
  4. Python ve OpenCV İle Video Kaydetme
  5. Python ve OpenCV ile Resim İşlemlerine Giriş
  6. Python ve OpenCV Kullanarak Yüz Tanıma Uygulaması
  7. Python ve OpenCV Kullanarak Bilgisayar Kamerasından Görüntü Almak
  8. Pyton ve OpenCV Kullanarak Kenar Algılama Uygulaması
  9. Python ve OpenCV Kullanarak Resimlere Elastik Efekti Verme
  10. Python ve OpenCV Kullanarak SuperPixel İşlemi Uygulaması
  11. Python Ve OpenCv İle WebCam’den Aldığınız Görüntüyü İşlemek
  12. Python Ve OpenCV Ile Resimlerinizi JSON Formatına Çevirin
  13. Python Ve OpenCV Ile Youtube Videolarını Stream Yapma
  14. Python Ve OpenCV Kullanarak Bir Resmin Tüm Piksellerine Ulaşmak
Cevap bırakın

E-posta hesabınız yayımlanmayacak.

Bu web sitesi deneyiminizi geliştirmek için çerezleri kullanır. Bununla iyi olduğunuzu varsayacağız, ancak isterseniz vazgeçebilirsiniz. Kabul etmek Mesajları Oku