Re: Improving Performane to compare JPEG files

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Another couple of things about your current method, JPEG files use lossy
compression to squeeze down the size of the image files, this means that
they actually throw some of the data from the original image away when
saving it and replace it with easily compressible approximations instead.
This means that even two seemingly similar images may actually be detected
as nearly completely different because of the compression artefacts in the
image after decompressing. Whilst basic tolerance checking does help to
alleviate this problem it is not a great solution since if the tolerance is
set too low then compression artefacts pollute the results, if set too high
then real image changes will start to get missed. A far better solution it
to work with uncompressed (or losslessly compressed) images as your source
data if you have access to them.
Also your tolerance checking won't work very well in your current
implementation because you're checking the entire packed colour value as a
single value rather than as 3 separate channels. This will work fine for
colour's where the red channel changes a little, but if either of the other
two channels change even slightly it will be detected as a huge change.
To fix this, break your colour value down into three bytes values and
compare those:

'***
Dim RedSrc, GreenSrc, BlueSrc As Byte
Dim RedDst, GreenDst, BlueDst As Byte
Dim ColDiff As Long

RedSrc = pColor1 And &HFF
GreenSrc = (pColor1 And &HFF00&) \ &H100
BlueSrc = (pColor1 And &HFF0000) \ &H10000

RedDst = pColor1 And &HFF
GreenDst = (pColor1 And &HFF00&) \ &H100
BlueDst = (pColor1 And &HFF0000) \ &H10000

ColDiff = Abs(CLng(RedDst) - RedSrc) +
Abs(CLng(GreenDst) - GreenSrc) +
Abs(CLng(BlueDst) - BlueSrc)
'***

Or put a little more succinctly:

'***
Private Function ColDiff(ByVal inA As Long, ByVal inB As Long) As Long
ColDiff = Abs((inA And &HFF&) - (inB And &HFF&)) + _
Abs(((inA And &HFF00&) \ &H100&) - ((inB And &HFF00&) \ &H100&)) + _
Abs(((inA And &HFF0000) \ &H10000) - ((inB And &HFF0000) \ &H10000))
End Function
'***

This will return a value between 0 and 0x2FD (0xFF * 3) for how similar the
two colours are.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@xxxxxxxx
WWW: Http://EDais.mvps.org/


.



Relevant Pages

  • Re: Populating picture box via byte array.
    ... > achieves such good compression on photographic images but if you've ever ... > ByVal nStartScan As Long, ByVal nNumScans As Long, ByRef lpBits As ... > ByRef lpBI As BitmapInfo8, ByVal wUsage As Long) As Long ... > Dim BMHead As BitmapInfo8 ...
    (microsoft.public.vb.general.discussion)
  • Re: Tiff or Tif with Jpeg Compression problem
    ... compression appears, System.Drawing.Image raises an exception. ... Dim objImage As System.Drawing.Image ... 'Exception -> misses ojpeg tags compression ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Tiff or Tif with Jpeg Compression problem
    ... An unhandled exception of type ... Maybe Tiff's TrueColor only supports jpeg compression or none i don't know. ... Dim bm As New Bitmap ... resolve that huge:) file space problem, every option you related of encoder ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Tiff or Tif with Jpeg Compression problem
    ... There are some other encoder parameters that might ... Maybe Tiff's TrueColor only supports jpeg compression or none i don't know. ... Dim bm As New Bitmap ... imgOutput.Save(outputFileName, icinfo, encParams) ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Tiff or Tif with Jpeg Compression problem
    ... Saving tiff with compression works fine for me: ... Dim bm As New Bitmap ... imgOutput.Save(outputFileName, icinfo, encParams) ... resolve that huge:) file space problem, every option you related of encoder ...
    (microsoft.public.dotnet.framework.drawing)