Chris,

    RED, GRN, BLU = 0, 1, 2
    line_pix = ((rmin <= data[:,:,RED] <= rmax) &
                (gmin <= data[:,:,GRN] <= gmax) &
                (bmin <= data[:,:,BLU] <= bmax))

I think this part doesn"t work because the data falls out of numpy 
array-type when you slice it like this.  It is coerced to a 
normal list [Python in a Nutshell, page 306, para 3, says this
is something being "fixed" in numarray] so these compares fail 
with:

   TypeError: list indices must be integers


Here is a test harness if you have any time to fool with it.

<code>
import wx
import numpy as N

YOU_HAVE_AN_IMAGE = 0
if YOU_HAVE_AN_IMAGE:
    path = "8bitchart01_small.tiff" # put its name here
    image = wx.Image(path, wx.BITMAP_TYPE_ANY)
    w, h = image.GetWidth(), image.GetHeight()
    # Rappin page 362, Smart page 285
    data = N.fromstring(image.GetData(), N.uint8)    
    data.reshape(w, h, 3)

else:
    # for testing:
    #>>> data = numpy.random.randint(0,255,(5,5,3))
    #>>> print data <snip>

    data = [
    [[ 64, 237, 108],  #  x  y
     [185, 240,  68],  # [0][1]
     [ 17, 116,  55],  # [0][2]
     [107,  11, 185],  # [0][3]
     [154,  30,  52]], # [0][4]

    [[251,  73, 150],  #  x  y
     [101,  91, 251],  # [1][1]
     [ 47, 225,  97],  # [1][2]
     [ 61,  23,  79],  # [1][3]
     [161,  22, 165]], # [1][4]

    [[  4, 162, 188],  # [2][0]
     [ 25, 131, 211],  # [2][1]
     [169,  83, 214],  # [2][2]
     [162, 112, 177],
     [254, 139,  95]],

    [[  2,  88, 216],  # [3][0]
     [230,  63, 192],
     [113, 151, 142],
     [ 71,  78, 250],
     [104,  65, 127]],

    [[  6, 191, 220],  # [4][0]
     [ 38, 141,   4],
     [245,  41,  23],
     [165,  24,   5],
     [ 23, 127, 183]]]



# red, grn, blu = data[0][0]
# print "r=%r, g=%r, b=%r" % (red, grn, blu)  # prints "r=64, g=237, b=108"

# this is a threshold filter
rmin, rmax, gmin, gmax, bmin, bmax = 0,178, 0,178, 100,255

def test1(data):
    very_blue_pixels = []
    w = 5  # data dependent
    x = y = 0
    # searches rows first (x is turning faster, so...)
    for yslice in data:
        for xyslice in yslice:
            if 0: print "x=%r, y=%r, type(xyslice)= %r" % (x, y, type(xyslice))
            if 0: print "data[x=%i][y=%i]=%r" % (x, y, data[x][y])
            red, grn, blu = xyslice
            if (rmin <= red <= rmax) and \
               (gmin <= grn <= gmax) and \
               (bmin <= blu <= bmax):
                if 0: print "a very blue pixel at x=%i, y=%i, rgb=%r" % (x, y, 
xyslice)
                very_blue_pixels.append((x,y))
            y += 1
            if y==w: y=0
        x += 1
    print very_blue_pixels
    # returns [(0, 3), (1, 1), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (3, 0), 
(3, 2), (3, 3), (3, 4), (4, 4)]

def test2(data, THRESH):
    # for my min/max dataset I can optimize like this 
    # at the cost of getting less output
    # [(3, 0), (1, 1), (0, 2), (1, 2), (2, 2), (0, 3), (3, 3), (4, 4)]
    very_blue_pixels = []
    w = 5  # data dependent
    x = y = 0
    for xslice in data:
        for xyslice in xslice:
            red, grn, blu = xyslice
            if (red <= THRESH) and (grn <= THRESH) and (THRESH <= blu):
                very_blue_pixels.append((x, y, xyslice))
                if 1: print "I see blue data[x=%i][y=%i] = %r" % (x, y, xyslice)
            y += 1
            if y==w: y=0
        x += 1
    print very_blue_pixels

#test1(data)
#test2(data, 178)

</code>

# ====

# CB wrote:
# > but, you can get rid of the loops:
# >                 if (rmin <= red <= rmax) and \
# >                    (gmin <= grn <= gmax) and \
# >                    (bmin <= blu <= bmax):

# > this could all be done in one fell swoop:   
RED, GRN, BLU = 0, 1, 2
rmin, rmax, gmin, gmax, bmin, bmax = 0,178, 0,178, 100,255

line_pix = ((rmin <= data[:,:,RED] <= rmax) &
            (gmin <= data[:,:,GRN] <= gmax) &
            (bmin <= data[:,:,BLU] <= bmax))

# data(line_pix)  =   0  # black   returns: 
#    SyntaxError: can't assign to function call
# data(~line_pix) = 255  # white    -CB

# > or maybe:
data = N.where(data, line_pix, 0, 255) 

_______________________________________________
FloatCanvas mailing list
[email protected]
http://mail.mithis.com/cgi-bin/mailman/listinfo/floatcanvas

Reply via email to