Image manipulation

Post

Posted
Rating:
#1 (In Topic #160)
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
 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?
Online now: Yes Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
stevedee is in the usergroup ‘Regular’

RichardB said

…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/ref/imsubtract.html
/comp/gb.image/image - Gambas Documentation
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
stevedee is in the usergroup ‘Regular’
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).

Image

(Click to enlarge)


The code:-

Code (gambas)

  1. Public Sub Button1_Click()
  2. Dim indexX As Integer
  3. Dim indexY As Integer
  4. Dim myImage1 As Image
  5. Dim myImage2 As Image
  6. Dim myImage3 As Image
  7.  
  8.   If Exist(txtImage1.Text) And Exist(txtImage2.Text) Then
  9.     Me.Text = "2 images found"
  10.     myImage1 = Image.Load(txtImage1.Text)
  11.     myImage2 = Image.Load(txtImage2.Text)
  12.     myImage3 = myImage1
  13.     For indexX = 0 To myImage1.Width - 1
  14.       For indexY = 0 To myImage2.Height - 1
  15.         If myImage1[indexX, indexY] - myImage2[indexX, indexY] < 0 Then
  16.           myImage3[indexX, indexY] = 0
  17.         Else
  18.           myImage3[indexX, indexY] = myImage1[indexX, indexY] - myImage2[indexX, indexY]
  19.         Endif
  20.       Next
  21.     Next
  22.     myImage3.Save("/home/steve/Gambas/ImageManipulation/image3.jpg")
  23.   Else
  24.     Me.Text = "2 images required"
  25.   Me.Text = "ERROR: " & Error.Text
  26.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
stevedee is in the usergroup ‘Regular’
…and here is an example using my code.

The following 2 images where time-lapse photos taken with my trail-cam:

Image

(Click to enlarge)


Image

(Click to enlarge)


…and here is the result…

Image

(Click to enlarge)


only unique pixels in image1 are reproduced in image3.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
jornmo is in the usergroup ‘Regular’
You can also utilize ImageMagick through SHELL/EXEC.

Here's a forum thread on how to do it with ImageMagick:
Simple subtract - Legacy ImageMagick Discussions Archive

Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
jornmo is in the usergroup ‘Regular’
But steve's solution is way cooler  8-)

Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
 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
Online now: No Back to the top
1 guest and 0 members have just viewed this.