如何创建光隧道变换图像 [英] How to create light tunnel transformation image

查看:19
本文介绍了如何创建光隧道变换图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将图像转换为具有光隧道效果的图像的练习,如下所示。

我的第一个想法是在中间选择一个区域圆,如果中心点、边界点和所考虑的点在同一条线上,则圆外的每个点都将具有该圆的边界点的值,但结果很差。

以下是我的代码:

import numpy as np
import cv2 
import math 
import numpy as np
from google.colab.patches import cv2_imshow
from shapely.geometry import LineString
from shapely.geometry import Point

img = cv2.imread("./orig_img.png")
h,w,_ = img.shape
flex_x = np.zeros((h,w),np.float32)
flex_y = np.zeros((h,w),np.float32)

scale_y= 1
center_x, center_y = (w // 2, h // 2)
radius = 50
scale_x = 1

p = Point(center_x, center_y)
c = p.buffer(radius).boundary

for y in range(h):
    delta_y = scale_y * (y - center_y)
    for x in range(w):
        delta_x = scale_x * (x - center_x)
        distance = delta_x * delta_x + delta_y * delta_y
        
        if distance <= (radius * radius):
            flex_x[y, x] = x
            flex_y[y, x] = y
        else:
            l = LineString([(center_x, center_y), (x,y)])
            i = c.intersection(l)
            new_x,new_y = round(i.coords[0][0]),round(i.coords[0][1])
            flex_x[y,x] = flex_x[new_y,new_x]
            flex_y[y,x] = flex_y[new_y,new_x]
            


dst = cv2.remap(img, flex_x, flex_y, cv2.INTER_LINEAR)

cv2_imshow(dst)

有没有人有更好的主意来做这件事,或者我的代码可以修复吗?请帮助我!非常感谢!

推荐答案

这需要cv.remap和一些几何图形。

您遇到了麻烦,因为您试图划线。您甚至不需要为此重新映射。

im = cv.imread("d1LU6.png")
(h,w) = im.shape[:2]

# some definitions
center = np.array([w/2, h/2])
radius = h / 5

i,j = np.mgrid[0:h, 0:w]
xymap = np.dstack([j,i]).astype(np.float32) # "identity" map

# coordinates relative to center
coords = (xymap - center)
# distance to center
dist = np.linalg.norm(coords, axis=2)
# touch only what's outside of the circle
mask = (dist >= radius)
# project onto circle (calculate unit vectors, move onto circle, then back to top-left origin)
xymap[mask] = coords[mask] / dist[mask,None] * radius + center

out = cv.remap(im, map1=xymap, map2=None, interpolation=cv.INTER_LINEAR)
# imshow(out)

这篇关于如何创建光隧道变换图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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