2

This might be easy, but I'm still struggling a bit. How do I compare two images if I want to find out if they are identical? Say I have an image A and and image B I want to do something like

if ( A == B ) ...

but this does not work.

2 Answers2

2

I think you need to specify a bit more clearly what you mean by identical. Within the framework of image-analysis in DigitalMicrograph, it could be (f.e.):

1) The identical file on disc.

This has been answered by others. But for simplicity - if an image is open in DM you can find out to which "file" it is linked by the following code:

image img := GetFrontImage()
imageDocument doc = img.ImageGetOrCreateImageDocument()
OKDialog("--> "+doc.ImageDocumentGetCurrentFile())

Note, that files on disc are linked to imageDocuments, not images.

2) The identical pixel values in all pixels

Simply compare the sum of the image difference!

image A
image B
if ( 0 == sum(A-B) ) OKDialog( "Identical!" )

The code assumes that both images are of same type, but you can check types separately.

3) The identical memory object

You may have multiple variables referring to the same 'image', i.e. you have one image as a local variable and then iterate through a list of images to identify that image in the list. To do so, you can use either the imageLabel, or the imageID. Any image in memory gets assigned each of those automatically. While imageLabels are re-used when the become free, imageIDs are monotonically increased from application start (starting with 1). I think imageID is the better one to use in general.

image A
image B
if ( A.ImageGetLabel() == B.ImageGetLabel() ) OKDialog( "Identical!" )
if ( A.ImageGetID() == B.ImageGetID() ) OKDialog( "Identical!" )

4) The image is a specific 'unique' image, but it has been saved and opened again.

A typical scenario here are survey-images linked to data. You might want to open data and a survey-image and check, if it is the 'correct' survey-image. For this, there is a specific script-object called "Global Image ID" which you will find information about in the F1 help-section in the section 'Objects'. It is similar to a check-sum, but not quite one. This ID consists of 4 long-values which are created when the image is created and never changed afterwards. (So changing an image data value or tag will NOT change this global-ID.) The main difference to the imageID above is, that the Global ID remains when you save & load an image.

Global Image ID

BmyGuest
  • 6,331
  • 1
  • 21
  • 35
  • Thank you so much. It was the 3rd example I wanted to have. But the other examples are also great! –  Apr 30 '15 at 11:05
0

When checking if files are the same (and therefore images should logically follow) you can make an MD5 checksum of both files and compare those. If the checksums are the same, then the files are the same, so the images must be the same.

gabe3886
  • 4,235
  • 3
  • 27
  • 31
  • Thank you very much for your answer. A good idea for having files 'identical', but my application was different. I wanted to compare two DigitalMicrograph 'images' while they are open (and not even saved.) –  Apr 30 '15 at 11:03