Setting different colors to differences between two images

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
Samir
Posts: 3
Joined: 2018-06-13T02:04:45-07:00
Authentication code: 1152

Setting different colors to differences between two images

Post by Samir » 2018-06-13T02:19:31-07:00

Trying to find the difference between two image and display image1 differences in Red and the image2 in blue. Basically the first image is the older one and image2 is the newer image, so we want to see what has been removed in red and what has been added in blue. If we use the composite so the image combined would show the pixels that didn't change in the original grey scale or could be black. When we use the following code there are a lot of edges that come as slightly change because the pixels might be off slightly. I assume we would use fuzz to remove some of this, but I don't know how.

Using vb.net to program and using version 7.4.3.0

Here is my latest attempt and it seems to show some changes in yellow and blue for the new additions. most of the lines are showing a yellow pixelation with alot of the lines even though their isn't a difference between the images. These images are plans for a building so it is typically lines of black and some greyscale.

Code: Select all

Using image1 As New MagickImage(CurrentBitmap)
    Using image2 As New MagickImage(PreviousBitmap)

      Dim diffImage = New MagickImage()
      Dim overlayBytes As Byte()
      Dim s As New CompareSettings
      s.HighlightColor = MagickColor.FromRgba(0, 0, 255, 180) 'setting Red color to be the Highlighted color
      s.LowlightColor = MagickColor.FromRgba(0, 0, 0, 180)
      image1.Compare(image2, s, diffImage)
      image1.Composite(diffImage, CompositeOperator.Difference)
      Dim overlayStream As New MemoryStream()
      image1.Write(overlayStream, MagickFormat.Jpeg)
      overlayBytes = overlayStream.ToArray()
      overlayStream.Close()

      Response.Clear()
      Response.ClearHeaders()
      Response.ClearContent()
      Response.Buffer = True
      Response.ContentType = "image/jpeg"
      If Request.Headers.Item("User-Agent").Contains("Edge") AndAlso IsNothing(Request.Headers.Item("GetContentFeatures.DLNA.ORG")) Then
          Dim bTemp As Byte()
          Response.BinaryWrite(bTemp) 'Empty output
          Response.Flush()
          Response.SuppressContent = True
          HttpContext.Current.ApplicationInstance.CompleteRequest()
      End If
      'Normal process:
      Response.AddHeader("Content-Disposition", "filename=""" & Session("FileName") & ".jpg""")
      Response.AddHeader("Content-Length", overlayBytes.Length)
      Response.BinaryWrite(overlayBytes.ToArray())

      Response.Flush()
      Response.End()
  End Using
End Using

snibgo
Posts: 10977
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Setting different colors to differences between two images

Post by snibgo » 2018-06-13T04:17:40-07:00

I don't use VB so can't comment on those aspects.

Can you link to sample input images? That would help people to understand your problem.

When we have two images, we can easily find which pixels are different. But if we also need to find which pixels have "added" or "removed", what does that mean? Perhaps that pixels have been changed from some base colour, or changed back to the base colour. If so, and we know the base colour, then we can also find those "added" and "removed" pixels.
snibgo's IM pages: im.snibgo.com

Samir
Posts: 3
Joined: 2018-06-13T02:04:45-07:00
Authentication code: 1152

Re: Setting different colors to differences between two images

Post by Samir » 2018-06-14T21:04:20-07:00

Here are the images that we are trying to compare. Ideally we would like to be able choose the colors for the what was removed from version 1 from what is in version 2. Typically that would be a lighter color and what are the changes (additions) in verison2 would show in a darker color.

Version 1:
https://bidtracer.box.com/s/lgnxapf2x7f ... yidj9ljc44

Version 2:
https://bidtracer.box.com/s/y5r3pvi3qxr ... tu8ff691ae

Another thing is a lot of times the images are off by a couple pixels because of the conversion, so we would need to add a fuzz factor so that it does not show most things as some change.

snibgo
Posts: 10977
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Setting different colors to differences between two images

Post by snibgo » 2018-06-15T03:05:48-07:00

1. Never use JPEG inputs if you can avoid it. JPEG compression usually changes pixels, so white pixels are no longer exactly white, etc. However, these examples seem okay.

2. These samples do align, so we don't have an "off by a couple pixels" problem. If we did, it would probably be best to align the images.

3. We have a base colour, which is within about 8% of white (it is mostly white, except for the large area top-left). So "adding detail" means close-to-white pixels in version1 are darker than close-to-white in version2, and "removing detail" means dark pixels in version1 are close-to-white in version2.

We easily find which pixels are different: mpr:DIFF (diff.png) is a mask that is black where pixels are unchanged, or some shade of gray.

To find added detail, we take mpr:DIFF and blacken it where version2 is close-to-white. The non-black pixels are where detail has been added.

To find removed detail, we take mpr:DIFF and blacken it where version1 is close-to-white. The non-black pixels are where detail has been removed.

Windows BAT script:

Code: Select all

set WHITE_TOL=8

%IMG7%magick ^
  ( M1.0301-version1.jpg +write mpr:V1 ) ^
  ( M1.0301-version2.jpg +write mpr:V2 ) ^
  -compose Difference -composite ^
  +write diff.png ^
  +write mpr:DIFF ^
  ( mpr:V2 ^
    -fill Black -fuzz %WHITE_TOL%%% +opaque White ^
    -fill White -fuzz 0 +opaque Black -negate ^
    +write add.png ^
  ) ^
  -compose Darken -composite ^
  +write added.png ^
  +delete ^
  mpr:DIFF ^
  ( mpr:V1 ^
    -fill Black -fuzz %WHITE_TOL%%% +opaque White ^
    -fill White -fuzz 0 +opaque Black -negate ^
    +write rem.png ^
  ) ^
  -compose Darken -composite ^
  +write removed.png ^
  NULL:
added.png and removed.png are black where pixels are unchanged, otherwise non-black.

We can use these to make an annotated version of the drawing:

Code: Select all

%IMG7%magick ^
  M1.0301-version1.jpg ^
  ( added.png ^
    ( +clone ^
      -fill Blue -colorize 100 ^
    ) ^
    +swap ^
    -alpha off -compose CopyOpacity -composite ^
    +write x.png ^
  ) ^
  -compose Over -composite ^
  ( removed.png ^
    ( +clone ^
      -fill Red -colorize 100 ^
    ) ^
    +swap ^
    -alpha off -compose CopyOpacity -composite ^
    +write x.png ^
  ) ^
  -compose Over -composite ^
  annot.png
annot.png is the version1 drawing, overlaid with blue for additions and red with removals.
snibgo's IM pages: im.snibgo.com

Samir
Posts: 3
Joined: 2018-06-13T02:04:45-07:00
Authentication code: 1152

Re: Setting different colors to differences between two images

Post by Samir » 2018-06-15T22:07:36-07:00

Thank you for the explanation. I am having issues transforming this to VB.net. Is there some reference I can use to help me figure out the conversion?

snibgo
Posts: 10977
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Setting different colors to differences between two images

Post by snibgo » 2018-06-16T18:51:55-07:00

I don't use VB or Magick.net, so can't help there. Aside from reading/write files, and list manipulation which you don't need, the only operations used in my commands are:

-composite
+opaque
-negate
-colorize

So you need to find the functions that correspond to those four operations. You may find the Magick.NET source helpful, https://github.com/dlemstra/Magick.NET/ ... ickImage.c , eg search in there for "colorize".
snibgo's IM pages: im.snibgo.com

Post Reply