Page 1 of 1

Image manipulation

Posted: Monday 5th March 2018 12:14pm
by RichardB
Hi folks. I'm trying to write a piece of code to perform some image manipulation - specifically, to mathematically subtract one image from another, and display the result. (The program aims to show changes in ground movement over time using aerial or satellite images.) I'm not having much luck in finding anything in the documentation for this kind of function using images. Can anyone give me some pointers on how to do this?

Thanks.

Re: Image manipulation

Posted: Monday 5th March 2018 12:50pm
by cogier
Hi RichardB and welcome to the forum.

Have you got a couple of pictures you can show us as an example of what you are trying to do? What type of file compression are you using as I think this would not be possible with .jpg files?

Re: Image manipulation

Posted: Monday 5th March 2018 5:45pm
by stevedee
RichardB wrote: Monday 5th March 2018 12:14pm ...I'm trying to write a piece of code to perform some image manipulation - specifically, to mathematically subtract one image from another...
I can't see a ready made function for this in Gambas. So you would either have to cycle through the 2 dimensional array for each of the 2 images, and subtract each value, or maybe call an external script to do the work (maybe using GIMP).

See also:-
https://uk.mathworks.com/help/images/re ... tract.html
http://gambaswiki.org/wiki/comp/gb.image/image

Re: Image manipulation

Posted: Monday 5th March 2018 8:05pm
by stevedee
Here is my very basic test code that seems to work.

Create a new project, make sure component gb.Image is included.

Add 2 textboxes and 1 button. Pre-load the text boxes with the paths of 2 equal sized images (I used jpegs).
ImageSub.png
ImageSub.png (19.01 KiB) Viewed 9739 times
The code:-
Public Sub Button1_Click()
Dim indexX As Integer
Dim indexY As Integer
Dim myImage1 As Image
Dim myImage2 As Image
Dim myImage3 As Image

  If Exist(txtImage1.Text) And Exist(txtImage2.Text) Then
    Me.Text = "2 images found"
    myImage1 = Image.Load(txtImage1.Text)
    myImage2 = Image.Load(txtImage2.Text)
    myImage3 = myImage1
    For indexX = 0 To myImage1.Width - 1
      For indexY = 0 To myImage2.Height - 1
        If myImage1[indexX, indexY] - myImage2[indexX, indexY] < 0 Then
          myImage3[indexX, indexY] = 0
        Else
          myImage3[indexX, indexY] = myImage1[indexX, indexY] - myImage2[indexX, indexY]
        Endif
      Next
    Next
    myImage3.Save("/home/steve/Gambas/ImageManipulation/image3.jpg")
  Else
    Me.Text = "2 images required"
  Endif
Catch
  Me.Text = "ERROR: " & Error.Text
End
I couldn't remember how to create a blank image the right size (for myImage3) so I just copied myImage1 into myImage3.

I hope this helps.

Re: Image manipulation

Posted: Monday 5th March 2018 8:18pm
by stevedee
...and here is an example using my code.

The following 2 images where time-lapse photos taken with my trail-cam:
image1.jpg
image1.jpg (106.84 KiB) Viewed 9739 times
image2.jpg
image2.jpg (107.07 KiB) Viewed 9739 times
...and here is the result...
image3.jpg
image3.jpg (7.36 KiB) Viewed 9739 times
only unique pixels in image1 are reproduced in image3.

Re: Image manipulation

Posted: Monday 5th March 2018 8:38pm
by jornmo
You can also utilize ImageMagick through SHELL/EXEC.

Here's a forum thread on how to do it with ImageMagick:
http://www.imagemagick.org/discourse-se ... hp?t=16279

Re: Image manipulation

Posted: Monday 5th March 2018 8:40pm
by jornmo
But steve's solution is way cooler 8-)

Re: Image manipulation

Posted: Monday 5th March 2018 10:41pm
by RichardB
Hi all, and thank you for the welcome.

I really appreciate everyone's help, and special thanks to steve - that looks like the sort of solution I've been chasing. Unfortunately I'm at work at the moment, so I won't be able to test out your code example until I go home. I'll let you know how it turns out.

Thanks again.
-RB