python программа для выцветания изображения в радиальном направлении - PullRequest
0 голосов
/ 29 февраля 2020

Я пытаюсь написать программу, которая выцветает изображение в радиальном направлении. это означает, что по мере удаления от центра изображения пиксели становятся черными. Для этого я написал пять различных функций:


center: returns coordinate pair (center_y, center_x) of the image center.
radial_distance: returns for image with width w and height h an array with shape (h,w), where the number at index (i,j) gives the euclidean distance from the point (i,j) to the center of the image.
scale: returns a copy of the array 'a' (or image) with its elements scaled to be in the given range.
radial_mask: takes an image as a parameter and returns an array with same height and width filled with values between 0.0 and 1.0.
radial_fade: returns the image multiplied by its radial mask.

Программа:


import numpy as np
import matplotlib.pyplot as plt

def center(a):
    y, x = a.shape[:2]
    return ((y-1)/2,(x-1)/2)   # note the order: (center_y, center_x)

def radial_distance(b):
    h, w = b.shape[:2]
    y, x = center(b)
    o = b[:h,:w,0]
    for i in range(h):
        for j in range(w):
            o[i,j] = np.sqrt((y-i)**2 + (x-j)**2)
    return o

def scale(c, tmin=0.0, tmax=1.0):

    """Returns a copy of array 'a' with its values scaled to be in the range
[tmin,tmax]."""
    mini, maxi = c.min(), c.max()
    if maxi == 0:
        return 0
    else:

        m = (tmax - tmin)/(maxi - mini)

        f = tmin - m*mini
        return c*m + f

def radial_mask(d):
    f = radial_distance(d)
    g = scale(f, tmin=0.0, tmax=1.0)
   # f = g[:,:,0]
    n = 1.0 - g
    return n

def radial_fade(l):
    f, g = l.shape[:2]
    q = l[:f,:g,0]
    return q * radial_mask(l)
image = plt.imread("src/painting.png")
fig, ax = plt.subplots(3)
masked = radial_mask(ima)
faded = radial_fade(ima)
ax[0].imshow(ima)
ax[1].imshow(masked)
ax[2].imshow(faded)
plt.show()

что-то не так в код, поскольку он не выполняет ожидаемую работу.

1 Ответ

0 голосов
/ 29 февраля 2020

Одна проблема заключается в том, что в

o = b[:h,:w,0]

вы используете ту же точность, что и для изображения, которое может быть целым числом (например, uint8).

Вы должны использовать, например,

o = np.zeros((h, w), np.float32)
...