您可以使用opencv和nummpy來做你想做的事情。
如果我正確理解ypu需要的是一種映射,它將原始圖像作爲距圖像中心距離的函數進行映射。
對於「黑洞」內的所有像素,您希望黑色,並且您希望所有其他像素都聚集在一起。
所以,如果我們把原始圖像是:
你正在尋找的結果是這樣的:
下面的代碼這劑。你需要玩的參數
RBlackHole - 您的黑洞
因素半徑 - 更改的「羣聚」太小的數量和所有像素也將被映射到黑色太大,他們會不要揉成團。
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)
最終我想要做的是推出圖像的中心像素,所以模擬黑洞和周圍的圖像是像素聚集在一起。 –