0

I want to identify a straight line in a picture (black/white, 0/1) and locate it so i already turned the picture into an 2d array with numpy and identified the line through calculating the average of the cell (a cell is a row in the 2d array, see source below). Now i would like to return the index (not the value) or at least the location of the cells which are not white.

I found a way to do so in a 1d array but going though the cells is faster than iterate though each pixel value. Till now i have something like this:

for cell in img_array:                              # the array is 2d and the values are 0 or 1
    cal_average(cell)                               # selfmade function to calculate the average
    if not cal_average(cell) == 1 :                 # condition to find the line
        print('line found, it is here:', location)  # not working outcome

Hope you know something. Thanks in advance!

Sources which i want to merge:

How to get item's position in a list?

Iterating through a multidimensional array in Python

szymanski
  • 51
  • 4
  • I have no idea what you are asking. "cell" is a term that is not defined. The inputs are not well defined, and I have no idea what "location" is. Please post an MCVE and define your terms properly. – Mad Physicist Aug 18 '20 at 17:03
  • That's why i put the sources in. The Cell is a row in the 2d array and the location is what i want to find out. I don't know the term so i wrote just 'location' to describe what i am searching for. Like the Number of the row or cell to stick with the terms i was using. – szymanski Aug 19 '20 at 08:21
  • You shouldn't expect people to go elsewhere to understand your question. Please make it self contained in the future. – Mad Physicist Aug 19 '20 at 15:31

1 Answers1

2

If I have understood the question right, you want to print the index (x and y coords) of an element within a 2d array.

I would use numpy for its convenience and efficiency for array operations.

import numpy as np  # import numpy 
arr = np.array([[5,4,3],
        [7,6,1],
        [10,34,7]
                ]) # example array

xs, ys = np.where(arr == 7) # criteria to search (can be modified to other operators like !=, <, > etc..
# xs, ys are the x and y coordinates of the indexes where the item was found. x[0], y[0] would be the the first point and so on...

for x,y in zip(xs,ys):
    print("Found at:",x,y) # loop through to print values 

Output:

Found at: 1 0

Found at: 2 2

Hissaan Ali
  • 2,229
  • 4
  • 25
  • 51
  • 1
    If it solved your problem, consider accepting the answer. If however there is something you would like me to add, feel free to mention. – Hissaan Ali Aug 19 '20 at 09:06
  • yes it worked, thanks. So the trick was to clarify the condition inside of the array and zip the output to print it correctly. – szymanski Aug 19 '20 at 09:29
  • 1
    Exactly. Further, if you would like to change the values at those indices to something else you can simply do `arr[ np.where(arr == 7) ] = YOUR_NEW_VALUE` – Hissaan Ali Aug 19 '20 at 09:36
  • i would like to print only the first and the last value. i tried `print('Found between',x[0],y[0],'and',x[-1],y[-1])` but i get _IndexError: invalid index to scalar variable._ – szymanski Aug 19 '20 at 09:50
  • 2
    It should be `xs[0],ys[0]` you are using `x` instead of `xs`. `x` is the looping value `xs` is the array to index – Hissaan Ali Aug 19 '20 at 09:57
  • Yes that was it. The np.where() function made my loop redundant. I learned something! :D – szymanski Aug 19 '20 at 11:04