gfxPostProccess libary - image post proccessing for fb

User projects written in or related to FreeBASIC.
BunnX
Posts: 16
Joined: Feb 01, 2014 11:45

gfxPostProccess libary - image post proccessing for fb

Postby BunnX » Mar 20, 2014 11:24

It seems that I like to do something with pictures ;)

Here's my in C++ written "post processing" libary for freebasic.
It enables you to add some image and post proccessing filters to a gfx screen or fb image.
Not all effects are fast enough for games, but some may are useful.

Functions

Code: Select all

   
   gfxFill()         faster filling of buffers
   gfxCls()         faster cls
   gfxInvert()         invert image by color
   gfxGrayscale()       grayscales image by avergange RGB
   gfxBrightness()      adjust brightness (add)
   gfxContrast()      adjust contrast luminance
   gfxEmboss()         emboss effect
   gfxSephia()         warm sepia
   gfxSoftGlow()      softglow | bloom
   gfxColorAnd()      masks pixels with given value per and
   gfxPruneColor()      reduce color by channel shifting
   gfxRGBNoise()      generates RGB noise
   gfxBlur()         blur
   gfxSharpen()      sharpen
   gfxSobelGradient()   not so perfect sobel
   gfxMonochrome()      monochrome with dithering
   gfxFlipV()         flip image vertical ( h axis )
   gfxFlipH()         flip image horizontal ( v axis )
   gfxFlipHV()         flip image both axis
   gfxNTSC()         NTSC old TV filter


Usage:

Code: Select all

   #Include "gfxPostProcess.bi"
   
   screenRes x, y, 32      '32 bit is important
   gfxInitEffects()         'gfxInitEffects(imageBuffer) for using on an fb image
   do
      gfxCls()
      'painting stuff
      'painting stuff
      'when you are finished:
      gfxFlipHV()          ' or that effect that you want, or more effects at once.
   loop
   gfxDeInit()




Download

If anybody got some suggestions, please tell me. I would try to implement some more filters. I hope there are no big bugs, but if, then tell me, too :)

bunnX


.
Last edited by BunnX on Mar 20, 2014 23:43, edited 4 times in total.
BasicCoder2
Posts: 2685
Joined: Jan 01, 2009 7:03

Re: gfxPostProccess libary - image post proccessing for fb

Postby BasicCoder2 » Mar 20, 2014 13:08

@BunnX
Are you able give a link to the actual C++ source code?
Can you translate the C++ processing functions into FreeBasic?
Now if you can interface FreeBasic to roborealm that would be useful it even has webcam support!
http://www.roborealm.com/
BunnX
Posts: 16
Joined: Feb 01, 2014 11:45

Re: gfxPostProccess libary - image post proccessing for fb

Postby BunnX » Mar 20, 2014 14:32

Here's the source... it's not so good to release this now, cause it has the false name. First it should be named "gfxEffects", but it's to generall to reserve this name. Than is nothing commented and all is messy.

http://www.bunnxchat.de/gfxPostProcessSRC.zip

Yes you can.. But for me it's too much, I've made it now in c++. But it shouldn't be a big thing to copy some functions out and correct them in freebasic syntax, cause I made not use of any libaries :) Maybe I can write some functions to fb if you need it. Roborealm, never heard, I will take a look on.


BTW: I quickly wrote a new filter: NTSC CRT emulation. Should look like an old TV.
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

Re: gfxPostProccess libary - image post proccessing for fb

Postby TESLACOIL » Mar 20, 2014 18:18

ref some suggestions

Frame stacking and optical flow are two powerful techniques.

Handy for accurate noise reduction and image stabilization. Astronomy software is pretty good and there is a lot of quality freeware out there. Seeing well in abysmal conditions is what astronomers do 24/7/365 so they tend to use more advanced techniques than other fields. They are also shifting over to USB webcams so its getting a bit more plug n play.



Main issues with imaging 'anything'

Lack of direct code control over camera settings and the sheer diversity of hardware.

If you know what your camera is, its optical/data capture weaknesses and strengths, what its settings are AND can communicate that info to the 'image processing code' then you have complete knowledge and control over the system.

Post processing filters etc need to be fine tuned to the specific camera / camera settings /capture settings.

..as i said this is the 'main issue' and why i and others tend to code their own filters, because we can match them to the specific setup and in doing so get much better results. That said the code you post is still useful for seeing exactly what code tricks to deploy.
BasicCoder2
Posts: 2685
Joined: Jan 01, 2009 7:03

Re: gfxPostProccess libary - image post proccessing for fb

Postby BasicCoder2 » Mar 20, 2014 20:13

BunnX wrote: it shouldn't be a big thing to copy some functions out and correct them in freebasic syntax, cause I made not use of any libaries :) Maybe I can write some functions to fb if you need it.


In fact I have already written most of those visual processing functions in FreeBasic as the subject really interested me but was of no interest to other FreeBasic programmers. I think by sephia you mean sepia?

The last topic on visual processing was how to turn an image into an oil painting. The topic is however buried somewhere in the squares thread never to be found again :) Such long threads defeat the whole purpose of sharing good ideas unless they are taken out of squares and given a thread of their own.

This is my effort to turn an image into an oil painting if you want to see what it does to your demo.bmp

Code: Select all

Const iWIDTH = 700
Const iHEIGHT = 438
 
ScreenRes 800,480,32

'processing routines assume images exist as uinteger arrays
Dim shared As Any Ptr lpImage
lpImage = ImageCreate(iWIDTH,iHEIGHT) 'used to load/display/save images as bitmaps

'global variables store image arrays
Dim Shared As Uinteger image1(iWIDTH,iHEIGHT)
Dim Shared As Uinteger image2(iWIDTH,iHEIGHT)


Sub loadImage(image1() as uinteger,file as string)
    Dim As integer i,imgW,imgH
    dim as uinteger ptr iPtr
    bload file,lpImage
    ImageInfo lpImage,imgW,imgH,,,iPtr
    'copy lpImage into image1()
    i = 0
    For y As Integer = 0 To iHEIGHT-1
        For x As Integer = 0 To iWIDTH-1
            image1(x,y) = iPtr[i]
            i = i + 1
        Next x
    Next y   
End Sub

Sub displayImage(image1() As Uinteger)
    Dim As Integer i,imgW,imgH
    dim as uinteger ptr iPtr
    ImageInfo lpImage,imgW,imgH,,,iPtr
    'copy image1() into lpImage
    i=0
    For y As Integer = 0 To iHEIGHT-1
        For x As Integer = 0 To iWIDTH-1
            iPtr[i] = image1(x,y)
            i = i + 1
        Next x
    Next y
    Put (0,0), lpImage,Pset
End Sub

sub oilify(image2() as uinteger,image1() as uinteger,radius as integer)
    dim as integer r,g,b,intensity,i,max,maxIndex
    intensity = 25
    dim as integer intensityCount(256)
    dim as uinteger pixel
    dim as integer sumR(256),sumG(256),sumB(256)
   
    for y as integer = 0 to iHeight-radius
        for x as integer = 0 to iWidth-radius
            'clear table of values
            for ii as integer = 0 to 255
                sumR(ii)=0
                sumG(ii)=0
                sumB(ii)=0
                intensityCount(ii)=0
            next ii
            'find intensity of rgb value and apply intensity level
            for yy as integer = -radius to radius
                for xx as integer = -radius to radius
                    pixel = image1(x+xx,y+yy)
                    r = pixel shr 16 and 255
                    g = pixel shr 8 and 255
                    b = pixel and 255
                    'find intensity of rgb value and apply intensity level
                    'intensity = (r+g+b)\3'*intensity)/255
                    intensity = (((r+g+b)/3.0)*25)/255
                    if intensity > 255 then
                        intensity = 255
                    end if
                    i = intensity
                    intensityCount(i)=intensityCount(i)+1
                    'sum of each pixel value is calculated
                    sumR(i) = sumR(i)+r
                    sumG(i) = sumG(i)+g
                    sumB(i) = sumB(i)+b
                next xx
            next yy
           
            'find intensity with highest occurence
            max = 0
            maxIndex = 0
            for ii as integer = 0 to 255
                if intensityCount(ii)>max then
                    max = intensityCount(ii)  'largest value so far
                    maxIndex = ii             'found at index ii
                end if
            next ii
           
            r = sumR(maxIndex)/max  'max = number of pixels with this intensity
            g = sumG(maxIndex)/max
            b = sumB(maxIndex)/max
           
            image2(x,y)=rgb(r,g,b)
           
        next x
    next y

end sub

sub copy(image1() as uinteger, image2() as uinteger)
    for y as integer = 0 to iHeight-1
        for x as integer  = 0 to iWidth-1
            image1(x,y)=image2(x,y)
        next x
    next y
end sub

' =======  MAIN PROGRAM =========
dim as string key
'loadImage(image1(),"C:\FreeBasic\bitmaps\Views\view1.bmp")
loadImage(image1(),"demo.bmp")
    displayImage(image1())       'view original
    sleep
    oilify(image2(),image1(),2)  'radius = 2 (5x5 matrix)
    displayImage(image2())       'view result
    sleep
imageDestroy(lpImage)

End




Roborealm, never heard, I will take a look on.


It is a project that has become commercial. It allows you to grab images from a webcam or other video source and make a sequence of calls to process the image. You can probably download a trial version to play with. I could have used it with FreeBasic had I been able to interface it. Roborealm supports the most popular languages. Unfortunately there was zero interest by other FreeBasic programmers in visual processing and visual recognition.
BunnX
Posts: 16
Joined: Feb 01, 2014 11:45

Re: gfxPostProccess libary - image post proccessing for fb

Postby BunnX » Mar 20, 2014 23:19

@TESLACOIL

Yes, everybody can do this on their own, I only want to provide a collection that's not meant to be professional (if I get you right, I'm german ;) ). I think this lib can be usefull for people that won't to code this at thier own and making games. So they can simulate night/day, play video sequences with the NTSC effekt, or do something with the screen if the player gets hit... like a software-pixelshader. Not to correct an image in a professionel way. Or for education like said. Apart from that, you're right.

@BasicCoder2

Yes I mean sepia, I'll correct this. Your oilify filter is great! I'm allowed to implement this in the libary? Roborealm listens interessant, I'll see.

btw: Everybody is invitet to improve or add something, only the name should keep the same. The Sobel filter isn't so good for example an the bloom could be better, too...
BasicCoder2
Posts: 2685
Joined: Jan 01, 2009 7:03

Re: gfxPostProccess libary - image post proccessing for fb

Postby BasicCoder2 » Mar 21, 2014 2:16

BunnX wrote: Your oilify filter is great! I'm allowed to implement this in the libary?

Found the topic.

viewtopic.php?f=3&t=16207&p=181057&hilit=oil+squares#p181057

It seems I translated it from a codeproject.
BasicCoder2
Posts: 2685
Joined: Jan 01, 2009 7:03

Re: gfxPostProccess libary - image post proccessing for fb

Postby BasicCoder2 » Mar 21, 2014 2:46

BunnX wrote:btw: Everybody is invitet to improve or add something, only the name should keep the same. The Sobel filter isn't so good for example an the bloom could be better, too...

And I assume you mean to add to the C++ library?

I started building an image processing library written in FreeBASIC although I haven't done anything since 2012 when the program below was written. I use plain vanilla uinteger or ubyte arrays because they are common to all languages and are readable to someone limited to programming in BASIC. Once the logic is worked out I can always write a faster if less readable version later.

So a images are passed as arrays like this,
Sub HFbypass(image1() as uinteger, image2() as uinteger)

Code: Select all

Const iWIDTH = 640
Const iHEIGHT = 480
 
ScreenRes 800,480,32

'processing routines assume images exist as uinteger arrays
Dim shared As Any Ptr lpImage
lpImage = ImageCreate(iWIDTH,iHEIGHT) 'used to load/display/save images as bitmaps

'global variables store image arrays
Dim Shared As Uinteger image1(iWIDTH,iHEIGHT)
Dim Shared As Uinteger image2(iWIDTH,iHEIGHT)


Sub loadImage(image1() as uinteger,file as string)
    Dim As integer i,imgW,imgH
    dim as uinteger ptr iPtr
    bload file,lpImage
    ImageInfo lpImage,imgW,imgH,,,iPtr
    'copy lpImage into image1()
    i = 0
    For y As Integer = 0 To iHEIGHT-1
        For x As Integer = 0 To iWIDTH-1
            image1(x,y) = iPtr[i]
            i = i + 1
        Next x
    Next y   
End Sub

Sub displayImage(image1() As Uinteger)
    Dim As Integer i,imgW,imgH
    dim as uinteger ptr iPtr
    ImageInfo lpImage,imgW,imgH,,,iPtr
    'copy image1() into lpImage
    i=0
    For y As Integer = 0 To iHEIGHT-1
        For x As Integer = 0 To iWIDTH-1
            iPtr[i] = image1(x,y)
            i = i + 1
        Next x
    Next y
    Put (0,0), lpImage,Pset
End Sub

' ========= create HFMASK =========
dim shared as integer HFmask(5,5)
for j as integer = 0 to 4
    for i as integer = 0 to 4
        read HFmask(i,j)
    next i
next j
data  0,-1,-1,-1, 0
data -1, 2,-4, 2,-1
data -1,-4,13,-4,-1
data -1, 2,-4, 2,-1
data  0,-1,-1,-1, 0


'==================================
 
Sub HFbypass(image1() as uinteger, image2() as uinteger)
   dim as integer pixel,r,g,b,sRed,sGrn,sBlu
   for y as integer = 2 to iHEIGHT-3
      for x as integer = 2 to iWIDTH-3
          sRed = 0
          sGrn = 0
          sBlu = 0
          for yy as integer = -2 to 2
              for xx as integer = -2 to 2
                 
                  'extract colors
                  pixel = image2(x+xx,y+yy)
                  r = pixel shr 16 and 255
                  g = pixel shr  8 and 255
                  b = pixel and 255
                 
                  sRed = sRed + r * HFmask(xx+2,yy+2)
                  sGrn = sGrn + g * HFmask(xx+2,yy+2)
                  sBlu = sBlu + b * HFmask(xx+2,yy+2)
                 
              next xx
          next yy
          sRed = sRed*2
          sGrn = sGrn*2
          sBlu = sBlu*2
          if sRed<0 then sRed = 0
          if sRed>255 then sRed = 255
          if sGrn<0 then sGrn = 0
          if sGrn>255 then sGrn = 255
          if sBlu<0 then sBlu = 0
          if sBlu>255 then sBlu = 255
          image1(x,y) = rgb(sRed,sGrn,sBlu)
      next x
   next y
End Sub

' =======  MAIN PROGRAM =========
    loadImage(image1(),"demo.bmp")
    displayImage(image1())       'view original
    sleep
    HFbypass(image2(),image1())
    displayImage(image2())       'view result
    sleep

imageDestroy(lpImage)

End
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

Re: gfxPostProccess libary - image post proccessing for fb

Postby TESLACOIL » Mar 21, 2014 21:05

My main interest is due to my involvement in vision systems for androids. Its been a fascinating voyage of discovery, every line of code allows you to see the world in a different way.

One of my favorite code snipets s has been "only display stable pixels" (with a little bit of buffering on the side)

This is great fun when you are capturing at a high frame rate and moving the camera around. Strange ghostly figures slew across the screen and coalesce out of the fairy dust when ever you stop moving.


*spent ages looking for a bug in my motion detection code only to discover that the curtains where ever so gently moving in the breeze and there was a bug crawling on the carpet rather than in my code. ( lots of false alarms, or so i thought )

Return to “Projects”

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests