【问题标题】:Distortion effect using OpenCv-python使用 OpenCv-python 的失真效果
【发布时间】:2017-08-01 15:41:00
【问题描述】:

我想在 python 中使用 cv2 库来创建像这样website 这样的螺旋、拉伸、鱼眼、楔形等变形效果。

【问题讨论】:

  • Imagemagick 有很多效果。你可以使用基于 Imagemagick 的 Python Wand 来做很多事情。 imagemagick.org/Usage

标签: python opencv image-processing graphics computer-vision


【解决方案1】:

这是答案的一半。 cv2.remap 函数使用映射从源中为目标中的每个像素选择一个像素。 alkasm 对此的回答:How do I use OpenCV's remap function? 在定义流程方面做得很好,但掩盖了这些地图的用处。如果您可以在地图中发挥创意,您​​可以制作任何您想要的效果。这是我想出的。

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

地图需要与正在处理的图像具有相同的尺寸,但深度为 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】:

    这是一个替换块,用于在图像中间绘制鱼眼。请在其他地方查找有关数学的详细信息。用它代替前面代码中的 2 个 for 循环。

    正如我的答案前半部分所述(参见上一个答案),此块的目的是创建 2 个映射,它们共同将源图像重新映射到目标图像。

    要创建这两个地图,此块会使用图像的尺寸扫描 2 个 for 循环。为 X 和 y 贴图(flex_x 和 flex_y)计算值。它首先将每个简单地分配给 x 和 y 以获得 1 对 1 的替换映射。然后,如果半径 (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)
    

    【讨论】:

    • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    【解决方案3】:

    我发现鱼眼失真了。

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

    这是我从以下输入图像中得到的:

    输入图像:

    扭曲的图像:

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

    更新

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

    【讨论】:

    • 谢谢杰鲁卢克。但我认为它用于校正鱼眼效果。我想从普通图像创建鱼眼效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多