我在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
知道会发生什么吗?
在调试中,我发现了问题。在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()进行更多研究后,我会更新这个答案