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.
Image manipulation
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Image manipulation
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?
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
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
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).
The code:-
I hope this helps.
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).
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
...and here is an example using my code.
The following 2 images where time-lapse photos taken with my trail-cam:
...and here is the result...
only unique pixels in image1 are reproduced in image3.
The following 2 images where time-lapse photos taken with my trail-cam:
...and here is the result...
only unique pixels in image1 are reproduced in image3.
Re: Image manipulation
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
Here's a forum thread on how to do it with ImageMagick:
http://www.imagemagick.org/discourse-se ... hp?t=16279
Re: Image manipulation
But steve's solution is way cooler
Re: Image manipulation
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
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