Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lists
#1
Please tell me the difference between this:
for x,y,radius in circles:
and this:
for x,y,radius in circles[0]:
and this:
for x,y,radius in circles[]:
given that circles is a list:
[[125 59 5][277 151 6]]
circles is the output from cv2.HoughCircles()

I come from C. I would have thought that circles[0] was the zeroth element of the array "circles", but it doesn't seem to work that way.
Reply
#2
There is a difference between a python list and a numpy array. It looks like your cv2 function is returning a numpy array. Can you show exactly what the value is? It should have commas in it. The value that you show above isn't a valid python list, nor a numpy array.

circles[0] is the zeroth element of circles. I suspect that if you count the number of brackets, they will be reduced by one when you compare circles and circles[0]. And if you print out circles.shape, it will say that the first element is of size 1. So the single element contains all the data, but you've removed one level of nesting.
Reply
#3
Whatever is after the word "in" is an iterable.
In the first example, "circles" is the iterable. It will yield two elements which are the two component lists. Since you are wanting 3 elements which are the parts of the list this will not work for you.
In the second example, "circles[0]" is the first element, which is a list, and therefore it yields 3 elements which are the items in the first component list, one at a time. That is why it does not work.
In the third example, you get a syntax error.

circles = [[1,2,3],[4,5,6]]
for n in circles:
    print(n)
for n in circles[0]:
    print(n)
Output:
[1, 2, 3] [4, 5, 6] 1 2 3
Reply
#4
You are right, opencv produces a numpy array. So the object "circles" is a numpy array. If I tell it to print(circles), I get the following (the dtype is float):
[[[102.5.  12.5  5.8]
[277.  151.  6.8]
[267.5  152.5  6]
[137.5  117.5  6]]]
size 12
shape (1,4,3)
strides (48,12,4]
There are three beginning brackets and three ending brackets, no commas, whitespace between elements.

This being the case, I need to understand the difference between: circles and circles[]. It was my understanding that a numpy array in memory is a pointer to the location in memory first value, the number of memory locations to skip before the second value, and the total length (in bytes). If that is correct, circles[0]= 125 and circles[4]= 151. However, if I print(circles[4]), I get an error telling me that "4" is out of bounds for axis 0 with size 1. So, clearly the '4' is not an array index in the sense I think it is.

This confuses the shit out of me. What am I actually asking with:
print(circles[4])
?
Reply
#5
jefsummers, sorry, I didn't see your post before making mine.

To hopefully answer my question then, circles[0] is the first group of three items, circles[1] is the second group, etc. So circles[4] refers to a fourth and non-existent group of three values. I suppose that this is a list of lists.

How does one address just a single data item, for example, how does one address the second item in the third group?
I have not figured that one out.
Reply
#6
The outermost level has only one element. circles[0] is that element. shape tells you this, that the outermost level has only 1 element.

circles (3 brackets) ->
[[[102.5.  12.5  5.8]
[277.  151.  6.8]
[267.5  152.5  6]
[137.5  117.5  6]]]
circles[0] (2 brackets) ->
[[102.5.  12.5  5.8]
[277.  151.  6.8]
[267.5  152.5  6]
[137.5  117.5  6]]
Shape tells you the next level has 4 elements. So the first element in that would be:

circles[0,0] (1 bracket) ->
[102.5.  12.5  5.8]
If you wanted the 152.5 value, that's in position 0, 2, 1
circles[0,2,1] ->
152.5
Reply
#7
OK, so this should be pictured as a 3 dimensional array where the first dimension is into<>out of the screen. The second is up<>down and the third is right<>left. One knows this by looking at the number of brackets, where three brackets indicates three dimensions.

If that is right, then in a color image (numpy array) the color channel data (BRG) must be in the [0] dimension.

So, lets say I want to look at every pixel in an image. If the sum of the channel values for that pixel is greater than 600 (bright pixel), I want to return the x,y location of that pixel. How does one do that?
Reply
#8
Answering the question about how to access a single element:
circles = [[1,2,3],[4,5,6],[7,8,9]]
print(circles[1][2])
Output:
6
Prints the 3rd item in the second group.
Reply
#9
Quote:So, lets say I want to look at every pixel in an image. If the sum of the channel values for that pixel is greater than 600 (bright pixel), I want to return the x,y location of that pixel. How does one do that?

Do you mean how to loop through, or to design a function that yields the locations that match your criteria?

You're starting to get into where Pandas would be useful. Take your list of lists, convert into a dataframe, and get your list of locations where the sum of the RGB is >600 in one line. Is that the direction you want to go?
Reply
#10
I am an engineer, not a programmer. I write code for my projects, but it is usually in C97 for use on microcontrollers. I am a Python neophyte. I have built an automated telescope with a HD imager and a spectroscopic imager. The purpose is to obtain the spectrum of UAP (Unidentified Aerial Phenomenon) objects in the sky. I simple terms, lights in the sky at night. I am interested in this because I think that there are clues to the operation of the propulsion system of these craft in their spectrum.

The movement and pointing of the telescope is run by a microcontroller. The cameras (imager + spectrograph) are co-aligned and they both take standard HD images. I am doing the image processing with an RPi 4 in python. I need to locate the coordinates of objects in the sky that appear in the images. They are then used to point the telescope.

So, the device takes an image about every second. In that second, the image processing software needs to examine the image, find any objects of a minimum size in the frame, and calculate their coordinates. If one of the objects is the moon, it needs to identify it as the moon. Then it needs to select the brightest object and track it from frame to frame, passing its coordinates to the motor uC that will point the telescope at the object till it is no longer visible. As long as the object is visible, the spectrograph will record video of the object's spectrum.

I had planned to use opencv for image analysis. The python routine I am working on can load the images taken by the telescope, but identifying the lights in the sky is proving to be a challenge, mostly because I am a novice at python and image processing.

Any suggestions on how to best identify and obtain the coordinates of lighted objects (in the frame) against a dark sky would be most welcome.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,190 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020