0

我在 Python 模式下使用处理来加载图像并对其进行计算。总体思路是:

def setup():
    global maxx, maxy
    maxx = 0
    maxy = 0

    # load the image
    global img
    img = loadImage("img.jpg");

    maxx = img.width
    maxy = img.height

def draw():
    image(img, 0, 0);

def mousePressed():
    calc()

def calc():
    height = int(img.height)
    width = int(img.width)
    print "width: " + `width`
    print "height: " + `height`
    print "width*height: " + `width*height`

    # iterate over the input image
    loadPixels()
    print "length of pixels array: " + `len(pixels)`

    # ... do stuff with the image

对于 1920x1200 左右的较小图像,“宽度 * 高度”和“像素阵列长度”是相同的。对于像 3025 × 2009 这样的大图像,像素阵列的长度要小得多。对于 3025 x 2009 的示例,差异是:
宽度 * 高度:6077225 像素阵列长度:3944600

任何想法可能会发生什么?

4

1 回答 1

1

在调试中,我发现了问题。在 img 中调用 loadPixel 可以获得正确的像素...

def calc():
    height = int(img.height)
    width = int(img.width)
    print "width: " + `width`
    print "height: " + `height`
    print "width*height: " + `width*height`

    # iterate over the input image
    img.loadPixels()
    print "length of pixels array: " + `len(img.pixels)`

在对 loadPixels() 进行更多研究后,我将更新此答案

于 2016-10-11T18:57:54.507 回答