Null0007
Null0007

Reputation: 13

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...

Upvotes: 0

Views: 672

Answers (1)

sascha
sascha

Reputation: 33522

  • 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)

Upvotes: 1

Related Questions