Fastest way to scan and analyze images
Posted: 2009-06-03T10:26:42-07:00
I have a variety of little algorithms that need to walk through pixels one at a time to look for stuff.
For example, given a b&w tiff image, which I know starts with around 100 rows of all white, I would like to locate the first row which has at least one non-white pixel. So I wrote this, which is very slow. I am writing in Ruby but hopefully it's pretty clear.
rows = img.rows
cols = img.columns
rows.times do
|row|
pixels = image.get_pixels(0, row, image.columns, 1)
pixels.each do |p|
if p.to_color != "white"
return row
end
end
end
return nil
So, basically I am getting the pixels of a whole row with image.get_pixels, and then stepping through those pixels, doing pixel.to_color and checking for "white".
This was the most obvious to me, but clearly way inefficient.
Tips would be greatly appreciated!
Pito
For example, given a b&w tiff image, which I know starts with around 100 rows of all white, I would like to locate the first row which has at least one non-white pixel. So I wrote this, which is very slow. I am writing in Ruby but hopefully it's pretty clear.
rows = img.rows
cols = img.columns
rows.times do
|row|
pixels = image.get_pixels(0, row, image.columns, 1)
pixels.each do |p|
if p.to_color != "white"
return row
end
end
end
return nil
So, basically I am getting the pixels of a whole row with image.get_pixels, and then stepping through those pixels, doing pixel.to_color and checking for "white".
This was the most obvious to me, but clearly way inefficient.
Tips would be greatly appreciated!
Pito