将图像环绕一个圆圈
Wrap image around a circle
在此示例中,我尝试做的是将图像环绕成一个圆圈,如下所示。
为了包裹图像,我使用三角函数简单地计算了 x,y 坐标。
问题是计算出的 X 和 Y 位置四舍五入使它们成为整数。这会导致在上面的包裹图像中看到空白像素。 x,y 位置必须是整数,因为它们是列表中的位置。
我在下面的代码中再次执行了此操作,但没有任何图像以便于查看。我所做的就是创建两个具有二进制值的数组,一个数组是黑色的,另一个是白色的,然后将一个包裹到另一个上。
代码的输出是。
import math as m
from PIL import Image # only used for showing output as image
width = 254.0
height = 24.0
Ro = 40.0
img = [[1 for x in range(int(width))] for y in range(int(height))]
cir = [[0 for x in range(int(Ro * 2))] for y in range(int(Ro * 2))]
def shom_im(img): # for showing data as image
list_image = [item for sublist in img for item in sublist]
new_image = Image.new("1", (len(img[0]), len(img)))
new_image.putdata(list_image)
new_image.show()
increment = m.radians(360 / width)
rad = Ro - 0.5
for i, row in enumerate(img):
hyp = rad - i
for j, column in enumerate(row):
alpha = j * increment
x = m.cos(alpha) * hyp + rad
y = m.sin(alpha) * hyp + rad
# put value from original image to its position in new image
cir[int(round(y))][int(round(x))] = img[i][j]
shom_im(cir)
我后来发现了中点圆算法,但结果更糟
from PIL import Image # only used for showing output as image
width, height = 254, 24
ro = 40
img = [[(0, 0, 0, 1) for x in range(int(width))]
for y in range(int(height))]
cir = [[(0, 0, 0, 255) for x in range(int(ro * 2))] for y in range(int(ro * 2))]
def shom_im(img): # for showing data as image
list_image = [item for sublist in img for item in sublist]
new_image = Image.new("RGBA", (len(img[0]), len(img)))
new_image.putdata(list_image)
new_image.show()
def putpixel(x0, y0):
global cir
cir[y0][x0] = (255, 255, 255, 255)
def drawcircle(x0, y0, radius):
x = radius
y = 0
err = 0
while (x >= y):
putpixel(x0 + x, y0 + y)
putpixel(x0 + y, y0 + x)
putpixel(x0 - y, y0 + x)
putpixel(x0 - x, y0 + y)
putpixel(x0 - x, y0 - y)
putpixel(x0 - y, y0 - x)
putpixel(x0 + y, y0 - x)
putpixel(x0 + x, y0 - y)
y += 1
err += 1 + 2 * y
if (2 * (err - x) + 1 > 0):
x -= 1
err += 1 - 2 * x
for i, row in enumerate(img):
rad = ro - i
drawcircle(int(ro - 1), int(ro - 1), rad)
shom_im(cir)
谁能建议一种消除空白像素的方法?
我想你需要的是一个噪音过滤器。我认为高斯滤波器可以从许多实现中得到很好的结果。您可以找到过滤器列表 here。如果过于模糊:
- 保留您的第一个计算图像
- 计算过滤图像
- 将过滤图像中的固定像素复制到第一个计算图像
这是手写的粗略的平均滤波器:
cir_R = int(Ro*2) # outer circle 2*r
inner_r = int(Ro - 0.5 - len(img)) # inner circle r
for i in range(1, cir_R-1):
for j in range(1, cir_R-1):
if cir[i][j] == 0: # missing pixel
dx = int(i-Ro)
dy = int(j-Ro)
pix_r2 = dx*dx + dy*dy # distance to center
if pix_r2 <= Ro*Ro and pix_r2 >= inner_r*inner_r:
cir[i][j] = (cir[i-1][j] + cir[i+1][j] + cir[i][j-1] +
cir[i][j+1])/4
shom_im(cir)
结果:
这基本上是在两个范围之间扫描,检查丢失的像素并用与其相邻的 4 个像素的平均值替换它们。在这个黑白色的情况下,它是全白的。
希望对您有所帮助!
你在填满你的圈子时遇到了问题,因为你从错误的方式接近了这个——完全是字面上的。
当映射从源到目标时,您需要填写目标 ,并将每个翻译后的像素映射到 source 图像中。这样一来,您就不会错过任何一个像素,同样,您永远不会多次绘制(或查找)一个像素。
以下有点rough-and-ready,仅作为概念示例。我首先编写了一些代码来绘制一个从上到下的实心圆。然后我添加了更多代码以删除中心部分(并添加了一个变量 Ri
,用于 "inner radius")。这导致了一个实心环,其中所有像素只绘制一次:从上到下,从左到右。
你到底怎么画环其实并不重要!一开始我用trig是因为我想到了re-using角度位,但是用Pythagorus'也可以做到,甚至用Bresenham's circle routine也可以。您需要记住的是,您迭代的是 target 行和列,而不是 source。这提供了实际的 x
、y
坐标,您可以将其输入重新映射过程。
完成上述工作后,我编写了三角函数来将 从 坐标转换为 处的像素 原始图像。为此,我创建了一个包含一些文本的测试图像:
这也是一件好事,因为在第一次尝试中我得到了两次文本(一次向左,一次向右)并进行了镜像——这需要一些小的调整。还要注意背景网格。我添加它是为了检查 'top' 和 'bottom' 线——最外层和最内层的圆——是否正确绘制。
运行 我的代码与此图像和 Ro
,Ri
在 100 和 50,我得到这个结果:
你可以看到三角函数使它从最右边的点开始,顺时针移动,并且图像的顶部指向外。所有这些都可以微调,但这样它会模仿您想要绘制图像的方向。
这是您 iris-image 的结果,使用 33
作为内半径:
这是一个很好的动画,展示了贴图的稳定性:
最后,那么,我的代码是:
import math as m
from PIL import Image
Ro = 100.0
Ri = 50.0
# img = [[1 for x in range(int(width))] for y in range(int(height))]
cir = [[0 for x in range(int(Ro * 2))] for y in range(int(Ro * 2))]
# image = Image.open('0vWEI.png')
image = Image.open('this-is-a-test.png')
# data = image.convert('RGB')
pixels = image.load()
width, height = image.size
def shom_im(img): # for showing data as image
list_image = [item for sublist in img for item in sublist]
new_image = Image.new("RGB", (len(img[0]), len(img)))
new_image.putdata(list_image)
new_image.save("result1.png","PNG")
new_image.show()
for i in range(int(Ro)):
# outer_radius = Ro*m.cos(m.asin(i/Ro))
outer_radius = m.sqrt(Ro*Ro - i*i)
for j in range(-int(outer_radius),int(outer_radius)):
if i < Ri:
# inner_radius = Ri*m.cos(m.asin(i/Ri))
inner_radius = m.sqrt(Ri*Ri - i*i)
else:
inner_radius = -1
if j < -inner_radius or j > inner_radius:
# this is the destination
# solid:
# cir[int(Ro-i)][int(Ro+j)] = (255,255,255)
# cir[int(Ro+i)][int(Ro+j)] = (255,255,255)
# textured:
x = Ro+j
y = Ro-i
# calculate source
angle = m.atan2(y-Ro,x-Ro)/2
distance = m.sqrt((y-Ro)*(y-Ro) + (x-Ro)*(x-Ro))
distance = m.floor((distance-Ri+1)*(height-1)/(Ro-Ri))
# if distance >= height:
# distance = height-1
cir[int(y)][int(x)] = pixels[int(width*angle/m.pi) % width, height-distance-1]
y = Ro+i
# calculate source
angle = m.atan2(y-Ro,x-Ro)/2
distance = m.sqrt((y-Ro)*(y-Ro) + (x-Ro)*(x-Ro))
distance = m.floor((distance-Ri+1)*(height-1)/(Ro-Ri))
# if distance >= height:
# distance = height-1
cir[int(y)][int(x)] = pixels[int(width*angle/m.pi) % width, height-distance-1]
shom_im(cir)
commented-out 行画了一个实心的白色圆环。注意这里和那里的各种调整以获得最佳结果。例如,distance
是从环的中心开始测量的,因此 returns 接近中心的低值和圆外的最大值。将其直接映射回目标图像将显示顶部 "inwards" 指向内孔的文本。所以我用 height - distance - 1
反转了这个映射,其中 -1
是让它再次从 0
映射到 height
。
distance
本身的计算也有类似的修复;没有调整 Ri+1
和 height-1
最内层或最外层的行都不会被绘制,这表明计算只差一个像素(这正是该网格的目的)。
在此示例中,我尝试做的是将图像环绕成一个圆圈,如下所示。
为了包裹图像,我使用三角函数简单地计算了 x,y 坐标。 问题是计算出的 X 和 Y 位置四舍五入使它们成为整数。这会导致在上面的包裹图像中看到空白像素。 x,y 位置必须是整数,因为它们是列表中的位置。
我在下面的代码中再次执行了此操作,但没有任何图像以便于查看。我所做的就是创建两个具有二进制值的数组,一个数组是黑色的,另一个是白色的,然后将一个包裹到另一个上。
代码的输出是。
import math as m
from PIL import Image # only used for showing output as image
width = 254.0
height = 24.0
Ro = 40.0
img = [[1 for x in range(int(width))] for y in range(int(height))]
cir = [[0 for x in range(int(Ro * 2))] for y in range(int(Ro * 2))]
def shom_im(img): # for showing data as image
list_image = [item for sublist in img for item in sublist]
new_image = Image.new("1", (len(img[0]), len(img)))
new_image.putdata(list_image)
new_image.show()
increment = m.radians(360 / width)
rad = Ro - 0.5
for i, row in enumerate(img):
hyp = rad - i
for j, column in enumerate(row):
alpha = j * increment
x = m.cos(alpha) * hyp + rad
y = m.sin(alpha) * hyp + rad
# put value from original image to its position in new image
cir[int(round(y))][int(round(x))] = img[i][j]
shom_im(cir)
我后来发现了中点圆算法,但结果更糟
from PIL import Image # only used for showing output as image
width, height = 254, 24
ro = 40
img = [[(0, 0, 0, 1) for x in range(int(width))]
for y in range(int(height))]
cir = [[(0, 0, 0, 255) for x in range(int(ro * 2))] for y in range(int(ro * 2))]
def shom_im(img): # for showing data as image
list_image = [item for sublist in img for item in sublist]
new_image = Image.new("RGBA", (len(img[0]), len(img)))
new_image.putdata(list_image)
new_image.show()
def putpixel(x0, y0):
global cir
cir[y0][x0] = (255, 255, 255, 255)
def drawcircle(x0, y0, radius):
x = radius
y = 0
err = 0
while (x >= y):
putpixel(x0 + x, y0 + y)
putpixel(x0 + y, y0 + x)
putpixel(x0 - y, y0 + x)
putpixel(x0 - x, y0 + y)
putpixel(x0 - x, y0 - y)
putpixel(x0 - y, y0 - x)
putpixel(x0 + y, y0 - x)
putpixel(x0 + x, y0 - y)
y += 1
err += 1 + 2 * y
if (2 * (err - x) + 1 > 0):
x -= 1
err += 1 - 2 * x
for i, row in enumerate(img):
rad = ro - i
drawcircle(int(ro - 1), int(ro - 1), rad)
shom_im(cir)
谁能建议一种消除空白像素的方法?
我想你需要的是一个噪音过滤器。我认为高斯滤波器可以从许多实现中得到很好的结果。您可以找到过滤器列表 here。如果过于模糊:
- 保留您的第一个计算图像
- 计算过滤图像
- 将过滤图像中的固定像素复制到第一个计算图像
这是手写的粗略的平均滤波器:
cir_R = int(Ro*2) # outer circle 2*r
inner_r = int(Ro - 0.5 - len(img)) # inner circle r
for i in range(1, cir_R-1):
for j in range(1, cir_R-1):
if cir[i][j] == 0: # missing pixel
dx = int(i-Ro)
dy = int(j-Ro)
pix_r2 = dx*dx + dy*dy # distance to center
if pix_r2 <= Ro*Ro and pix_r2 >= inner_r*inner_r:
cir[i][j] = (cir[i-1][j] + cir[i+1][j] + cir[i][j-1] +
cir[i][j+1])/4
shom_im(cir)
结果:
这基本上是在两个范围之间扫描,检查丢失的像素并用与其相邻的 4 个像素的平均值替换它们。在这个黑白色的情况下,它是全白的。
希望对您有所帮助!
你在填满你的圈子时遇到了问题,因为你从错误的方式接近了这个——完全是字面上的。
当映射从源到目标时,您需要填写目标 ,并将每个翻译后的像素映射到 source 图像中。这样一来,您就不会错过任何一个像素,同样,您永远不会多次绘制(或查找)一个像素。
以下有点rough-and-ready,仅作为概念示例。我首先编写了一些代码来绘制一个从上到下的实心圆。然后我添加了更多代码以删除中心部分(并添加了一个变量 Ri
,用于 "inner radius")。这导致了一个实心环,其中所有像素只绘制一次:从上到下,从左到右。
你到底怎么画环其实并不重要!一开始我用trig是因为我想到了re-using角度位,但是用Pythagorus'也可以做到,甚至用Bresenham's circle routine也可以。您需要记住的是,您迭代的是 target 行和列,而不是 source。这提供了实际的 x
、y
坐标,您可以将其输入重新映射过程。
完成上述工作后,我编写了三角函数来将 从 坐标转换为 处的像素 原始图像。为此,我创建了一个包含一些文本的测试图像:
这也是一件好事,因为在第一次尝试中我得到了两次文本(一次向左,一次向右)并进行了镜像——这需要一些小的调整。还要注意背景网格。我添加它是为了检查 'top' 和 'bottom' 线——最外层和最内层的圆——是否正确绘制。
运行 我的代码与此图像和 Ro
,Ri
在 100 和 50,我得到这个结果:
你可以看到三角函数使它从最右边的点开始,顺时针移动,并且图像的顶部指向外。所有这些都可以微调,但这样它会模仿您想要绘制图像的方向。
这是您 iris-image 的结果,使用 33
作为内半径:
这是一个很好的动画,展示了贴图的稳定性:
最后,那么,我的代码是:
import math as m
from PIL import Image
Ro = 100.0
Ri = 50.0
# img = [[1 for x in range(int(width))] for y in range(int(height))]
cir = [[0 for x in range(int(Ro * 2))] for y in range(int(Ro * 2))]
# image = Image.open('0vWEI.png')
image = Image.open('this-is-a-test.png')
# data = image.convert('RGB')
pixels = image.load()
width, height = image.size
def shom_im(img): # for showing data as image
list_image = [item for sublist in img for item in sublist]
new_image = Image.new("RGB", (len(img[0]), len(img)))
new_image.putdata(list_image)
new_image.save("result1.png","PNG")
new_image.show()
for i in range(int(Ro)):
# outer_radius = Ro*m.cos(m.asin(i/Ro))
outer_radius = m.sqrt(Ro*Ro - i*i)
for j in range(-int(outer_radius),int(outer_radius)):
if i < Ri:
# inner_radius = Ri*m.cos(m.asin(i/Ri))
inner_radius = m.sqrt(Ri*Ri - i*i)
else:
inner_radius = -1
if j < -inner_radius or j > inner_radius:
# this is the destination
# solid:
# cir[int(Ro-i)][int(Ro+j)] = (255,255,255)
# cir[int(Ro+i)][int(Ro+j)] = (255,255,255)
# textured:
x = Ro+j
y = Ro-i
# calculate source
angle = m.atan2(y-Ro,x-Ro)/2
distance = m.sqrt((y-Ro)*(y-Ro) + (x-Ro)*(x-Ro))
distance = m.floor((distance-Ri+1)*(height-1)/(Ro-Ri))
# if distance >= height:
# distance = height-1
cir[int(y)][int(x)] = pixels[int(width*angle/m.pi) % width, height-distance-1]
y = Ro+i
# calculate source
angle = m.atan2(y-Ro,x-Ro)/2
distance = m.sqrt((y-Ro)*(y-Ro) + (x-Ro)*(x-Ro))
distance = m.floor((distance-Ri+1)*(height-1)/(Ro-Ri))
# if distance >= height:
# distance = height-1
cir[int(y)][int(x)] = pixels[int(width*angle/m.pi) % width, height-distance-1]
shom_im(cir)
commented-out 行画了一个实心的白色圆环。注意这里和那里的各种调整以获得最佳结果。例如,distance
是从环的中心开始测量的,因此 returns 接近中心的低值和圆外的最大值。将其直接映射回目标图像将显示顶部 "inwards" 指向内孔的文本。所以我用 height - distance - 1
反转了这个映射,其中 -1
是让它再次从 0
映射到 height
。
distance
本身的计算也有类似的修复;没有调整 Ri+1
和 height-1
最内层或最外层的行都不会被绘制,这表明计算只差一个像素(这正是该网格的目的)。