Convert Numpy 3D array to 3 R,G,B hex-strings - python

I have large lists containing RGB values for pictures.
I use numpy to create patterns and/or pillow to load pictures and convert them to 3D-numpy arrays (int32). Now i want to restructure my array into hex-strings in a weird way:
Three hex-strings for R, G, B in this structure: '0000FFFF', where the first 4 characters always have to be zero, then 2 characters represent pixel n+1 and the last 2 cahracters represent pixel n.
I already have done this with a code which takes too long for larger images and i require some improvement. What i got so far:
import numpy
import numpy.matlib
#from matplotlib.colors import rgb2hex
import time
def pairwise(iterable):
"""Create a paired-list from a list."""
a = iter(iterable)
return zip(a, a)
def test(imgSize=[480,640], brightness=[255,255,255]):
#generate pattern
startPattern = time.time()
patternDescription = 'Stripe Test'
pattern = numpy.zeros((imgSize[0], imgSize[1], 3))
line = (numpy.r_[:imgSize[1]]%255)/255
colorChR = numpy.matlib.repmat(line, imgSize[0], 1)
colorChG = numpy.matlib.repmat(line, imgSize[0], 1)
colorChB = numpy.matlib.repmat(line, imgSize[0], 1)
colorChR[:, :] = 0
colorChR[:, 0:60] = 1
colorChG[:, :] = 0
colorChG[:, 0:60] = 1
colorChB[:, :] = 0
colorChB[:, 0:60] = 1
pattern[:, :, 0] = colorChR
pattern[:, :, 1] = colorChG
pattern[:, :, 2] = colorChB
stopPattern = time.time()
print('TIME: Pattern generation: ' + str(round(stopPattern-startPattern,3)) + ' s. ')
# first reshape
startReshape = time.time()
pattern[:, :, 0] = pattern[:, :, 0]*brightness[0] # red brightness multiplicator
pattern[:, :, 1] = pattern[:, :, 1]*brightness[1] # green brightness multiplicator
pattern[:, :, 2] = pattern[:, :, 2]*brightness[2] # blue brightness multiplicator
img = pattern.astype(int)
# IDEALLY I WANT TO CHANGE THE CODE ONLY FROM HERE ON
# redValues = pattern[:,:,0].astype(int)
# greenValues = pattern[:,:,1].astype(int)
# blueValues = pattern[:,:,2].astype(int)
# test = ("0000" + ("{:0>2X}" * len(redValues))).format(*tuple(redValues[::-1]))
# numpy.set_printoptions(formatter={'int':hex})
# #test = [ rgb2hex(img[i,:]) for i in range(img.shape[0]) ]
# rgb2hex = lambda r,g,b: '%02X%02X%02X' %(r,g,b)
# test = [ rgb2hex(*img[i,:]) for i in range(img.shape[0]) ]
# # img = numpy.array2string(img, formatter = {'int':lambda img: hex(img)})
imgReshape = numpy.reshape(img, (1, imgSize[0]*imgSize[1]*3)) #necessary?
redValues = imgReshape[0][0::3] #red values (0, 3, 6, ..)
greenValues = imgReshape[0][1::3] #green values (1, 4, 7, ..)
blueValues = imgReshape[0][2::3] #blue values (2, 5, 8, ..)
stopReshape = time.time()
print('TIME: Reshape into colors: ' + str(round(stopReshape-startReshape,3)) + ' s. ')
redString = ''
greenString = ''
blueString = ''
outData = dict()
startString = time.time()
for i, j in pairwise(redValues):
tempRed = "0000%02X%02X" % (int(j), int(i))
redString += tempRed
for i, j in pairwise(greenValues):
tempGreen = "0000%02X%02X" % (int(j), int(i))
greenString += tempGreen
for i, j in pairwise(blueValues):
tempBlue = "0000%02X%02X" % (int(j), int(i))
blueString += tempBlue
outData['red'] = redString
outData['green'] = greenString
outData['blue'] = blueString
stopString = time.time()
print('TIME: String formatting: ' + str(round(stopString-startString, 3)) + ' s')
print('DATATEST: First 200 red chars: ' + str(outData['red'][0:200]))
print('DATATEST: First 200 green chars: ' + str(outData['green'][0:200]))
print('DATATEST: First 200 blue chars: ' + str(outData['blue'][0:200]))
#return outData

Try to use numpy array instead:
redValues = np.random.randint(0, 255, (10, 2))
red = np.array(redValues).reshape(-1, 2)
red_channel = (red[:, 1] << 8) + red[:, 0]
redString = ''.join(map(lambda val: f'0000{val:04x}', red_channel))

Related

Find remaining line from rectangle in OpenCV

I'm detecting a rectangle with known dimensions in OpenCV. I've written code - which works - for detecting all four lines, but the furthest line is difficult to detect and usually causes problems when it comes time for warp the perspective.
My question (which I've looked into, but haven't found an answer for), is whether, theoretically, having 3 lines (angles and intercept) is enough to approximate the final line.
EDIT: I was asked to include my current code. Here it is:
import numpy as np
import cv2
from sklearn.cluster import AgglomerativeClustering
# find intersection of lines
def line_intersect(m1, b1, m2, b2):
if m1 == m2:
print ("These lines are parallel!!!")
return None
x = int((b2 - b1) / (m1 - m2))
y = int(m1 * x + b1)
return [x,y]
# method specs
width_error = 10
height_error = 10
kernel_size = 7
kernel_dilate = np.ones((1, 1), 'uint8')
kernel = np.ones((5, 5), 'uint8')
# read and process image
img = cv2.imread('assets/game-frames/hard-m-2019-124-1200.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
edges = cv2.Canny(blur_gray, 0, 100, apertureSize = 3)
dilate_img = cv2.dilate(edges, kernel_dilate, iterations=3)
closing = cv2.morphologyEx(dilate_img, cv2.MORPH_CLOSE, kernel)
# find hough lines
lines = cv2.HoughLinesP(closing, 2, np.pi/180, 100, minLineLength=20, maxLineGap=10)
lines_np = np.reshape(lines, (np.int32(lines.size/4), 4))
lines_np = lines_np[(lines_np[:, 2] - lines_np[:, 0]) != 0]
lines_np = np.c_[lines_np, (lines_np[:, 3] - lines_np[:, 1])/(lines_np[:, 2] - lines_np[:, 0])]
lines_np = np.c_[lines_np, np.sqrt((lines_np[:, 3] - lines_np[:, 1])**2 + (lines_np[:, 2] - lines_np[:, 0])**2)]
# find lengthwise lines
lengthwise_lines = lines_np[(abs(lines_np[:, 4]) < 3.5) & (abs(lines_np[:, 4]) > 1.20) & (lines_np[:, 5] > 100)]
if lengthwise_lines[:, 4].size < 2: print('No lengthwise lines!')
ward_length = AgglomerativeClustering(n_clusters = None, distance_threshold = 0.50, linkage = "ward").fit(abs(lengthwise_lines[:, 4].reshape(-1,1)))
lengthwise_lines = np.c_[lengthwise_lines, ward_length.labels_]
length_clusts = np.bincount(ward_length.labels_).argsort()[-2:]
lengthwise_lines = lengthwise_lines[np.isin(ward_length.labels_, length_clusts)]
# find extremities
x_max = np.max(lengthwise_lines[:,[0,2]].reshape(-1,1))
x_min = np.amin(lengthwise_lines[:,[0,2]])
y_coords = lengthwise_lines[:, [1, 3]].reshape(-1,1)
ward = AgglomerativeClustering(n_clusters = None, distance_threshold = 10, linkage = "ward").fit(y_coords)
want_clusts = np.bincount(ward.labels_).argsort()[-2:]
y_ext1 = np.median(y_coords[np.isin(ward.labels_, want_clusts[0])])
y_ext2 = np.median(y_coords[np.isin(ward.labels_, want_clusts[1])])
y_max = max(y_ext1, y_ext2)
y_min = min(y_ext1, y_ext2)
# find outer lengthwise lines
length_want = ward_length.labels_[np.isin(ward_length.labels_, length_clusts)][np.argmin(abs(lengthwise_lines[:,4]))]
lengthwise_lines = lengthwise_lines[lengthwise_lines[:, 6] == length_want]
lengthwise_lines = np.c_[lengthwise_lines, (lengthwise_lines[:,1] - (lengthwise_lines[:,4]*lengthwise_lines[:,0]))]
lengthwise_lines = np.c_[lengthwise_lines, ((np.max(lengthwise_lines[:,1]) - lengthwise_lines[:,5])/lengthwise_lines[:,4])]
# subset left and right outer lengthwise lines
l_left = lengthwise_lines[lengthwise_lines[:,4] < 0]
l_right = lengthwise_lines[lengthwise_lines[:,4] > 0]
# find top and bottom lines
widthwise_lines = lines_np[(abs(lines_np[:, 4]) < 0.10)]
widthwise_lines = np.c_[widthwise_lines, (widthwise_lines[:,1] - (widthwise_lines[:,4]*widthwise_lines[:,0]))]
bottom_lines = widthwise_lines[np.where((np.amin(widthwise_lines[:,[1,3]], axis = 1) >= y_min - height_error) & (np.amin(widthwise_lines[:,[1,3]], axis = 1) <= y_min + height_error) & (np.amin(widthwise_lines[:,[0,2]], axis = 1) >= x_min - width_error) & (np.amax(widthwise_lines[:,[0,2]], axis = 1) <= x_max + width_error))[0]]
top_lines = widthwise_lines[np.where((np.amax(widthwise_lines[:,[1,3]], axis = 1) >= y_max - height_error) & (np.amax(widthwise_lines[:,[1,3]], axis = 1) <= y_max + height_error) & (np.amin(widthwise_lines[:,[0,2]], axis = 1) >= x_min - width_error) & (np.amax(widthwise_lines[:,[0,2]], axis = 1) <= x_max + width_error))[0]]
# if no lines found for any border, stop
if top_lines.size == 0 or bottom_lines.size == 0 or l_left.size == 0 or l_right.size == 0: print('No Lengthwise lines!')
# take median of left outer lengthwise lines
l_left_b = np.median(l_left[:,7])
l_left_m = np.median(l_left[:,4])
# take median of right outer lengthwise lines
l_right_b = np.median(l_right[:,7])
l_right_m = np.median(l_right[:,4])
# take median of top widthwise lines
top_lines_b = np.median(top_lines[:,6])
top_lines_m = np.median(top_lines[:,4])
# take median of bottom widthwise lines
bottom_lines_b = np.median(bottom_lines[:,6])
bottom_lines_m = np.median(bottom_lines[:,4])
# find interesction of lines
int_pt1 = line_intersect(l_left_m, l_left_b, bottom_lines_m, bottom_lines_b)
int_pt2 = line_intersect(l_right_m, l_right_b, bottom_lines_m, bottom_lines_b)
int_pt3 = line_intersect(l_left_m, l_left_b, top_lines_m, top_lines_b)
int_pt4 = line_intersect(l_right_m, l_right_b, top_lines_m, top_lines_b)
# draw intersections
cv2.circle(img, int_pt1, 3, (0, 255, 0), -1)
cv2.circle(img, int_pt3, 3, (255, 255, 0), -1)
cv2.circle(img, int_pt2, 3, (0, 255, 0), -1)
cv2.circle(img, int_pt4, 3, (255, 255, 0), -1)
# show image
cv2.imshow('frame diff ', img)
cv2.waitKey(0)
Original image:

ValueError in NumPy: shapes not aligned

The code below shows error "ValueError: shapes (400,16,1) and (16,16) not aligned: 1 (dim 2) != 16 (dim 0)". How can I solve this problem? I want to create an image recognition algorithm using numpy only. Test images are 20*20 px sized. (sorry for my English, I speak Russian)
from numpy import exp, array, random, dot, squeeze, asarray
from PIL import Image
images = []
for k in range(8):
im = Image.open(f'learn\\yes\\{k + 1}.png', 'r')
a = list(im.getdata())
pixel_values = []
for i in a:
pixel_values.append((i[0] + i[1] + i[2] / 3) / 1000)
images.append(pixel_values)
im = Image.open(f'learn\\no\\{k + 1}.png', 'r')
a = list(im.getdata())
pixel_values = []
for i in a:
pixel_values.append((i[0] + i[1] + i[2] / 3) / 1000)
images.append(pixel_values)
im = Image.open(f'test\\1.png', 'r')
a = list(im.getdata())
pixel_values = []
for i in a:
pixel_values.append((i[0] + i[1] + i[2] / 3) / 1000)
print(*images, sep='\n', end='\n\n')
print(pixel_values)
# print(pixel_values3)
training_set_inputs = array([images])
training_set_outputs = array([[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]]).T
random.seed(1)
print('processing...')
synaptic_weights = squeeze(asarray(3 * random.random((400, 1)) - 1))
for iteration in range(2):
print(f'starting iteration {iteration + 1}')
output = 1 / (1 + exp(-(dot(training_set_inputs, synaptic_weights))))
synaptic_weights += dot(training_set_inputs.T, (training_set_outputs - output) * output * (1 - output))
print('done!')
a = 1 / (1 + exp(-(dot(array(pixel_values), synaptic_weights))))[0]
print(a)
if a > 0.6:
print('yes')
else:
print('no')
I've solved the problem. The problem was here:
training_set_inputs = array([images])
instead of
training_set_inputs = array(images)

convert image to rgb array using PIL and manually convert it to CMYK array

I'm trying to convert an image to cmyk array manually and reconstruct the image but i didn't get any image then i decide to separate c,m,y,k and display it but there is no proper image .so i just tried to save every data in file for verification,i can't find any error in that data.can any one tell me why this happens and what is the error that i done here.i post my entire code below.
from PIL import Image
import numpy as np
im = Image.open('idcard.jpg').convert('RGB')
np_image = np.array(im)
num_list = np_image.tolist()
print( len(num_list))
str1 =str(num_list)
print( len(str1))
f=open("idcardforgb.txt","w")
f.write(str1)
f.close()
cyan = 0
magenta = 0
yellow = 0
key = 0
cmyk_scale = 255
t=np.shape(np_image)
print (t)
i=(int)(t[0])
j=(int)(t[1])
k=(int)(t[2])
c_final=[[[0 for f in range(4)]for g in range(j)]for h in range(i)]
for z in range(i):
temp_z=z
for y in range(j):
temp_y=y
for x in range(3):
if x==0:
r = np_image[z][y][x]
if x==1:
g = np_image[z][y][x]
if x==2:
b = np_image[z][y][x]
if (r == 0) and (g == 0) and (b == 0):
cyan=0
magenta=0
yellow=0
key=cmyk_scale
c_final[temp_z][temp_y][0]=cyan
c_final[temp_z][temp_y][1]=magenta
c_final[temp_z][temp_y][2]=yellow
c_final[temp_z][temp_y][3]=key
else:
c = 1 - r / 255.
m = 1 - g / 255.
y = 1 - b / 255.
min_cmy = min(c,m,y)
c = (c - min_cmy) / (1 - min_cmy)
m = (m - min_cmy) / (1 - min_cmy)
y = (y - min_cmy) / (1 - min_cmy)
k = min_cmy
cyan =(int) (c*cmyk_scale)
magenta =(int) (m*cmyk_scale)
yellow = (int)(y*cmyk_scale)
key = (int)(k*cmyk_scale)
c_final[temp_z][temp_y][0]=cyan
c_final[temp_z][temp_y][1]=magenta
c_final[temp_z][temp_y][2]=yellow
c_final[temp_z][temp_y][3]=key
np_image1 = np.array(c_final)
t1=np.shape(np_image1)
print(t1)
cnum_list = np_image1.tolist()
print( len(cnum_list))
str1c =str(cnum_list)
print( len(str1c))
f=open("idcardcmyk.txt","w")
f.write(str1c)
f.close()
im = Image.fromarray(np_image1, mode='CMYK')
im.save('testing.jpg')
##im = Image.fromarray(im, mode="CMYK")
print("test ok")
ct = np_image1[:, :, 0]
cyan_list=ct.tolist()
str_c =str(cyan_list)
f=open("idcard_cp.txt","w")
f.write(str_c)
f.close()
print("test ok")
img= Image.fromarray(ct)
img.save('idcard_c.png')
mt = np_image1[:, :, 1]
imm= Image.fromarray(mt)
imm.save('idcard_m.png')
yt = np_image1[:, :, 2]
imm = Image.fromarray(yt)
imm.save('idcard_y.png')
kt = np_image1[:, :, 3]
imm = Image.fromarray(kt)
imm.save('idcard_k.png')

matplotlib: changing a single voxel face color

I want to reproduce this image using matplotlib. The example docs have a numpy logo, but all the voxel cubes are homogenous in color.
I could imagine perhaps making a separate surface plot for each face I want to change but that seems impractical. Here's the code for the example docs numpy logo:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
def explode(data):
size = np.array(data.shape)*2
data_e = np.zeros(size - 1, dtype=data.dtype)
data_e[::2, ::2, ::2] = data
return data_e
# build up the numpy logo
n_voxels = np.zeros((4, 3, 4), dtype=bool)
n_voxels[0, 0, :] = True
n_voxels[-1, 0, :] = True
n_voxels[1, 0, 2] = True
n_voxels[2, 0, 1] = True
facecolors = np.where(n_voxels, '#FFD65DC0', '#7A88CCC0')
edgecolors = np.where(n_voxels, '#BFAB6E', '#7D84A6')
filled = np.ones(n_voxels.shape)
# upscale the above voxel image, leaving gaps
filled_2 = explode(filled)
fcolors_2 = explode(facecolors)
ecolors_2 = explode(edgecolors)
# Shrink the gaps
x, y, z = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2
x[0::2, :, :] += 0.05
y[:, 0::2, :] += 0.05
z[:, :, 0::2] += 0.05
x[1::2, :, :] += 0.95
y[:, 1::2, :] += 0.95
z[:, :, 1::2] += 0.95
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.voxels(x, y, z, filled_2, facecolors=fcolors_2, edgecolors=ecolors_2)
plt.show()
'''
=====================================
Rotating 3D voxel animation of PYTHON
=====================================
Demonstrates using ``ax.voxels`` with uneven coordinates
'''
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as manimation
from math import copysign
def explode(data):
size = np.array(data.shape)*2
data_e = np.zeros(size - 1, dtype=data.dtype)
data_e[::2, ::2, ::2] = data
return data_e
def voxel_face(corns, dm, nf):
'''
Grab the corner coordinates of one voxel face
Parameters
----------
corns : np.indices array of corners for one voxel
dm : (dimension), values can be 0(x), 1(y), 2(z)
nf : (near/far face), values can be 0(near), 1(far)
'''
lc = corns.copy() #local copy so we don't swap original
if dm == 1 : #swap y into x and correct ordering
lc[0], lc[1] = corns[1].transpose(1,0,2), corns[0].transpose(1,0,2)
if dm == 2 : #swap z into x and correct ordering
lc[0], lc[2] = corns[2].transpose(2,1,0), corns[0].transpose(2,1,0)
ret = np.zeros((3,2,2))
xc1 = lc[0,nf,0,0] #hold x dim constant
ret[0,:] = np.array([[xc1, xc1], [xc1, xc1]])
yc1, yc2 = lc[1,0,0:2,0]
ret[1,:] = np.array([[yc1, yc2], [yc1, yc2]])
zc1, zc2 = lc[2,0,0,0:2]
ret[2,:] = np.array([[zc1, zc1], [zc2, zc2]])
if dm != 0 : #swap x back into desired dimension
ret[0], ret[dm] = ret[dm].copy(), ret[0].copy()
return ret
# build PYTHON letters
n_voxels = np.zeros((4, 4, 5), dtype=bool)
letters = [None]*6
letter_faces = np.zeros((6,2),dtype=int)
#P
n_voxels[0, 0, :] = True
n_voxels[:, 0, -3] = True
n_voxels[:, 0, -1] = True
n_voxels[-1, 0, -2] = True
letters[0] = np.array(np.where(n_voxels)).T
letter_faces[0] = [1, 0] #close y face
n_voxels[...] = False
#Y
n_voxels[-1, 0, -3:] = True
n_voxels[-1, -1, :] = True
n_voxels[-1, :, -3] = True
n_voxels[-1, :, 0] = True
letters[1] = np.array(np.where(n_voxels)).T
letter_faces[1] = [0, 1] #far x face
n_voxels[...] = False
#T
n_voxels[:, 0, -1] = True
n_voxels[1:3, :, -1] = True
letters[2] = np.array(np.where(n_voxels)).T
letter_faces[2] = [2, 1] #far z face
n_voxels[...] = False
#H
n_voxels[0, 0, :] = True
n_voxels[0, -1, :] = True
n_voxels[0, :, 2] = True
letters[3] = np.array(np.where(n_voxels)).T
letter_faces[3] = [0, 0] #close x face
n_voxels[...] = False
#O
n_voxels[0, 1:3, 0] = True
n_voxels[-1, 1:3, 0] = True
n_voxels[1:3, 0, 0] = True
n_voxels[1:3, -1, 0] = True
letters[4] = np.array(np.where(n_voxels)).T
letter_faces[4] = [2, 0] #close z face
n_voxels[...] = False
#N
n_voxels[0, -1, :] = True
n_voxels[-1, -1, :] = True
n_voxels[1, -1, 1:3] = True
n_voxels[2, -1, 2:4] = True
letters[5] = np.array(np.where(n_voxels)).T
letter_faces[5] = [1, 1] #far y face
n_voxels[...] = False
fcol = np.full(n_voxels.shape, '#7A88CC60')
ecol = np.full(n_voxels.shape, '#7D84A6')
filled = np.ones(n_voxels.shape)
# upscale the above voxel image, leaving gaps
filled_2 = explode(filled)
fcolors_2 = explode(fcol)
ecolors_2 = explode(ecol)
# Shrink the gaps
corn = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2
ccorn = 0.05 #close corner
fcorn = 1.0 - ccorn
corn[0,0::2, :, :] += ccorn
corn[1,:, 0::2, :] += ccorn
corn[2,:, :, 0::2] += ccorn
corn[0,1::2, :, :] += fcorn
corn[1,:, 1::2, :] += fcorn
corn[2,:, :, 1::2] += fcorn
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.axis("off")
#Plot the voxels
x, y, z = corn
ax.voxels(x, y, z, filled_2, facecolors=fcolors_2, edgecolors=ecolors_2)
#Plot the letter square faces
jj=0
for j in [x for x in letters if x is not None]:
locf = np.empty((j.shape[0],3,2,2)) #local face
ji = 0
for i in j:
i = i * 2 #skip empty voxels
loc = corn[:,i[0]:i[0]+2,i[1]:i[1]+2,i[2]:i[2]+2] #local corners
locf[ji] = voxel_face(loc, letter_faces[jj,0], letter_faces[jj,1])
ax.plot_surface(locf[ji,0],locf[ji,1],locf[ji,2],color='#ffe500a0',
shade=False)
ji += 1
jj += 1
#Views: PY, P, Y, T, H, O, N, PY
view_elev = [ 5, 0, 0, 90, 0, -90, 0, 5]
view_azim = [-60, -90, 0, 90, 180, 180, 90, -60]
#'''
FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
comment='Movie support!')
writer = FFMpegWriter(fps=25, metadata=metadata)
with writer.saving(fig, "pythonRot2.mp4", 100):
for j in range(20):
ax.view_init(view_elev[0], view_azim[0])
plt.draw()
writer.grab_frame()
for i in range(1,len(view_elev)):
de = (view_elev[i] - view_elev[i-1])
da = (view_azim[i] - view_azim[i-1])
if abs(da) >= 180 : #unecessary in this config
da -= copysign(360, da)
if abs(de) >= 180 :
de -= copysign(360, de)
if i != 1 :
steps = 60
else :
steps = 10
da = da / steps
de = de / steps
for j in range(10): #Pause on direct view of a letter
ax.view_init(view_elev[i-1], view_azim[i-1])
plt.draw()
writer.grab_frame()
for j in range(steps): #Rotate to next letter
ax.view_init(view_elev[i-1] + j*de,
view_azim[i-1] + j*da)
plt.draw()
writer.grab_frame()
#'''

Numpy: Setting an array element with a sequence

I'm not sure what i did wrong with this code:
import cv2
from matplotlib import image as img
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.pyplot import axis
img = cv2.imread('popi.png', 0)
cv2.imshow('lel', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
a = np.shape(img)
iloscpixeli = a[0] * a[1]
print(iloscpixeli)
b = np.zeros((256, 1))
H = np.zeros((a[0], a[1]))
# czest = np.zeros((256, 1))
# probf = np.zeros((256, 1))
# probc = np.zeros((256, 1))
# cum = np.zeros((256, 1))
dim = np.shape(img)
wyjscie = np.zeros(dim)
H = np.zeros(dim)
print("dim", dim)
czest = np.zeros(dim)
probc = np.zeros(dim)
# print("r",czest)
probf = np.zeros(dim)
cum = np.zeros(dim)
for i in range(1, a[0]):
for j in range(1, a[1]):
wartosc = img[i, j]
czest[wartosc + 1] = (czest[wartosc + 1] + 1)
probf[wartosc + 1] = czest[wartosc + 1] / iloscpixeli
suma = 0
nobins = 255
d = np.zeros((256, 1))
d1 = np.shape(d)
d11 = d1[0]
for i in range(1, d11):
suma = suma + czest[i]
cum[i] = suma
probc[i] = cum[i] / iloscpixeli
wyjscie[i] = np.around(probc[i] * nobins)
wyjscie=wyjscie.tolist()
for i in range(1, a[0]):
for j in range(1, a[1]):
H[i, j] = wyjscie[img[i,j] + 1]
cv2.imshow('wyrownany', H)
And this line(yeah last :C) :
H[i, j] = wyjscie[img[i,j] + 1]
Gives me error ValueError: setting an array element with a sequence. Trying to repair checked about change the 'wyjscie' from array to list.. but doesnt work well.
I looking for any help. It's great when you look for code, probably I do something stupid and...but there is line czest[wartosc + 1] = (czest[wartosc + 1] + 1) and it works well...
H is a numpy-array with dtype=float as it's default. It's shape is 2d
You want to insert wyjscie[img[i,j] + 1]
wyjscie itself is a numpy-array with shape 2d
you convert wyjscie to a list, but this list will be a nested list because original dim is 2d
you index in nested list, therefore obtain a list and put this list into a cell which holds a float = putting a sequence/list into array element ERROR
(you are polish :-D)

Categories