Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
Python中的变形_Python_Image Processing_Cylindrical_Anamorphism - Fatal编程技术网

Python中的变形

Python中的变形,python,image-processing,cylindrical,anamorphism,Python,Image Processing,Cylindrical,Anamorphism,我试图通过以下链接对图像进行变形 它给出了一个变形的图像,但它给出了半圆中的图像。但是我想要一个完整的圆大小的输出。 我试过了 warp[c-j, i-1] = img[p-1, q-1] warp[c+j, i-1] = img[p-1, q-1] 而不是warp[c-j,i-1]=img[p-1,q-1] 但它不会在整圈中给出一个图像,而是创建两次相同的输出 谁能帮帮我吗 完整代码: import math from cv2 import * import numpy as np im

我试图通过以下链接对图像进行变形

它给出了一个变形的图像,但它给出了半圆中的图像。但是我想要一个完整的圆大小的输出。 我试过了

warp[c-j, i-1] = img[p-1, q-1]
warp[c+j, i-1] = img[p-1, q-1]
而不是
warp[c-j,i-1]=img[p-1,q-1]

但它不会在整圈中给出一个图像,而是创建两次相同的输出

谁能帮帮我吗

完整代码:

import math
from cv2 import *
import numpy as np

img = imread("test.jpg")
(rows, cols) = (img.shape[0], img.shape[1])
r = 0  #offset-gives space to keep cylinder and height of the image from bottom: original: math.trunc(.25*rows)
c = rows  #this will be the decisive factor in size of output image-maximum radius of warped image: original: c = r+rows
warp = np.zeros([c,2*c,3], dtype=np.uint8)


def convert(R, b):
    return math.trunc(b*rows/(2*math.asin(1))), math.trunc(c-R)


for i in range(0, 2*c):
    for j in range(1, c):
        b = math.atan2(j, i-c)
        R = math.sqrt(j*j+math.pow(i-c, 2))
        if R>=r and R<=c:
            (q, p) = convert(R, b)
            warp[c-j, i-1] = img[p-1, q-1]
            #warp[c+j, i-1] = img[p-1, q-1]

imshow("Output", warp)
waitKey()
导入数学
从cv2进口*
将numpy作为np导入
img=imread(“test.jpg”)
(行,列)=(img.shape[0],img.shape[1])
r=0#偏移量提供空间,以保持柱面和图像从底部的高度:原始:math.trunc(.25*行)
c=行#这将是输出图像大小的决定性因素扭曲图像的最大半径:原始:c=r+行
warp=np.zero([c,2*c,3],dtype=np.uint8)
def转换(R,b):
返回math.trunc(b*行/(2*math.asin(1))、math.trunc(c-R)
对于范围(0,2*c)内的i:
对于范围(1,c)内的j:
b=数学常数2(j,i-c)
R=数学sqrt(j*j+math.pow(i-c,2))

如果R>=R且R类似于列偏移量,则在计算
b
R
时,还应包括行偏移量。由于扭曲图像具有
c
行,因此偏移量为
c//2

b = math.atan2(j - c//2, i-c)
R = math.sqrt((j - c//2)**2 + math.pow(i-c, 2))
请注意,扭曲的图像不是一个完美的圆,因为您指定它的宽度是高度的两倍。如果您想要一个完整的圆,您还应该将
R
的上边界检查调整为
c//2
,因为这是沿行的最大半径:

if r <= R <= c//2:
    ...
但是,在任何情况下,您都可以从一开始就使用方形图像,即指定
warp.shape==(c,c)

编辑 更新的代码,使用扭曲图像的原始尺寸:

import math
import cv2
import numpy as np

img = cv2.imread("/tmp/img.jpg")
(rows, cols) = (img.shape[0], img.shape[1])
r = 0
c = rows // 2
warp = np.zeros([rows, cols, 3], dtype=np.uint8)


def convert(R, b):
    return math.trunc(c * (1 - b/math.pi)), 2*math.trunc(c - R) - 1


for i in range(0, cols):
    for j in range(0, rows):
        b = math.atan2(j - c, i - c)
        R = math.sqrt((j - c)**2 + (i - c)**2)
        if r <= R <= c:
            q, p = convert(R, b)
            warp[j, i] = img[p, q]

cv2.imshow("Output", warp)
cv2.waitKey()
导入数学
进口cv2
将numpy作为np导入
img=cv2.imread(“/tmp/img.jpg”)
(行,列)=(img.shape[0],img.shape[1])
r=0
c=行//2
warp=np.zero([rows,cols,3],dtype=np.uint8)
def转换(R,b):
返回math.trunc(c*(1-b/math.pi)),2*math.trunc(c-R)-1
对于范围内的i(0,cols):
对于范围内的j(0,行):
b=数学常数2(j-c,i-c)
R=数学sqrt((j-c)**2+(i-c)**2)

如果是r,请提供所有相关输入数据以及算法的相关部分。谢谢。用完整的代码更新了问题。谢谢。它确实给出了一个完整的循环,但问题是它在同一输出中产生了两次效果。
import math
import cv2
import numpy as np

img = cv2.imread("/tmp/img.jpg")
(rows, cols) = (img.shape[0], img.shape[1])
r = 0
c = rows // 2
warp = np.zeros([rows, cols, 3], dtype=np.uint8)


def convert(R, b):
    return math.trunc(c * (1 - b/math.pi)), 2*math.trunc(c - R) - 1


for i in range(0, cols):
    for j in range(0, rows):
        b = math.atan2(j - c, i - c)
        R = math.sqrt((j - c)**2 + (i - c)**2)
        if r <= R <= c:
            q, p = convert(R, b)
            warp[j, i] = img[p, q]

cv2.imshow("Output", warp)
cv2.waitKey()