B4J Question Comparison of two images

RichardN

Well-Known Member
Licensed User
Longtime User
I am using the mp3agic-0.9.1.jar to extract cover art from a large number of MP3 files in order to detect if it conforms to a certain default image.

I have confirmed that the cover art is being correctly extracted as expected, but it appears that despite Log(DefaultImage) returning the same javafx.scene.image.Image@xxxxxxxx signature every time the two images cannot be directly compared using... If Image1 = Image2 Then...

What is the correct method of doing this?

Compare two images:
Dim DefaultImage As Image
DefaultImage.Initialize(File.DirAssets,"default.jpg")
Dim IsDefaultImage As Boolean

For Each FileName As String In FileNames
                
    TrackInfo = ReadMP3File(CONTENTPATH & FileName)
                
    If TrackInfo.CoverArt = DefaultImage Then           'This direct comparison never returns True
        IsDefaultImage = True
    Else
        IsDefaultImage = False
    End If
            
Next
 

agraham

Expert
Licensed User
Longtime User
I would have expected trying to compare image objects directly might have thrown an exception but as it doesn't I'm guessing the the comparison is an identity comparison which will always fail as DefaultImage and TrackInfo.CoverArt are different objects. What you call a signature is not referencing the contained image but is referencing the Image object itself.

Comparing images is tricky. You might think you could do a byte by byte comparison but as the images are, I guess, jpgs even slight differences in the original compression and decompression implementations will produce different pixel values for the images. Overall it's not easy

 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
@agraham .... I can't argue with any of that.

Perhaps 'signature' was the wrong word to use. I put Log(TrackInfo.CoverArt) and Log(DefaultImage) merely to check I was comparing objects like for like..... which it appears I am.

My file 'default.jpg' was actually the one embedded into the .mp3 files in the first place, albeit using third party software. I will start by extracting that as a file and comparing it to the original. If all else fails I will extract the first X bytes from a known matching embedded cover art and then subsequently test for that with each file.
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
Are you trying to check if the images are exactly the same? Or kind of similar?
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
tchart said:
Are you trying to check if the images are exactly the same? Or kind of similar?
To re-cap... my purpose was to actually detect the presence of a certain album-art tag and then manipulate other data if found.

I had it in mind that MP3 tags are effectively 'tagged-on' to the end of the audio data. With that presumption I thought that if I added some cover-art to an MP3 and then extracted it again I would get the same file back... byte-for-byte. This did not work out as I expected hence my OP.

Clearly, writing code to compare bitmaps is potentially a PhD level subject so in fact it has proven to be much more code/time efficient to read the byte sequence of the file detecting the start of the tag followed by the initial byte sequence of the graphic in question. Old school but effective!
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User

Well thats kind of why I was asking.

If you just want to check if the image matches a known sample then I'd just use a CRC/Checksum on the file content. That would work. That would be fast than doing a byte by byte check.

RE comparing images, I do have a library that will compare two images and tell you (as a percent) whether they match but this doesnt sound like what you are attempting.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…