Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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_Python 3.x_Image Processing - Fatal编程技术网

Python 将二维矩阵转换为圆锥体的图像

Python 将二维矩阵转换为圆锥体的图像,python,python-3.x,image-processing,Python,Python 3.x,Image Processing,我有一个大小为512x256的2D numpy矩阵。我可以很容易地使用PIL或scipy等将其转换为图像,但很明显,这给了我512x256大小的矩形的形状。我想知道我是否可以做些什么,使这个矩阵成为一个圆锥形状,如图所附 我是怎么想的,矩阵的第一列是圆锥最左边的一条线,矩阵的下一列是在这条线的右边,依此类推。因为两个极端之间的角度是45度,我有256列,这意味着每一行的角度增量是(45/256)?这些只是一些粗略的想法,但我想向社区学习,如果他们对我应该如何进行这项工作有任何想法的话?我设想一个

我有一个大小为512x256的2D numpy矩阵。我可以很容易地使用PIL或scipy等将其转换为图像,但很明显,这给了我512x256大小的矩形的形状。我想知道我是否可以做些什么,使这个矩阵成为一个圆锥形状,如图所附


我是怎么想的,矩阵的第一列是圆锥最左边的一条线,矩阵的下一列是在这条线的右边,依此类推。因为两个极端之间的角度是45度,我有256列,这意味着每一行的角度增量是(45/256)?这些只是一些粗略的想法,但我想向社区学习,如果他们对我应该如何进行这项工作有任何想法的话?我设想一个黑色正方形的主图像,这个圆锥体在中间。有什么想法/想法吗

这是一个快速而肮脏的解决方案,它将结果图像中的极坐标映射到原始图像中的直角坐标,并在原始图像的每个通道上使用
interp2d

import numpy as np
from scipy import misc
from scipy.interpolate import interp2d
from math import pi, atan2, hypot

inputImagePath = 'wherever/whateverYouWantToInterpolate.jpg'
resultWidth = 800
resultHeight = 600
centerX = resultWidth / 2
centerY = - 50.0
maxAngle =  45.0 / 2 / 180 * pi
minAngle = -maxAngle
minRadius = 100.0
maxRadius = 600.0

inputImage = misc.imread(inputImagePath)
h,w,chn = inputImage.shape
print(f"h = {h} w = {w} chn = {chn}")
channels = [inputImage[:,:,i] for i in range(3)]
interpolated = [interp2d(range(w), range(h), c) for c in channels]
resultImage = np.zeros([resultHeight, resultWidth, 3], dtype = np.uint8)

for c in range(resultWidth):
  for r in range(resultHeight):
    dx = c - centerX
    dy = r - centerY
    angle = atan2(dx, dy) # yes, dx, dy in this case!
    if angle < maxAngle and angle > minAngle:
      origCol = (angle - minAngle) / (maxAngle - minAngle) * w
      radius = hypot(dx, dy)
      if radius > minRadius and radius < maxRadius:
        origRow = (radius - minRadius) / (maxRadius - minRadius) * h
        for chn in range(3):
          resultImage[r, c, chn] = interpolated[chn](origCol, origRow)

import matplotlib.pyplot as plt
plt.imshow(resultImage)
plt.show()
将numpy导入为np
从scipy导入杂项
从scipy.interpole导入interp2d
从数学导入pi、atan2、hypot
inputImagePath='where/whateverYouWantToInterpolate.jpg'
resultWidth=800
结果光=600
centerX=结果宽度/2
centerY=-50.0
最大角度=45.0/2/180*pi
minAngle=-maxAngle
最小半径=100.0
最大半径=600.0
inputImage=misc.imread(inputImagePath)
h、 w,chn=inputImage.shape
打印(f“h={h}w={w}chn={chn}”)
通道=[inputImage[:,:,i]用于范围(3)中的i]
插值=[通道中c的interp2d(范围(w)、范围(h)、c)]
resultImage=np.zero([resultHeight,resulttwidth,3],dtype=np.uint8)
对于范围内的c(结果宽度):
对于范围内的r(结果灯):
dx=c-centerX
dy=r-centerY
角度=atan2(dx,dy)#是的,在这种情况下,dx,dy!
如果角度<最大角度和角度>最小角度:
origCol=(角度-米南格尔)/(最大角度-米南格尔)*w
半径=下压(dx,dy)
如果半径>最小半径且半径<最大半径:
origRow=(半径-最小半径)/(最大半径-最小半径)*h
对于范围(3)内的chn:
结果年龄[r,c,chn]=插值[chn](origCol,origRow)
将matplotlib.pyplot作为plt导入
plt.imshow(结果图像)
plt.show()
产生:


性能很差,没有费心“矢量化”。将在了解如何更新时更新。

您始终需要完整的像素矩阵来生成图像,因此,当您希望从等距笛卡尔坐标系进行变换,但仍要生成基于像素的图片时,您必须考虑将什么放入像素中,一旦生成圆锥体,每个像素的定义将为空。如果你在谷歌上搜索“投影”或“变换”,你应该能找到制作圆锥体的方法。但正如所指出的,您仍然可以根据自己的喜好填充“空”像素。或者,也许你很幸运,有人为此制作了一个库,如果是的话,我不知道。那么,你实际上想要实现什么,即你所看到的更高层次的问题是什么?我的意思是,有很多软件可以混合/变形矢量图形(以及像素图形),但你的起点是一个由numpy矩阵表示的图像?基本上,你在寻找从极坐标到线性的变换,其中原始图像中的X是角度,Y是距离。可能有点use@ahed87-你看过超声图像吗?它们是圆锥形的。你是对的,我的出发点不是一个numpy矩阵,而是数字的原始数据。我必须清理它,然后创建几个矩阵,每个矩阵代表一个框架。是的,谷歌没有返回任何方便的库,我可以用它来进行转换。我刚刚看到了你的答案,并通读了代码。顺便说一句,“h,w,chn=inputImage.shape”中的“chn”是什么?测试123个颜色通道,通常应该是
3
,可以省略。我认为如果我在其他一些地方用
chn
替换
3
,它也应该适用于灰度图像;;这不是中国的国家代码,也不是“中国制造”,如果你的意思是…;)哈哈…“中国基金公司”听起来更好:)除了开玩笑,你的解决方案有效!