Page 1 of 1

PNG count not transparent pixels

Posted: Sun Jan 01, 2017 1:32 am
by infratec
Hi,

I needed to compare the area of 2 estates.
Ok, I used google maps and grabbed the pictures.
I loaded the pictures in gimp and cut out the estates.
Then I saved each estate as png with transparence.

Now I needed only a tool to count the pixels.
After a short time of searching I used my 'swiss army knife' called PB :mrgreen:

Code: Select all

UsePNGImageDecoder()

Filename$ = OpenFileRequester("Choose a file", "", "PNG|*.png", 0)
If Filename$ <> ""
  
  If LoadImage(0, Filename$)
    
    w = ImageWidth(0)
    h = ImageHeight(0)
    
    If StartDrawing(ImageOutput(0))
      DrawingMode(#PB_2DDrawing_AllChannels)
      
      For y = 0 To h - 1
        For x = 0 To w - 1
          P = Point(x, y)
          If Alpha(P) <> 0
            ;Debug Str(x) + "x" + Str(y) + " " + Str(Alpha(P))
            Count + 1
            Plot(x, y, $FF0000FF)
          EndIf
        Next x
      Next y
      
      StopDrawing()
      
      SaveImage(0, Filename$ + ".bmp", #PB_ImagePlugin_BMP)
      
      MessageRequester("Info", "Found " + Str(Count) + " pixel")
      
    EndIf
  EndIf
EndIf
As test for the truth I store a bmp with all counted pixels in red.
In my case it worked.

Maybe someone can use it too.

Happy new year,

Bernd

Re: PNG count not transparent pixels

Posted: Sun Jan 01, 2017 12:59 pm
by Bisonte
/OT
infratec wrote:After a short time of searching I used my 'swiss army knife' called PB :mrgreen:
I love this moments :D

OT/

Re: PNG count not transparent pixels

Posted: Sun Jan 01, 2017 5:37 pm
by infratec
An extended version:

Code: Select all

EnableExplicit

#Size = 380

Enumeration
  #OrigPicture
  #SmallOrigPicture
  #SmallTestPicture
EndEnumeration


Define.i w, h, x, y, P, Count
Define.f FactorX, FactorY, Factor
Define Filename$

UsePNGImageDecoder()

Filename$ = OpenFileRequester("Choose a file", "", "PNG|*.png", 0)
If Filename$ <> ""
  
  If LoadImage(#OrigPicture, Filename$)
    
    w = ImageWidth(#OrigPicture)
    h = ImageHeight(#OrigPicture)
    
    CopyImage(#OrigPicture, #SmallOrigPicture)
    
    FactorX = #Size / w
    FactorY = #Size / h
    
    If FactorX < FactorY
      Factor = FactorX
    Else
      Factor = FactorY
    EndIf
    
    If Factor < 0
      Factor = 1.0
    EndIf
    
    ResizeImage(#SmallOrigPicture, w * Factor, h * Factor)
    
    OpenWindow(0, 0, 0, w * Factor * 2 + 30, h * Factor + 30 + 20, "PixelCount", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
    
    ImageGadget(0, 10, 10, 0, 0, ImageID(#SmallOrigPicture), #PB_Image_Border)
    ImageGadget(1, 10 + w * Factor + 10, 10, w * Factor, h * Factor, 0, #PB_Image_Border)
    
    CreateStatusBar(0, WindowID(0))
    AddStatusBarField(200)
    
    
    If StartDrawing(ImageOutput(#OrigPicture))
      DrawingMode(#PB_2DDrawing_AllChannels)
      
      For y = 0 To h - 1
        For x = 0 To w - 1
          P = Point(x, y)
          If Alpha(P) <> 0
            ;Debug Str(x) + "x" + Str(y) + " " + Str(Alpha(P))
            Count + 1
            Plot(x, y, $FF0000FF)
          EndIf
        Next x
      Next y
      
      StopDrawing()
            
      CopyImage(#OrigPicture, #SmallTestPicture)
      ResizeImage(#SmallTestPicture, w * Factor, h * Factor)
      SetGadgetState(1, ImageID(#SmallTestPicture))
      
      If Count = 0
        StatusBarText(0, 0, "no transparent PNG", #PB_StatusBar_Center)
      Else
        StatusBarText(0, 0, "Found " + Str(Count) + " visible pixels", #PB_StatusBar_Center)
      EndIf
      
    EndIf
    
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
    
  EndIf
EndIf
Bernd

Re: PNG count not transparent pixels

Posted: Sun Jan 01, 2017 11:26 pm
by netmaestro
Nice idea, kind of like dropping a blob of butter into a measuring cup that contains a predetermined amount of water. Butter raises the level, by how much tells you the volume of butter.

Switching to estates, if you know the greatest distance North to South and the greatest distance East to West covered by the estate, say it's 100m each way just for an example, the total would be 10,000 square meters. You could calculate the sqare meters of the estate like this:

number of visible px
----------------------- X 10,000
total number of px

which looks to me like a fair way to get the number of square meters covered by the estate.