简体   繁体   English

2d矩阵到锥形图像

[英]2d matrix to image of a cone

I have a 2D numpy matrix of size 512x256. 我有一个尺寸为512x256的2D numpy矩阵。 I can easily convert it to an image using PIL, or scipy, etc. but that gives me the shape of a rectangle of size 512x256, obviously. 我可以很容易地使用PIL或scipy等将其转换为图像,但这显然给了我一个512x256大小的矩形形状。 I am wondering if I can do something to make this matrix take shape of a cone like the figure attached? 我想知道我是否可以做一些事情来使这个矩阵形成一个像附图所示的锥形? 在此输入图像描述

How I am thinking about it is that the first column of the matrix would be the left most line of the cone and the next column of the matrix would be a little right to that line, and so on. 我如何思考它是矩阵的第一列是锥体的最左边的行,矩阵的下一列对那条线来说是一点点,依此类推。 Since the angle between the two extremes is 45 degrees and I have 256 columns, that would be mean that each line gets an increment of (45/256) degree angle? 由于两个极端之间的角度是45度,而我有256列,这意味着每条线的增量为(45/256)度角? These are just some rough thoughts but I wanted to learn from the community if they have any ideas about how should I proceed with this? 这些只是一些粗略的想法,但我想向社区学习,如果他们对我应该如何处理这个有任何想法? I am envisioning a black square main image and this cone in the middle of it. 我正在设想一个黑色方形主图像和它中间的这个锥形。 Any ideas/thoughts? 任何想法/想法?

Here is a quick & dirty solution that maps polar coordinates in the result image to rectangular coordinates in the original image and uses interp2d on each channel of the original image: 这是一个快速而肮脏的解决方案,它将结果图像中的极坐标映射到原始图像中的直角坐标,并在原始图像的每个通道上使用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()

Produces: 生产:

StackOverflow的扭曲徽标

The performance is terrible, didn't bother to "vectorize". 表现很糟糕,没有费心去“矢量化”。 Will update when find out how. 在找到方法时会更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM