0

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

1
  • What is the shape or len() of wyjscie[img[i,j] + 1]? Since H is 2d, H[i,j] requires a scalar or single item list or array. Commented May 18, 2016 at 2:41

1 Answer 1

1
  • 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)
Sign up to request clarification or add additional context in comments.

9 Comments

Yep, i am and my english is .. xD no comment. @sascha
I'm not sure if I understood it well, so i no need to convert wyjscie to a list? What i should do?
wyjscie is a list after you convert it to a list before. I don't know exactly what you are doing. The .tolist() step before, well i don't like it. But with only one index into wyjscie, you obtain a list/sequence. So you need a second indexing or a completely different approach. I recommend regular usages of print(type(X)) and print(X.shape) if it's a numpy array for debugging purposes. This will help!
when i just delete the wyjscie.tolist() (btw. wyjscie in polish is out ;P) and print(np.shape(H)) print(np.shape(wyjscie)) print(np.shape(img)) it gives me fit sizes(shapes) (H=wyjscie=img).. so for me it should work but still...
wyjscie[img[i,j] + 1] is always a one-dimensional index (img[i,j] = number + 1 = number) -> you will always get the other dimension back -> a full row/column = sequence. A 2d-indexing would look like: wyjscie[img[i,j] + 1, 5]. But well... i don't know what you are doing :-). Check the shapes and look up numpys indexing rules.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.