I wrote a program for analyzing pictures. The problem I’m having is very slow processing time for medium images like (800×800). And I think the root of the problem for this is the ‘for loop’ where i complete my numpy array with values. The main task for the program is to count how many times does x intensity show in each color channel and then plot them in histogram. For example in the end we will be able to see how many times do we get intensity 200 in red channel and so on.
My code is probably very hard to read but i tried to add comments to make things easier.
from PIL import Image import numpy as np from IPython import get_ipython get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt #Load an image fname = 'picture1.jpg' image_grey = Image.open(fname).convert("L") image_rgb = Image.open(fname).convert("RGB") arrey_grey = np.asarray(image_grey) arrey_rgb = np.asarray(image_rgb) #Get image size with Image.open('picture1.jpg') as img: width, height = img.size size = width * height #Plot the uploaded image both grey and rgb plt.imshow(arrey_grey, cmap='gray') plt.show() plt.imshow(arrey_rgb) plt.show() #Define numpy arrey for each color channel matrix_red = np.zeros((256, 4),dtype = object) matrix_red[:,1] = int(0) matrix_red[:,2:] = float(0) matrix_green = np.zeros((256, 4),dtype = object) matrix_green[:,1] = int(0) matrix_green[:,2:] = float(0) matrix_blue = np.zeros((256, 4),dtype = object) matrix_blue[:,1] = int(0) matrix_blue[:,2:] = float(0) # Completing first column with 0-255 for i in range(256): matrix_red[i] = i matrix_green[i] = i matrix_blue[i] = i # Counting intensity for each color channel for i in range(width): Matrix_Width = arrey_rgb[i] for i in range(height): Matrix_Height = Matrix_Width[i] Red_Value = Matrix_Height Green_Value = Matrix_Height Blue_Value = Matrix_Height for i in range(256): if (matrix_red[i] == Red_Value): matrix_red[i] = matrix_red[i] + 1 if (matrix_green[i] == Green_Value): matrix_green[i] = matrix_green[i] + 1 if (matrix_blue[i] == Blue_Value): matrix_blue[i] = matrix_blue[i] + 1 # Data for task ahead Hx = 0 for i in range(256): matrix_red[i] = matrix_red[i] / size Hx = (matrix_red[i] + matrix_red[i]) + Hx matrix_red[i] = Hx #Plotting results Frequencie_Red = np.zeros((256, 1),dtype = object) Frequencie_Red[:,0] = int(0) Frequencie_Green = np.zeros((256, 1),dtype = object) Frequencie_Green[:,0] = int(0) Frequencie_Blue = np.zeros((256, 1),dtype = object) Frequencie_Blue[:,0] = int(0) Intensity = np.zeros((256, 1),dtype = object) Intensity[:,0] = int(0) for i in range(256): Frequencie_Red[i] = matrix_red[i] Frequencie_Green[i] = matrix_green[i] Frequencie_Blue[i] = matrix_blue[i] for i in range(256): Intensity[i] = i pos = Intensity width = 1.0 ax = plt.axes() ax.set_xticks(pos + (width / 2)) ax.set_xticklabels(Intensity) plt.bar(pos, Frequencie_Red, width, color='r') plt.show() plt.bar(pos, Frequencie_Green, width, color='g') plt.show() plt.bar(pos, Frequencie_Blue, width, color='b') plt.show()
Any help will be greatly appreciated.
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!