二维矩阵到一个犯人的图像

2024-06-17 12:11:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大小为512x256的二维数字矩阵。我可以很容易地使用PIL或scipy等将其转换为图像,但显然,这给了我一个512x256大小的矩形的形状。我想知道我能不能做点什么,让这个矩阵变成一个圆锥形状,就像所附的图一样?enter image description here

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


Tags: 图像pil矩阵数字scipy社区增量形状
1条回答
网友
1楼 · 发布于 2024-06-17 12:11:48

下面是一个快速和肮脏的解决方案,它将结果图像中的极坐标映射到原始图像中的直角坐标,并在原始图像的每个通道上使用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()

产生:

warped logo of StackOverflow

表演很糟糕,没有费心去“矢量化”。当发现如何更新。在

相关问题 更多 >