我有一個練習將影像轉換為具有這樣的光隧道效果的影像。


我的第一個想法是在中間選擇一個區域圓,如果中心點,邊界點和考慮的點在同一條線上,那么圓外的每個點都將具有圓的邊界點的值,但結果非常貧窮的。

這是我的代碼:
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)
有沒有人有更好的主意,或者我的代碼可以修復嗎?, 請幫我!非常感激!
uj5u.com熱心網友回復:
這需要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)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/410290.html
標籤:
