jeudi 3 mars 2016

If statement to cycle through individual elements in array in Python

In my code, I am trying to import a grayscale image (2D array) and then solve for the optical density (OD) based off an empirical formula I came up with. Then, I converted it into an RGB image.

My problem is I am trying to run each individual element of the image array into an if statement. It does not enter the statement though. What I want to do is increase the intensity of each individual element or pixel value in R, G, and B based on what condition is met with the optical density. Say if it has an OD value that falls between b and c, it adds [128,0,0] to each element that satisfies that criteria.

t = Image.open("IMG_1.jpg") #grayscale image
f = array(t) #Convert test image into an array

OD = 0.51*((f-22.08)/(176.09-f))**(1./-1.519) #Empirical Rodbard formula
OD[np.isnan(OD)] = 0

def to_rgb5(im):
    OD.resize((OD.shape[0], OD.shape[1], 1))
    return np.repeat(OD.astype(np.uint8), 3, 2)

cmap = plt.get_cmap('jet')

rgba_img = cmap(OD)
rgb_img = np.delete(rgba_img, 3, 2)

a = 0.08
b = 0.11
c = 0.15

if np.all(OD < a):
        background_noise = rgb_img
    if np.all(OD < b):
        small = rgb_img + [128, 0, 0]
    else np.all(OD >= c):
        large = rgb_img + [0, 0, 128]


Red = f + small
Green = f
Blue = f + large

Aucun commentaire:

Enregistrer un commentaire