Get mean luminance of parts of a bitmap

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Get mean luminance of parts of a bitmap

I have two images with the same size:

**GG*     G stands for green

**LL*      L is the part of the image which is interesting for analysis

Now I want to have the mean value of luminance of the part of the second image which is at the same location as the green color in Image 1. The green color can change its position, you have to analyse both images together.

I solved the problem but it is very slow and I did not use the bitmap class. I include the code at the end.

Perhaps there is anybody here who can speed this codesnippet up using the special squeak benefits.

Thank you in advance.


The code I made:

        | sumLuminance count sensitiveColor r nxtImage imageBelowMe oldVis |
        ((self objName) compare: 'NXT') =2 ifFalse:[^ self].

        sumLuminance _ 0. count _ 0.

        sensitiveColor _ Color green.

        r _ self bounds intersect: ((self owner) bounds).

        nxtImage _  Form extent: r extent depth: 16.
        imageBelowMe _  Form extent: r extent depth: 16.

        oldVis _ self visibility. self visibility: 100.
        self drawOn: ((FormCanvas on: nxtImage) copyOffset: r topLeft negated).
        self visibility: oldVis.

        "This makes only a picture of the stage ignoring sprite below the NXT-sprite."
        (self owner) drawOn: ((FormCanvas on: imageBelowMe) copyOffset: r topLeft negated).

        "This makes a picture of anything below the NXT-sprite."
        "(aSprite owner) patchAt: r withoutWatchersAnd: aSprite  andNothingAbove: true."

        1 to: nxtImage width do: [:x |
                1 to: nxtImage height do: [:y |
                                "Transcript show: (nxtImage colorAt: x@y); cr."
                        (nxtImage colorAt: x@y) = sensitiveColor
                        ifTrue: [
                                sumLuminance _ sumLuminance + ((imageBelowMe colorAt:x@y) luminance).
                                count _ count +1.].].].

        count > 0 ifTrue: [^ (sumLuminance/count).] ifFalse: [^ (-1)].