<jupyter_start><jupyter_text>Cell 1 - Required Modules<jupyter_code>import matplotlib.pyplot as plt
import cv2
import pandas as pd
import numpy as np
from scipy.misc import imread
from PIL import Image
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Next Two Cells - Import and displays unmodified image<jupyter_code>img = Image.open('D:/CW Spectral Eval - Feb 2nd/Canon_Photos/File038.tiff')
plt.imshow(img)
plt.grid(b=True, which ='major', color='r', linewidth = 2)<jupyter_output><empty_output><jupyter_text>Next Three Cells - The next three cells is a manual process.  It selects the region of interest to analyze, and reduces the pixel size so that subsequent analysis may be performed within a reasonable time period (less than 5 minutes).<jupyter_code>img = img.crop((1250, 1000, 1750, 1250))
plt.imshow(img)
plt.grid(b=True, which ='major', color='r', linewidth =2)
img = img.crop((0, 0, 500, 50))
plt.imshow(img)
img = img.crop((0, 0, 500, 20))
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>Next Cell - saves reduced sized file for the analysis below, and reads the new (smaller file) as an array of float numbers<jupyter_code>img.save('C:/Users/Timothy/Dropbox/File _requests/Hawaii_Astronomy_Conf_Presentation/img.jpg')
img = imread('C:/Users/Timothy/Dropbox/File _requests/Hawaii_Astronomy_Conf_Presentation/img.jpg').astype(np.float32)<jupyter_output><empty_output><jupyter_text>Next Two Cells - establishes the r,g,b array as one r array, one g array, and one b array<jupyter_code>def rgbw(img):
    red, green, blue = np.zeros(shape=(img.shape[0], img.shape[1], 3)), np.zeros(shape=(img.shape[0], img.shape[1], 3)), np.zeros(shape=(img.shape[0], img.shape[1], 3))
    red[:,:,0], green[:,:,1], blue[:,:,2] = img[:,:,0], img[:,:,1], img[:,:,2]
    blue_, green_, red_ = pd.DataFrame(blue[:,:,2]), pd.DataFrame(green[:,:,1]),pd.DataFrame(red[:,:,0])
    return red_, green_ , blue_, red, green, blue

red_, green_, blue_, red, green, blue = rgbw(img)
red = red.astype(np.uint8)
green = green.astype(np.uint8)
blue = blue.astype(np.uint8)<jupyter_output><empty_output><jupyter_text>Next Two Cells - Crops the data for values greater than or equal to 30, and less than or equal to 240 (to eliminate saturated pixels, or pixels closer to the noise floor)  AND displays the results<jupyter_code>red[red < 30] = 0
red[red > 240] = 0
green[:,:,1][red[:,:,0] == 0] = 0
blue[:,:,2][red[:,:,0] == 0] = 0

blue[blue < 30] = 0
blue[blue > 240] = 0
green[:,:,1][blue[:,:,2] == 0] = 0
red[:,:,0][blue[:,:,2] == 0] = 0

green[green < 30] = 0
green[green > 240] = 0
red[:,:,0][green[:,:,1] == 0] = 0
blue[:,:,2][green[:,:,1] == 0] = 0

img = red + green + blue

np.amax(green), np.amax(red), np.amax(blue), np.amax(img)
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>Next Cell - Test of data; does not contribute to the program<jupyter_code># plt.hist(red.ravel(),40,[0,280], color='r', alpha =0.4); plt.ylim(0,200); plt.show()
# plt.hist(green.ravel(),40,[0,280], color='g', alpha =0.4); plt.ylim(0,250); plt.show()
# plt.hist(blue.ravel(),40,[0,280], color='b', alpha =0.4); plt.ylim(0,800); plt.show()

np.average(red), np.average(green), np.average(blue)
np.sum(red), np.sum(green), np.sum(blue), len(red), len(green), len(blue)<jupyter_output><empty_output><jupyter_text>Next Two Cells - Saves and reads the file to take advantage of the CV2 resources.<jupyter_code>cv2.imwrite('D:/Python_Database/test_thresh1.png', red)
cv2.imwrite('D:/Python_Database/test_thresh2.png', green)
cv2.imwrite('D:/Python_Database/test_thresh3.png', blue)
red = cv2.imread('D:/Python_Database/test_thresh1.png')
grn = cv2.imread('D:/Python_Database/test_thresh2.png')
blue = cv2.imread('D:/Python_Database/test_thresh3.png')

red = np.float64(red)
grn = np.float64(grn)
blue = np.float64(blue)

red.shape, grn.shape, blue.shape 
def four_channel(array):
    r_channel, g_channel, b_channel = cv2.split(array)
    a_channel = np.ones((array.shape[0], array.shape[1]))*0
    array = cv2.merge((r_channel, g_channel, b_channel, a_channel))
    return array

alp_red = four_channel(red)
alp_grn = four_channel(grn)
alp_blue = four_channel(blue)
alp_img = alp_red + alp_grn + alp_blue
def background(array,n):
    array[:,:,3][array[:,:,n] != 0] = 255
    return array

alp_red = background(alp_red,0)
alp_grn = background(alp_grn,1)
alp_blue = background(alp_blue,2)
np.amax(alp_red[:,:,0]), np.amax(alp_grn[:,:,1]), np.amax(alp_blue[:,:,2])
alp_img.shape
r, g, b = [], [], []

for i, row in enumerate(alp_img):
    for j, pixel in enumerate(row):
        tempr = pixel[0]
        tempg = pixel[1]
        tempb = pixel[2]
        
        r.append(tempr), g.append(tempg), b.append(tempb)
r1 = filter(lambda a: a != 0, r)
g1 = filter(lambda a: a != 0, g)
b1 = filter(lambda a: a != 0, b)
r2, g2, b2 = sum(r1)/len(r1), sum(g1)/len(g1), sum(b1)/len(b1) 
r2, g2, b2
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(8,5))
ax = fig.add_subplot(111, projection = '3d')

for i in range(len(r1)):
    ax.scatter(r1[i]/255, g1[i]/255, b1[i]/255, c=[r1[i]/255, g1[i]/255, b1[i]/255], lw=0, marker='o', alpha=1)

ax.scatter(r2/255, g2/255, b2/255, c=[r2/255,g2/255,b2/255], marker='o', s = 55, lw=0, alpha=1)

ax.set_xlim3d(0, 1)
ax.set_ylim3d(0, 1)
ax.set_zlim3d(0, 1)

ax.set_xlabel('r-Value')
ax.set_ylabel('g-Value')
ax.set_zlabel('b-Value')
# fig = plt.figure(figsize=(8,5))
# ax = fig.add_subplot(111, projection = '3d')

# for i in range(len(r1)):
#     ax.scatter(r1[i]/255, g1[i]/255, b1[i]/255, c=[r1[i]/255, g1[i]/255, b1[i]/255], lw=0, marker='o', alpha=0.09)

# ax.scatter(r2/255, g2/255, b2/255, c=[r2/255,g2/255,b2/255], marker='o', s = 55, lw=0, alpha=1)

# ax.set_xlim3d(0, 1)
# ax.set_ylim3d(0, 1)
# ax.set_zlim3d(0, 1)

# ax.set_xlabel('r-Value')
# ax.set_ylabel('g-Value')
# ax.set_zlabel('b-Value')
rt, gt, bt = r2/255, g2/255, b2/255
rt, gt, bt = np.arange(0, rt, rt/100), np.arange(0, gt, gt/100), np.arange(0, bt, bt/100)
z1 = np.zeros(len(rt))
rtz, gtz, btz=[rt]*len(rt),[gt]*len(rt),[bt]*len(rt)
fig = plt.figure(figsize=(8,5))
ax = fig.add_subplot(111, projection = '3d')

for i in range(len(r1)):
    ax.scatter(r1[i]/255, g1[i]/255, b1[i]/255, c=[r1[i]/255, g1[i]/255, b1[i]/255], lw=0, marker='o', alpha=.02)

