0

I am looking for a pythonic way to make a list of tuples that looks at a range of points determines if the pixel information at those points meets criteria and if so adds to the list.

I have the following line of code, which I know is incorrect but I hope it explains what i'm trying to accomplish. the "h,s,v = img[iy,((x//2)-ix)]" part is not correct because I don't think you can not assign h,s,v how I currently have the code.

how do you assign h,s,v = img[] inside a for loop?

pointlist = [h,s,v = img[iy,((x//2)-ix)] for ix in range(x//2) for iy in ylist if any((hm-hsm)<h<(hm+hsm) and (sm-ssm)<s<(sm+ssm) and (vm-vsm)<v<(vm+vsm) for hm,sm,vm,hsm,ssm,vsm in csample)]

Another way to maybe write this would be:

csample = (60,30,100,15,15,25)

for iy in ylist:
    for (x//2)-ix for ix in range(x//2):
        h,s,v = img[iy,ix]
        if any((hm-hsm)<h<(hm+hsm) and (sm-ssm)<s<(sm+ssm) and (vm-vsm)<v<(vm+vsm) for hm,sm,vm,hsm,ssm,vsm in csample)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135

1 Answers1

2

Your requirements weren't specified all that well, but I think this is what you had in mind:

Code:

One thing that might need explaining, is the isgood(*img...). This expands the tuple stored in img and passes the three elements as sperate args to is_good. See here.

hm, sm, vm, hsm, ssm, vsm = csample
def is_good(h, s, v):
    return (hm-hsm < h < hm+hsm and
            sm-ssm < s < sm+ssm and
            vm-vsm < v < vm+vsm
            )

point_list = [(ix, iy) for iy in ylist for ix in range(x//2+1)
              if is_good(*img[iy, x//2-ix])]

Test data:

csample = (60, 30, 100, 15, 15, 25)

img = (
    ((1, 2, 3), (1, 2, 3)),
    ((60, 30, 100), (1, 2, 3)),
)
ylist = [0, 1]
x = 2

Produces:

[(1, 1)]
Community
  • 1
  • 1
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Stephen, this got me on the right track thank you and my apologies on my wording of the question and code. it is sometimes difficult to word my questions correctly. what is the " * " for that you used in " is_good(*img[ " as you can tell I'm a beginner so im curious as to why the use of " * " thanks again for making sense of my lack of sense – Exclusiveicon Feb 27 '17 at 15:06