使用OpenCv-python的失真效果 [英] Distortion effect using OpenCv-python

查看:94
本文介绍了使用OpenCv-python的失真效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建诸如螺旋,拉伸,鱼眼,楔形的扭曲效果,以及诸如此类网站的其他效果,如水下和雪景在python中使用cv2库.

I want to create distortion effect like Spiral, stretch, fisheye, Wedge and other effect like underwater and snow like this website using cv2 library in python.

推荐答案

这是答案的一半.cv2.remap函数使用地图从源中为目标中的每个像素选择一个像素.alkasm对此的回答:如何使用OpenCV的重映射功能?在定义流程方面做得很好,但是掩盖了这些地图的用处.如果您可以在地图上发挥创意,则可以产生任何想要的效果.这是我想出的.

Here is half of an answer. The cv2.remap function uses maps to choose a pixel from the source for each pixel in the destination. alkasm's answer to this: How do I use OpenCV's remap function? does a great job of defining the process, but glosses over the usefulness of those maps. If you can get creative in the maps, you can make any effect you want. Here is what I came up with.

程序首先加载图像并调整其大小.这对于较小的屏幕来说很方便.然后创建空地图.

The program starts by loading the image and resizing it. This is a convenience for a smaller screen. Then the empty maps are created.

地图的尺寸​​必须与要处理的图像相同,但深度为1.如果调整后的原始尺寸为633 x 400 x 3,则地图的尺寸​​都必须为633 x 400.

The maps need to be the same dimensions as the image that is being processed, but with a depth of 1. If the resized original is 633 x 400 x 3, the maps both need to be 633 x 400.

重新映射完成后,cv2.remap将使用地图中每个坐标处的值来确定要在目标中使用的原始像素.对于目标中的每个x,y,dest [x,y] = src [map1 [x,y],map2 [x,y]].

When the remapping is done, cv2.remap will used the value at each coordinate in the maps to determine which pixel in the original to use in the destination. For each x,y in the destination, dest[x,y] = src[map1[x,y],map2[x,y]].

最简单的映射是,对于每个(x,y),map1(x,y)= x和map2(x,y)= y.这将创建一对一映射,并且目的地将与源匹配.在此示例中,一个小的偏移量被添加到每个值.偏移中的余弦函数会同时产生正向和负向偏移,从而在最终图像中产生波动.

The simplest mapping would be if for every (x,y), map1(x,y)=x and map2(x,y)=y. This creates a 1-to-1 map, and the destination would match the source. In this example, a small offset is added to each value. The cosine function in the offset creates both positive and negative shifts, creating waves in the final image.

请注意,创建地图的速度很慢,但是cv2.remap的速度却很快.创建地图后,cv2.remap的速度足以将其应用于视频帧.

Note that creating the maps is slow, but the cv2.remap is fast. Once you have created the map, the cv2.remap is fast enough to be applied to frames of video.

    import numpy as np            #create waves
    import cv2
    import math

    # read in image and resize down to width of 400
    # load your image file here
    image = cv2.imread("20191114_154534.jpg")

    r = 400.0 / image.shape[1]
    dim = (400, int(image.shape[0] * r))

    # Perform the resizing of the image
    resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)

    # Grab the dimensions of the image and calculate the center
    # of the image  (center not needed at this time)
    (h, w, c) = resized.shape
    center = (w // 2, h // 2)

    # set up the x and y maps as float32
    flex_x = np.zeros((h,w),np.float32)
    flex_y = np.zeros((h,w),np.float32)

    # create simple maps with a modified assignment
    # the math modifier creates ripples.  increase the divisor for less waves, 
    # increase the multiplier for greater movement
    # this is where the magic is assembled
    for y in range(h):
        for x in range(w):
            flex_x[y,x] = x + math.cos(x/15) * 15
            flex_y[y,x] = y + math.cos(y/30) * 25


    # do the remap  this is where the magic happens      
    dst = cv2.remap(resized,flex_x,flex_y,cv2.INTER_LINEAR)


    #show the results and wait for a key
    cv2.imshow("Resized",resized)
    cv2.imshow("Flexed",dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

这篇关于使用OpenCv-python的失真效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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