如何在python中操纵图像来模拟黄斑变性

2024-09-30 18:34:36 发布

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

我试图找到一个好的软件包或算法来修改图像,将图像的中心向外推,以模拟黄斑变性。我发现最好的方法是图像切片器包装和分裂成4块图像,推动内角和缝合图像回来。但是,包的连接方法不起作用,文档也不清楚。有人有能做这件事的包裹吗?你知道吗

同时,我也在尝试将图像的外部推入,以创建隧道视觉。你知道吗

(对于这两种情况,我仍在努力保留图像,尽管“倾斜”是可以的,但我正在努力防止图像丢失。)

我写的一些代码

import image_slicer
#split image into 4 pieces
image_slicer.slice('piegraph.jpeg',4) #just a simple sample img

#code to resize corners
#I can figure this out later.

#stitch images back
tiles = ("pie_01_01.png","pie_01_02.png","pie_02_01.png","pie_02_02.png")
image_slicer.join(tiles)

Tags: 方法文档图像image算法png切片中心
1条回答
网友
1楼 · 发布于 2024-09-30 18:34:36

你可以使用opencv和numpy来做你想做的事情。你知道吗

如果我理解正确的话,你需要的是一个映射,它将原始图像映射为距离图像中心的函数。你知道吗

对于“黑洞”中的所有像素,你希望它们是黑色的,而对于其他所有像素,你希望它们聚在一起。你知道吗

所以如果我们把原始图像看作:

Before

你想要的结果是:

After

以下代码适用于此。您需要使用的参数是

黑洞的半径

因子-改变“聚束”的数量太小,所有的像素也将映射到黑色太大,他们将不会聚束。你知道吗

import cv2
import numpy as np
import math

# Read img
img = cv2.imread('earth.jpg')
rows,cols,ch = img.shape

# Params
FACTOR = 75
RBlackHole = 10

# Create a 2d mapping between the image and a new warp
smallSize = min(rows,cols)
xMap = np.zeros((rows,cols), np.float32)
yMap = np.zeros_like(xMap)
for i in range(rows):
    for j in range(cols):

        # Calculate the distance of the current pixel from the cneter of the image
        r = math.sqrt((i-rows/2)*(i-rows/2) + (j-cols/2)*(j-cols/2))

        # If the pixles are in the radius of the black hole
        # mapped them to a location outside of the image.
        if r <= RBlackHole:
            xMap[i, j] = rows*cols
            yMap[i, j] = rows*cols
        else:

            # Mapped the pixels as a function of the distance from the center.
            # The further thay are the "buncher thay will be"
            xMap[i, j] = (r-RBlackHole)*(j - cols/2)/FACTOR + cols/2
            yMap[i, j] = (r-RBlackHole)*(i - rows/2)/FACTOR + rows/2



# Applay the remmaping
dstImg = cv2.remap(img,xMap,yMap,cv2.INTER_CUBIC)

# Save output image
cv2.imwrite("blackHoleWorld.jpg", dstImg)

相关问题 更多 >