Re: Fast Algorithm to Determine Average Brightness of an Image
@BR I adjusted your function a litte bit (and it works now here). There is a new parameter for testing the upper/lower half of the image (this param = empty tests the full image). The mouseUp is my suggestion for a first testing. On base of these two values you could develop a decision rule for a dark/light setting. [This may be better than simply averaging "sky" and "stones". For example my first trial for a rule would be if 0.62*upperBrightness + 0.38*lowerBrightness > 127 then set totalBrightnes to "light" else set totalBrightness to "dark"] on mouseUp set the paintcompression to RLE set topleft of img 1 to the bottomleft of me put the millisecs into m1 put avgBrightness2(the long id of img 1,"upper") into b set backcolor of grc 1 to (b,b,b) put b into c put avgBrightness2(the long id of img 1,"lower") into b set backcolor of grc 2 to (b,b,b) put comma & b after c put c & ": " & (the millisecs - m1) & " ms" into fld 1 end mouseUp ## The adjusted function: -- theImage = the long id of an img -- thePart= "upper" or "lower" or empty (or anything else) function avgBrightness2 theImage, thePart lock screen; lock messages put "tmp.42.42" into ii put the imageData of theImage into iData0 put the width of img 1 into tOrigWidth if thePart is "upper" then put (the height of img 1) div 2 into tOrigHeight put byte 1 to 4*tOrigHeight*tOrigWidth of iData0 into iData0 else if thePart is "lower" then put (the height of img 1) div 2 into tOrigHeight put byte -4*tOrigHeight*tOrigWidth to -1 of iData0 into iData0 else put the height of img 1 into tOrigHeight end if repeat while there is an img ii delete img ii end repeat set the paintcompression to RLE -- important for speed create img ii set resizeQuality of img ii to "normal" -- "good", "best" set width of img ii to tOrigWidth -- may be important* set height of img ii to tOrigHeight -- may be important* set imagedata of img ii to iData0 put 8 into newBase -- choose newBase in range 1-64 (watch speed!) if tOrigWidth > tOrigHeight then put newBase into newHeight put trunc(newBase*tOrigWidth/tOrigHeight) into newWidth else put newBase into newWidth put trunc(newBase*tOrigHeight/tOrigWidth) into newHeight end if set width of img ii to newWidth set height of img ii to newHeight put the imagedata of img ii into iData delete img ii put 0 into vRed; put 0 into vGreen; put 0 into vBlue repeat with i=1 to newHeight put (i-1)*4 into i0 repeat with j=1 to newWidth add byteToNum(byte i0+4*j-2 of iData) to vRed add byteToNum(byte i0+4*j-1 of iData) to vGreen add byteToNum(byte i0+4*j of iData) to vBlue end repeat end repeat -- return round((0.1*vRed+0.6*vGreen+0.3*vBlue)/newWidth/newHeight) return round(avg(vRed, vGreen, vRed)/newWidth/newHeight) unlock screen; unlock messages end avgBrightness2 * Your experience of "deleting" or not img "tmp" was an effect of the resizeQuality setting because you didn't set the tmp-image to the original's size. This differs not this much from setting the size, but it differs by a 'jumping' amount for different original's size and different resizeQuality. ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Fast Algorithm to Determine Average Brightness of an Image
@ HH OK I will "bite" on your idea. but I have never examined or processed the imagedata. Please scrutinize my code below. But there is a major caveat with your original code (see below) I think I got it correct. but there is a huge difference on the result between the 1 X 1 and the 12 X 9 I have a late afternoon shot of the sun setting over the ocean. your original function returns 148.981481 # which seems too low… But if I did this correctly (I may not have) I get 192.33 # which seems a lot more correct visually because this is bright image. average across 12x 9 = 108 px = 432 bytes of image data. did I do something wrong here. (disclaimer… this could probably be optimized…but still very fast) function avgBrightness theImage --lock screen; lock messages if there is no img ii then create img ii set resizeQuality of img ii to "best" put the width of img 1 into tOrigWidth put the height of img 1 into tOrigHeight set width of img ii to tOrigWidth set height of img ii to tOrigHeight set imagedata of img ii to the imagedata of img theImage put 12 into tNewWidth # Scale down proportinally set the width of img ii to tNewWidth put round ( (tOrigHeight*tNewWidth)/tOrigWidth) into tNewHeight set height of img ii to tNewHeight --set width of img ii to 1 --set height of img ii to 1 put the imagedata of img ii into iData put tNewWidth * tNewHeight * 4 into tNoPixelBytes delete img ii # get beyond first byte otherwise mod 4 returns bogus value put (byteToNum(byte 2 of iData) & "," &\ byteToNum(byte 3 of iData) & "," &\ byteToNum(byte 4 of iData) & ",") into tAllPixelColorValues repeat with x = 5 to tNoPixelBytes if x mod 4 = 1 then next repeat # skip this one, all zeros here. put byteToNum(byte x of iData) &"," after tAllPixelColorValues end repeat put empty into item -1 of tAllPixelColorValues # remove last empty itm return avg (tAllPixelColorValues) unlock screen; unlock messages end avgBrightness BR But if you use your original code and do not delete img ii, a different value is returned than if you uncomment that line.. if you delete img ii you get a different value. function avgBrightness1 theImage --lock screen; lock messages if there is no img ii then create img ii set resizeQuality of img ii to "best" put the width of img 1 into tOrigWidth put the height of img 1 into tOrigHeight set imagedata of img ii to the imagedata of img theImage set width of img ii to 1 set height of img ii to 1 put the imagedata of img ii into iData --delete img ii return avg (byteToNum(byte 2 of iData), \ byteToNum(byte 3 of iData), \ byteToNum(byte 4 of iData)) --unlock screen; unlock messages end avgBrightness1 On 9/20/16, 1:31 PM, "use-livecode on behalf of hh"wrote: There is still one option that uses a weighted grayLevel-mean (I use these weights in imageJIT). This would reflect more than simple averaging that one wants a dark/light decision. return (0.1*byteToNum(byte 2 of iData) \ + 0.6*byteToNum(byte 3 of iData) \ + 0.3*byteToNum(byte 4 of iData)) Also, what will be still fast enough, you could think about scaling down _proportional_ to 3x2 pixels (or 12x8 pixels) and then applying the above weighted mean on these few pixels. p.s. I checked: The one-pixel color is by LC built from the arithmetic mean of the color channels, nearly as fast as an external! ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Fast Algorithm to Determine Average Brightness of an Image
There is still one option that uses a weighted grayLevel-mean (I use these weights in imageJIT). This would reflect more than simple averaging that one wants a dark/light decision. return (0.1*byteToNum(byte 2 of iData) \ + 0.6*byteToNum(byte 3 of iData) \ + 0.3*byteToNum(byte 4 of iData)) Also, what will be still fast enough, you could think about scaling down _proportional_ to 3x2 pixels (or 12x8 pixels) and then applying the above weighted mean on these few pixels. p.s. I checked: The one-pixel color is by LC built from the arithmetic mean of the color channels, nearly as fast as an external! ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Fast Algorithm to Determine Average Brightness of an Image
@ hh That works! fascinating. Though what is bright not bright, is, as you say, "not sharp" Images that return a value > 100 still seem bright enough to warrant darker type… and of course you may have e.g. very dark stone mountains (taking up 1/3 of the composition space at the bottom half of the image) with a very bright, blue clear sky… So "average brightness" has only so much utility in terms of choosing a color brightness for an overlay. But, this is still very useful function. Thank you! On 9/20/16, 10:14 AM, "use-livecode on behalf of hh"wrote: You could try the following function. It is "dirty" (the definition of 'brightness' is also unsharp!), but it is very fast. local ii="tmp-1.414214" -- uses the average color intensity of the pixel -- resulting from scaling down to a 1x1 image function avgBrightness theImage lock screen; lock messages if there is no img ii then create img ii set resizeQuality of img ii to "best" set width of img ii to the width of img 1 set height of img ii to the height of img 1 set imagedata of img ii to the imagedata of theImage set width of img ii to 1 set height of img ii to 1 put the imagedata of img ii into iData delete img ii return avg (byteToNum(byte 2 of iData), \ byteToNum(byte 3 of iData), \ byteToNum(byte 4 of iData)) unlock screen; unlock messages end avgBrightnes ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Fast Algorithm to Determine Average Brightness of an Image
> So is there a fast, efficient way to analyze a 1200 X 800 px > image to get this average brightness value. You could try the following function. It is "dirty" (the definition of 'brightness' is also unsharp!), but it is very fast. local ii="tmp-1.414214" -- uses the average color intensity of the pixel -- resulting from scaling down to a 1x1 image function avgBrightness theImage lock screen; lock messages if there is no img ii then create img ii set resizeQuality of img ii to "best" set width of img ii to the width of img 1 set height of img ii to the height of img 1 set imagedata of img ii to the imagedata of theImage set width of img ii to 1 set height of img ii to 1 put the imagedata of img ii into iData delete img ii return avg (byteToNum(byte 2 of iData), \ byteToNum(byte 3 of iData), \ byteToNum(byte 4 of iData)) unlock screen; unlock messages end avgBrightness ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Re: Fast Algorithm to Determine Average Brightness of an Image
Hi Sannyasin, Based on my previous experiences trying to process images with LC pixel by pixel, I would have to say no. LC is too slow for this kind of processing. You would probably have to use a routine written in some other language like Objective C, or C++, etc. If anyone else has had a different experience with this type of image processing in LC please chime in! Thanks, Rick > On Sep 20, 2016, at 3:15 PM, Sannyasin Brahmanathaswami> wrote: > > Use Case: > > I want to place puzzle tiles on top of an image. We take a field, put a grc > behind it, take a snapshot, crop each word off at -1000, -1000 and then > scramble the tiles to make a puzzle… all this works really well. So I decided > to dress it up a bit more: we dynamically choose a color for the foreground > of the field in RGB range of 0-135 for all three colors → This gets us dark > type… then add 100 to all the colors and this gets us a color for the bkgnd > grc which is the same hue, but brighter (well, that depends on your preferred > brand HSV, HSL, HSB LAB color theory, it's not that simple… but, close enough > for this context) tiles look great.. color is randomized for each puzzle > with the caveat of an occasional "ugly" (in relation to the nature photo in > the background) but still… cool.. > > OK I thought hmmm we could also use light type with dark background and I got > this working… too much fun.. > > Switch sUserColorRange > case "darkType" > set the itemdelimiter to "," # just to be safe > put randomInRange(0,135) into tRGB > put comma & randomInRange(0,135) after tRGB > put comma & randomInRange(0,135) after tRGB > set the backgroundcolor of grc "startHere" to tRGB > set the foregroundcolor of fld _Quote to tRGB > # same hue for background, but light > repeat for each item tHue in tRGB > put tHue+ (115) & comma after tTileBkgndRGB > end repeat > delete char -1 of tTileBkgndRGB > set the backgroundColor of grc quoteBkgnd to tTileBkgndRGB > break > case "lightType" > set the itemdelimiter to "," # just to be safe > put randomInRange(136,255) into tRGB > put comma & randomInRange(136,255) after tRGB > put comma & randomInRange(136,255) after tRGB > set the backgroundcolor of grc "startHere" to tRGB ## oops need to fix this… > too light, disappears > set the foregroundcolor of fld _Quote to tRGB > # same hue for background, but light > repeat for each item tHue in tRGB > put tHue- (115) & comma after tTileBkgndRGB > end repeat > delete char -1 of tTileBkgndRGB > set the backgroundColor of grc quoteBkgnd to tTileBkgndRGB > end Switch > > The incoming nature photo that is the backdrop for the puzzle is a random > selection. The above is predicated on a user choice/save-as-setting, but a > preferred algorithm would be: > > Determine average overall brightness of the image. If below 130 on a scale of > 1-255, then use light colored type on dark tile… if >130 then use dark type > on light tiles > > So is there a fast, efficient way to analyze a 1200 X 800 px image to get > this average brightness value. > > > > ___ > use-livecode mailing list > use-livecode@lists.runrev.com > Please visit this url to subscribe, unsubscribe and manage your subscription > preferences: > http://lists.runrev.com/mailman/listinfo/use-livecode ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode
Fast Algorithm to Determine Average Brightness of an Image
Use Case: I want to place puzzle tiles on top of an image. We take a field, put a grc behind it, take a snapshot, crop each word off at -1000, -1000 and then scramble the tiles to make a puzzle… all this works really well. So I decided to dress it up a bit more: we dynamically choose a color for the foreground of the field in RGB range of 0-135 for all three colors → This gets us dark type… then add 100 to all the colors and this gets us a color for the bkgnd grc which is the same hue, but brighter (well, that depends on your preferred brand HSV, HSL, HSB LAB color theory, it's not that simple… but, close enough for this context) tiles look great.. color is randomized for each puzzle with the caveat of an occasional "ugly" (in relation to the nature photo in the background) but still… cool.. OK I thought hmmm we could also use light type with dark background and I got this working… too much fun.. Switch sUserColorRange case "darkType" set the itemdelimiter to "," # just to be safe put randomInRange(0,135) into tRGB put comma & randomInRange(0,135) after tRGB put comma & randomInRange(0,135) after tRGB set the backgroundcolor of grc "startHere" to tRGB set the foregroundcolor of fld _Quote to tRGB # same hue for background, but light repeat for each item tHue in tRGB put tHue+ (115) & comma after tTileBkgndRGB end repeat delete char -1 of tTileBkgndRGB set the backgroundColor of grc quoteBkgnd to tTileBkgndRGB break case "lightType" set the itemdelimiter to "," # just to be safe put randomInRange(136,255) into tRGB put comma & randomInRange(136,255) after tRGB put comma & randomInRange(136,255) after tRGB set the backgroundcolor of grc "startHere" to tRGB ## oops need to fix this… too light, disappears set the foregroundcolor of fld _Quote to tRGB # same hue for background, but light repeat for each item tHue in tRGB put tHue- (115) & comma after tTileBkgndRGB end repeat delete char -1 of tTileBkgndRGB set the backgroundColor of grc quoteBkgnd to tTileBkgndRGB end Switch The incoming nature photo that is the backdrop for the puzzle is a random selection. The above is predicated on a user choice/save-as-setting, but a preferred algorithm would be: Determine average overall brightness of the image. If below 130 on a scale of 1-255, then use light colored type on dark tile… if >130 then use dark type on light tiles So is there a fast, efficient way to analyze a 1200 X 800 px image to get this average brightness value. ___ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode