Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
畸变eff;使用opencvpython的ect_Python_Opencv_Image Processing_Graphics_Computer Vision - Fatal编程技术网

畸变eff;使用opencvpython的ect

畸变eff;使用opencvpython的ect,python,opencv,image-processing,graphics,computer-vision,Python,Opencv,Image Processing,Graphics,Computer Vision,我想使用python中的cv2库创建扭曲效果,如螺旋、拉伸、鱼眼、楔形和其他效果,如水下和雪。我发现了鱼眼扭曲 在OpenCV版本3.0及更高版本中,可以使用cv2.fisheye.unsortimage()执行此操作。如果需要,我有python代码 这是我为以下输入图像得到的结果: 输入图像: 失真图像: 该函数接受一个矩阵,修改后会产生不同的图像失真 更新 为了添加降雪效果,您可以添加一些噪声,如泊松噪声。这里是答案的一半。cv2.remap函数使用贴图从源中为目标中的每个像素选择一个像

我想使用python中的cv2库创建扭曲效果,如螺旋、拉伸、鱼眼、楔形和其他效果,如水下和雪。我发现了鱼眼扭曲

在OpenCV版本3.0及更高版本中,可以使用
cv2.fisheye.unsortimage()
执行此操作。如果需要,我有python代码

这是我为以下输入图像得到的结果:

输入图像:

失真图像:

该函数接受一个矩阵,修改后会产生不同的图像失真

更新


为了添加降雪效果,您可以添加一些噪声,如泊松噪声。

这里是答案的一半。cv2.remap函数使用贴图从源中为目标中的每个像素选择一个像素。alkasm对此的回答是: 在定义流程方面做了大量工作,但掩盖了这些地图的有用性。如果你能在地图上发挥创意,你可以做出任何你想要的效果。这是我想到的

程序首先加载图像并调整其大小。这对于较小的屏幕是一种方便。然后创建空映射

贴图的尺寸必须与正在处理的图像相同,但深度必须为1。如果调整大小的原始文件为633 x 400 x 3,则贴图都需要为633 x 400

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

最简单的映射是如果对于每个(x,y),map1(x,y)=x和map2(x,y)=y。这将创建一个1对1映射,并且目标将与源匹配。在本例中,每个值都会添加一个小偏移。偏移中的余弦函数创建正偏移和负偏移,从而在最终图像中创建波

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

    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()

这里是一个替换块,用来在图像中间画出鱼眼。请在别处查找有关数学的详细信息。用它代替前面代码中的2 for循环

如我的回答的前半部分所述(参见前面的回答),此块的目的是创建两个贴图,它们一起工作以将源图像重新映射到目标图像

要创建这两个贴图,此块将扫描具有图像尺寸的2个for循环。计算X和y贴图(flex_X和flex_y)的值。首先,为1对1替换贴图将每个贴图简单地指定为x和y。然后,如果半径(r)介于0和1之间,将应用鱼眼的切线滑动贴图,并映射新的flex_x和flex_y值

请参阅我的其他答案了解更多详细信息

    # create simple maps with a modified assignment
    # outside the bulge is normal, inside is modified
    # this is where the magic is assembled
    for y in range(h):
        ny = ((2*y-250)/(h-250))-1     #play with the 250's to move the y
        ny2 = ny*ny
        for x in range(w):
            nx = ((2*x-50)/(w-50))-1   #play with the 50's to move the x
            nx2 = nx*nx
            r = math.sqrt(nx2+ny2)
            flex_x[y,x] = x
            flex_y[y,x] = y
            if r>0 and r<1:
                nr1 = 1 - r**2
                nr2 = math.sqrt(nr1)
                nr = (r + (1.0-nr2)) / 2.0
                theta = math.atan2(ny,nx)
                nxn = nr*math.cos(theta)
                nyn = nr*math.sin(theta)
                flex_x[y,x] = (((nxn+1)*w)/2.0)
                flex_y[y,x] = (((nyn+1)*h)/2.0)
#使用修改后的分配创建简单地图
#凸起外部为正常,内部为修改
#这就是魔法的聚集地
对于范围(h)内的y:
ny=((2*y-250)/(h-250))-1#使用250移动y
ny2=ny*ny
对于范围(w)内的x:
nx=((2*x-50)/(w-50))-1#使用50移动x
nx2=nx*nx
r=math.sqrt(nx2+ny2)
flex_x[y,x]=x
弹性体y[y,x]=y

如果r>0,r感谢你,杰鲁·卢克。但我认为它是用来纠正鱼眼效应的。我想从普通图像创建鱼眼效果。Imagemagick有很多效果。您可以使用Python Wand,它基于Imagemagick来做很多事情。虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。