0

I have a code that I inform a folder, where it has n images that the code should return me the relative frequency histogram.

From there I have a function call:

for image in total_images:
    histogram(image)

Where image is the current image that the code is working on and total_images is the total of images (n) it has in the previously informed folder.

And from there I call the histogram() function, sending as a parameter the current image that the code is working.

My histogram() function has the purpose of returning the histogram of the relative frequency of each image (rel_freq).

Although the returned values ​​are correct, rel_freq should be a array 1x256 positions ranging from 0 to 255.

How can I transform the rel_freq variable into a 1x256 array? And each value stored in its corresponding position?

When I do len *rel_freq) it returns me 256, that's when I realized that it is not in the format I need...

Again, although the returned data is correct...

After that, I need to create an array store_all = len(total_images)x256 to save all rel_freq...

I need to save all rel_freq in an array to later save it and to an external file, such as .txt.

I'm thinking of creating another function to do this...

Something like that, but I do not know how to do it correctly, but I believe you will understand the logic...

def store_all_histograms(total_images):
    n = len(total_images)

    store_all = [n][256]

    for i in range(0,n):
        store_all[i] = rel_freq

I know the function store_all_histograms() is wrong, I just wrote it here to show more or less the way I'm thinking of doing... but again, I do not know how to do it properly... At this point, the error I get is:

    store_all = [n][256]
IndexError: list index out of range

After all, I need the store_all variable to save all relative frequency histograms for example like this:

position:     0   ...  256 
store_all = [
            [..., ..., ...],
            [..., ..., ...],
            .
            .
            .
            n
            ]

Now follow this block of code:

def histogram(path):
    global rel_freq

    #Part of the code that is not relevant to the question...

    rel_freq = [(float(item) / total_size) * 100 if item else 0 for item in abs_freq]

def store_all_histograms(total_images):
    n = len(total_images)

    store_all = [n][256]

    for i in range(0,n):
        store_all[i] = rel_freq

#Part of the code that is not relevant to the question...

# Call the functions
for fn in total_images:
    histogram(fn)

    store_all_histograms(total_images)

I hope I have managed to be clear with the question.

Thanks in advance, if you need any additional information, you can ask me...

6
  • "When I do len (rel_freq) it returns me 256, that's when I realized that it is not in the format I need..." what do you expect from a 1x256 array? len is a python builtin. Commented Jun 29, 2017 at 18:37
  • 1
    Welcome to stack overflow. A longer question is not necessarily better than a brief one. I have a hard time understanding what is the important part of your question is. Here's a guide to writing great questions: How to Ask Commented Jun 29, 2017 at 18:46
  • @roganjosh I need this because I will later put the value of the histograms into the Multilayer Perceptron and see if it is able to classify my images. Commented Jun 29, 2017 at 18:48
  • @HåkenLid Thanks and sorry. Commented Jun 29, 2017 at 18:49
  • 1
    That's fine. I perhaps wasn't clear; the len of a 1x256 array will just show 256 so even if it's a numpy array, len will look at it like a (nested if necessary, which it's not here) list. That's not an indication that anything went wrong. As @HåkenLid stated, your question is quite long and that was the first issue I picked up on. Commented Jun 29, 2017 at 18:50

2 Answers 2

2

Return the result, don't use a global variable:

def histogram(path):
    return [(float(item) / total_size) * 100 if item else 0 for item in abs_freq]

Create an empty list:

store_all = []

and append your results:

for fn in total_images:
    store_all.append(histogram(fn))

Alternatively, use a list comprehension:

store_all = [histogram(fn) for fn in total_images]
Sign up to request clarification or add additional context in comments.

Comments

1
for i in range(0,n):
    store_all[i+1] = rel_freq

Try this perhaps? I'm a bit confused on the question though if I'm honest. Are you trying to shift the way you call the array with all the items by 1 so that instead of calling position 1 by list[0] you call it via list[1]?

So you want it to act like this?

>>list = [0,1,2,3,4]
>>list[1]
0

1 Comment

>>list = [[0,0,0,0,...,0,0,0,0], [1,1,1,1,...,1,1,1,1], [2,2,2,2,...,2,2,2,2]] >>list[3] >>[2,2,2,2,...,2,2,2,2] This is how I need it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.