Starting at square one with image processing
Wilhelm Sanke
sanke at hrz.uni-kassel.de
Sat Jul 7 05:40:58 EDT 2007
James Hurley jhurley0305 at sbcglobal.net
<mailto:use-revolution%40lists.runrev.com?Subject=Starting%20at%20square%20one%20with%20image%20processing&In-Reply-To=20070706134327.98049488F6E%40mail.runrev.com>
wrote:
>I've discovered that I have no idea what's going on with the
>imagedata function.
>
>I thought that the imagedata was a pixel by pixel map of the image. I
>tried the following handler to look at each pixel of my image:
Allow me to add some information to the recommendations you already got from this list.
The basic format for imagedata manipulation I use in my applications is like shown below.
Treating the image as a square enables you also to access and change pixels according to
their positions, i.e. you could select single pixels or rectangular parts of the picture
and manipulate only them. Changing the imagedata for an entire image would look like here:
"put the imageData of image "x" into iData
put the height of img "x" into theight
put the width of img "x" into twidth
put 4* twidth into ro # "ro" for "row": There are 4 chars to each pixel in a row.
repeat with i = 0 to theight - 1
repeat with j = 0 to twidth - 1
#get the color values
put (chartonum(char (i*ro + j* 4 + 2) of idata)) into tRed
put (chartonum(char (i*ro + j* 4 + 3) of idata)) into tGreen
put (chartonum(char (i*ro + j* 4 + 4) of idata)) into tBlue
#=============================
# now do whatever you want to change the values of tRed, TGreen, and tBlue
# and then put them back into the imagedata
#==================================
put numtochar(tRed) into char (i*ro + j* 4 + 2) of idata
put numtochar(tGreen) into char (i*ro + j* 4 + 3) of idata
put numtochar(tBlue) into char (i*ro + j* 4 + 4) of idata
end repeat
end repeat
set the imageData of image "x" to iData"
I also use the "for each" format in my scripts, but this is not necessarily faster.
I have even found cases where "for each" is slower than the double repeat loop.
To speed up script execution it is advisable to simplify the computation inside
the loops - especially for larger images. Such a simplified script is not as
readable as the basic one, but it is indeed faster.
Thus, the repeat loop from above could be changed to:
"repeat with i = 0 to theight - 1
put i*ro into ti
repeat with j = 0 to twidth - 1
put ti + j*4 into tij
#get the color values
put (chartonum(char tij+ 2) of idata)) into tRed
put (chartonum(char (tij+ 3) of idata)) into tGreen
put (chartonum(char (tij+ 4) of idata)) into tBlue
#=============================
# now do whatever you want to change the values of tRed, TGreen, and tBlue
# and then put them back into the imagedata
#==================================
put numtochar(tRed) into char (tij+ 2) of idata
put numtochar(tGreen) into char (tij+ 3) of idata
put numtochar(tBlue) into char (tij+ 4) of idata
end repeat
end repeat"
To produce a gray-scale image based on the red values (this looks much better
than computing the average value of tRed, tGreen, tBlue) the inner part of
the script examples above would be
" put (chartonum(char tij+ 2) of idata)) into tRed
put numtochar(tRed) into char (tij+ 3) of idata
put numtochar(tRed) into char (tij+ 4) of idata"
In my various "imagedata" stacks there are quite a number of accessible scripts
that you could change and experiment with - about hundred in
stack "Imagedata toolkit 2 preview".
<http://www.sanke.org/Software/ImagedataToolkitPreview3.zip>
<http://www.sanke.org/Software/SeamlessTiles.zip>
A new imagedata stack that can handle images of any size will be released soon.
Regards,
Wilhelm Sanke
<http://www.sanke.org/MetaMedia>
More information about the use-livecode
mailing list