如何在OpenCV中变形? [英] How to do deformations in OpenCV?

查看:237
本文介绍了如何在OpenCV中变形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何使图像变形感兴趣,例如,有一张1000x1000的照片,在点500400处,我想说明如何在Photoshop中用塑料完成它.

I am interested in how to deform the image, for example, there is a photo 1000x1000, at the point 500 400, I want to inflate how it can be done with plastic in Photoshop.

我没有代码,因为找不到合适的代码.

I do not have a code, because I did not find the right one.

推荐答案

您可以使用

You can use remapping. Here's a quick demo, but note that you'd have to know the exact distortion function to duplicate Photoshop's functionality

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import math
img = cv.imread('kit.jpg')

right_eye = (215,105)
radius = 30
power = 1.6 # >1.0 for expansion, <1.0 for shrinkage

height, width, _ = img.shape
map_y = np.zeros((height,width),dtype=np.float32)
map_x = np.zeros((height,width),dtype=np.float32)

# create index map
for i in range(height):
    for j in range(width):
        map_y[i][j]=i
        map_x[i][j]=j

# deform around the right eye
for i in range (-radius, radius):
    for j in range(-radius, radius):
        if (i**2 + j**2 > radius ** 2):
            continue

        if i > 0:
            map_y[right_eye[1] + i][right_eye[0] + j] = right_eye[1] + (i/radius)**power * radius
        if i < 0:
            map_y[right_eye[1] + i][right_eye[0] + j] = right_eye[1] - (-i/radius)**power * radius
        if j > 0:
            map_x[right_eye[1] + i][right_eye[0] + j] = right_eye[0] + (j/radius)**power * radius
        if j < 0:
            map_x[right_eye[1] + i][right_eye[0] + j] = right_eye[0] - (-j/radius)**power * radius

warped=cv.remap(img,map_x,map_y,cv.INTER_LINEAR)
cv.imwrite('warp.jpg', warped)

这篇关于如何在OpenCV中变形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