for i in range(len(rt)):
    ax.scatter(rt[i], z1[i], z1[i], c=[rt[i], z1[i], z1[i]], s=8, marker='o', lw=0, alpha=.1)
    ax.scatter(z1[i] + max(rt), gt[i], z1[i], c=[z1[i], gt[i], z1[i]], s=10, marker='o', lw=0, alpha=.1)
    ax.scatter(z1[i] + max(rt), z1[i] + max(gt), bt[i], c=[z1[i], z1[i], bt[i]], s=10, marker='o', lw=0, alpha=1)
    ax.scatter(rt[i], gt[i], bt[i], c=[rt[i], gt[i], bt[i]], marker='o', lw=0, alpha=0)

ax.set_xlim3d(0, 1)
ax.set_ylim3d(0, 1)
ax.set_zlim3d(0, 1)

ax.set_xlabel('r-Value')
ax.set_ylabel('g-Value')
ax.set_zlabel('b-Value')
# r1lin, g1lin, b1lin =[],[],[]
# for i in range(len(r1)):
#     if r1[i] <= 0.0405:
#         tempr = r1[i]/12.92
#     else:
#         tempr = ((r1[i]+0.055)/(1.055))**2.4
        
#     if g1[i] <= 0.0405:
#         tempg = g1[i]/12.92
#     else:
#         tempg = ((g1[i]+0.055)/(1.055))**2.4
        
#     if b1[i] <= 0.0405:
#         tempb = b1[i]/12.92
#     else:
#         tempb = ((b1[i]+0.055)/(1.055))**2.4

#     r1lin.append(tempr), g1lin.append(tempg), b1lin.append(tempb)
    
# X,Y,Z = [],[],[]
# for i in range(len(r1)):
#     tempX = r1lin[i]  * 0.4124 + g1lin[i] * 0.3576 + b1lin[i] * 0.1805
#     tempY = r1lin[i] * 0.2126 + g1lin[i] * 0.7152 + b1lin[i] * 0.0722
#     tempZ = r1lin[i] * 0.0193 + g1lin[i] * 0.1192 + b1lin[i] * 0.9505
#     X.append(tempX), Y.append(tempY), Z.append(tempZ)
    
# X = filter(lambda a: a != 0, X)
# Y = filter(lambda a: a != 0, Y)
# Z = filter(lambda a: a != 0, Z)

# X = [(i/max(Y))*100 for i in X]
# Z = [(i/max(Y))*100 for i in Z]
# Y = [(i/max(Y))*100 for i in Y]

# max(X), max(Y), max(Z)
# x, y = [], []
# for i in range(len(X)):
#     tempx = X[i]/(X[i] + Y[i] + Z[i])
#     tempy = Y[i]/(X[i] + Y[i] + Z[i])
#     x.append(tempx), y.append(tempy)
# x_avg, y_avg = sum(x)/len(x), sum(y)/len(y)
# x_avg, y_avg
# CIE_1931 = pd.read_csv('D:\Python_Database\CIE1931_1nm.csv')
# CIE_1931 = CIE_1931.rename(columns = {CIE_1931.columns[0]:'x', CIE_1931.columns[1]:'y'})
# plt.plot(CIE_1931['x'], CIE_1931['y'], c='k', linewidth = 0.5, label='Hawaii File038.tiff')
# plt.gca().set_aspect('equal', adjustable='box')
# plt.grid(); plt.ylim(-.1,.9); plt.xlim(-.1,.9)
# plt.xlabel('x'); plt.ylabel('y')
# plt.legend()

# for i in range(len(x)):
#     plt.scatter(x[i],y[i], c=[r1[i]/255, g1[i]/255, b1[i]/255], lw=0, alpha=.02)

# plt.scatter(x_avg, y_avg, c=[r2/255,g2/255,b2/255], lw=0, s=55, alpha=1)
# plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21977770138650166, 'fraction_numerical': 0.07516901569840724, 'mean_word_length': 4.566326530612245, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '80539', 'license': 'MIT', 'n_tokens_mistral': 4302, 'n_tokens_neox': 3987, 'n_words': 773, 'path': 'src/notebooks/RGB Analysis.ipynb', 'repo_name': 'probinso/color-pollution', 'size': '8727'}