Last active
May 12, 2020 06:51
-
-
Save samuelsaari/4eb6017e3cf0247bbc400e249b066844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Part03-e12 radial_fade: | |
import os | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# comment this when submitting | |
#uncomment when testing locally | |
os.chdir("C:/Users/fnsuc/OneDrive - University of Helsinki/Tilasto/python_data_analysis/hy-data-analysis-with-python-spring-2020/part03-e12_radial_fade") | |
print(os. getcwd()) | |
# def center(a): | |
# return (0,0) # note the order: (center_y, center_x) | |
a= np.random.randint(0, 5, size=(3, 3)) | |
#print('a',a) | |
def center(a): | |
painting=a.copy() | |
#print(painting.shape) | |
y=(painting.shape[1]-1)/2 | |
x=(painting.shape[0]-1)/2 | |
return (x,y) # OBS! check order!!!!! | |
print(a) | |
print('shape', a.shape) | |
print('center', center(a)) | |
#print('center_painting', center("src/painting.png")) | |
# print(center(plt.imread("src/painting.png"))) | |
# def radial_distance(a): | |
# return np.array([[]]) | |
def radial_distance(a): | |
c = center(a) | |
#d=np.asarray(c).reshape(2,1) | |
dist = np.zeros((a.shape[0],a.shape[1])) | |
for h in range(dist.shape[0]): | |
for w in range(dist.shape[1]): | |
dist[h, w] = np.sqrt(np.sum((np.array([h, w])-c)**2)) | |
return dist | |
#print('radial distance', radial_distance(a)) | |
def scale(a, tmin=0.0, tmax=1.0): | |
if a.min()==a.max(): | |
return a*0 # edit: was "return a", now corrected and should work | |
else: | |
return ((a-a.min())/(a.max()-a.min())) | |
#print('scale', scale(a)) | |
def radial_mask(a): | |
#frame = np.zeros((a.shape[0],a.shape[1])) | |
img2 = a.copy() | |
a_dist = radial_distance(img2) | |
a_scaled = scale(a_dist) | |
mask = 1-a_scaled | |
return mask | |
#print('mask',radial_mask(a)) | |
#print(radial_mask(a)) | |
#print(radial_mask(a)[:,:,np.newaxis]) | |
def radial_fade(a): | |
img2 = a.copy() | |
m=radial_mask(img2).reshape(img2.shape[0],img2.shape[1],1) | |
r=img2*m | |
#img2=img2*radial_mask(img2)[:,:,np.newaxis] | |
#img2=img2.reshape((img2.shape[0],img2.shape[1])) | |
return r | |
#print('fade', radial_fade(a)) | |
# print('a',a) | |
# print('shape',a.shape) | |
def main(): | |
filename="src/painting.png" | |
#filename="src/orange.png" | |
image=plt.imread(filename) | |
#image=image[0:1, 0:5, :] | |
print('mask',radial_mask(image)) | |
fig, ax = plt.subplots(3,1) | |
ax[0].imshow(image) | |
ax[1].imshow(radial_mask(image)) | |
ax[2].imshow(radial_fade(image)) | |
plt.show(fig) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment