๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[Python ์œผ๋กœ ์˜์ƒ์ฒ˜๋ฆฌ (1)] cv2.seamlessClone() ์˜์ƒ์˜ ์กฐํ™”๋กœ์›€!! ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/Image Processing

[Python ์œผ๋กœ ์˜์ƒ์ฒ˜๋ฆฌ (1)] cv2.seamlessClone() ์˜์ƒ์˜ ์กฐํ™”๋กœ์›€!!

์ง•์ง•์•ŒํŒŒ์นด 2023. 12. 28. 10:34
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’— NORMAL & MIXED

import cv2
import numpy as np

# ํ–‰๋ ฌ (์ƒ‰ ์ •๋ณด b g r)
logo = cv2.imread('images/logo.png')
lena = cv2.imread('images/lena.jpg')

mask = np.full_like(logo, 255)
height, width = lena.shape[:2]
center = (width//2, height//2)

# ์†Œ์Šค ์ด๋ฏธ์ง€, destination, ๋ณต์ œ ์ฃผ๋ณ€ mask, center ์ด๋ฏธ์ง€, flag
normal = cv2.seamlessClone(logo, lena, mask, center, cv2.NORMAL_CLONE)      # ๋ธ”๋Ÿฌ์ฒ˜๋ฆฌ
mixed = cv2.seamlessClone(logo, lena, mask, center, cv2.MIXED_CLONE)        # ์กฐํ™”๋กญ๊ฒŒ

cv2.imshow('normal', normal)
cv2.imshow('mixed', mixed)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

๐Ÿ’— NORMAL & POLY (ํŠน์ • ์—ฃ์ง€ ๋„๋ ค๋‚ด๊ธฐ..)

import cv2
import numpy as np 

# Read images
src = cv2.imread("images/airplane2.jpg")
dst = cv2.imread("images/red_sky.jpg")

# Create a rough mask around the airplane.
src_mask = np.zeros(src.shape, src.dtype)
# ์ฃผ๋ณ€ ๋„๋ ค๋‚ด๊ธฐ...
poly = np.array([ (0,40), (150,60), (203,56), (200,100), (88,103), (1,75) ], np.int32)
# poly = np.array([ [4,80], [30,54], [151,63], [254,37], [298,90], [272,134], [43,122] ], np.int32)
cv2.fillPoly(src_mask, [poly], (255, 255, 255))

# This is where the CENTER of the airplane will be placed
center = (600,150)

# Clone seamlessly.
output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)

# Save result
cv2.imshow('Output', output)
cv2.waitKey(0)
cv2.destroyAllWindows()

(์šฐ) ๋ฐ‘์—๊บผ poly ๋กœ ์ž‘์šฉํ•˜๋ฉด ๋จธ๋ฆฌ๋ถ€๋ถ„์ด ์ง€์›Œ์ง

 

๐Ÿ’— NORMAL & POLY

import cv2
import numpy as np 

src = cv2.imread("images/airplane1.jpg")
dst = cv2.imread("images/sunset2.jpg")

src_mask = np.zeros(src.shape, src.dtype)
poly = np.array([ [4,190], [160,220], [640,150], [630,180], [690,240], [340,310], [15,300] ], np.int32)
#poly = np.array([ [0,40], [150,60], [203,56], [200,100], [88,103], [1,75] ], np.int32)
#poly = np.array([ [4,80], [30,54], [151,63], [254,37], [298,90], [272,134], [43,122] ], np.int32)
cv2.fillPoly(src_mask, [poly], (255, 255, 255))

center = (600,300)

output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)

cv2.imshow('Output', output)
cv2.waitKey(0)
cv2.destroyAllWindows()

(์šฐ) ๋ฐ‘์— poly ์‹คํ–‰ํ•˜๋ฉด ๋น„ํ–‰๊ธฐ๊ฐ€ ์‚ฌ๋ผ์ง€๋„ค์šฉ

 

๐Ÿ’— NORMAL & POLY & webcam

import cv2

src = cv2.imread("images/airplane2.jpg")
src_mask = np.zeros(src.shape, src.dtype)
poly = np.array([ [0,40], [150,60], [203,56], [200,100], [88,103], [1,75] ], np.int32)
cv2.fillPoly(src_mask, [poly], (255, 255, 255))
center = (200,150)

cap = cv2.VideoCapture(0)

if cap.isOpened():
    print(cap.get(cv2.CAP_PROP_FPS))
    delay = int(1000 / cap.get(cv2.CAP_PROP_FPS))
    while True:
        ret, img = cap.read()
        if ret:
            output = cv2.seamlessClone(src, img, src_mask, center, cv2.NORMAL_CLONE)
            cv2.imshow("Movie", output)
            if cv2.waitKey(delay) & 0xFF == 27 : # ESCํ‚ค
                print("ESC Key pressed")
                break
        else:
            print("No Frame")
            print(ret, img)
            break
        
else:
    print("File not opened")

cap.release()
cv2.destroyAllWindows()

 

728x90
๋ฐ˜์‘ํ˜•
Comments