images and equality

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Eggermann

    #1

    images and equality

    Hello all,
    I'm looking for a way to tell whether two image objects which do not
    have object equality are the same image or not, and I mean identical, say
    originally loaded from the same file. Any ideas on how to approach this?

    TIA,
    Eric


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: images and equality

    Eric,

    I don't think that there is an easy way to do this. If you can get a
    byte array with the contents, then you can cycle through the bytes and then
    do a comparison.

    Or, you could even attach the byte arrays to a MemoryStream, then feed
    that stream through a hash algorithm in the System.Security .Cryptography
    namespace and then compare the results.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Eric Eggermann >" <<none> wrote in message
    news:uQrsUeAuDH A.1788@tk2msftn gp13.phx.gbl...[color=blue]
    > Hello all,
    > I'm looking for a way to tell whether two image objects which do not
    > have object equality are the same image or not, and I mean identical, say
    > originally loaded from the same file. Any ideas on how to approach this?
    >
    > TIA,
    > Eric
    >
    >[/color]


    Comment

    • Eric Eggermann

      #3
      Re: images and equality


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:emBPs7BuDH A.2060@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Eric,
      >
      > I don't think that there is an easy way to do this. If you can get a
      > byte array with the contents, then you can cycle through the bytes and[/color]
      then[color=blue]
      > do a comparison.
      >
      > Or, you could even attach the byte arrays to a MemoryStream, then feed
      > that stream through a hash algorithm in the System.Security .Cryptography
      > namespace and then compare the results.
      >
      > Hope this helps.[/color]

      Thanks, I'll have a whack at those.

      Eric


      Comment

      • JDeats

        #4
        Re: images and equality

        This is a great spot to showcase the power of the .NET Framework, this
        fairly complex task can be done in less than ten lines of code:

        public bool CompareImages(I mage first, Image second)
        {
        System.IO.Memor yStream ms1 = new System.IO.Memor yStream();
        System.IO.Memor yStream ms2 = new System.IO.Memor yStream();

        first.Save( ms1, System.Drawing. Imaging.ImageFo rmat.Bmp);
        byte[] First = ms1.ToArray();

        second.Save( ms2, System.Drawing. Imaging.ImageFo rmat.Bmp );
        byte[] Second = ms2.ToArray();

        return System.Text.Enc oding.ASCII.Get String(First) ==
        System.Text.Enc oding.ASCII.Get String(Second);

        }


        "Eric Eggermann" <<none>> wrote in message news:<uQrsUeAuD HA.1788@tk2msft ngp13.phx.gbl>. ..[color=blue]
        > Hello all,
        > I'm looking for a way to tell whether two image objects which do not
        > have object equality are the same image or not, and I mean identical, say
        > originally loaded from the same file. Any ideas on how to approach this?
        >
        > TIA,
        > Eric[/color]

        Comment

        • Eric Eggermann

          #5
          Re: images and equality


          "JDeats" <jeremy@pdq.net > wrote in message
          news:b0738dc6.0 312011433.20508 32d@posting.goo gle.com...[color=blue]
          > This is a great spot to showcase the power of the .NET Framework, this
          > fairly complex task can be done in less than ten lines of code:
          >
          >snip[/color]

          Thanks a million Jeremy. I hadn't gotten around to trying the suggestions in
          the first reply I got, but anyway, I'd have never thought to compare the
          bytes as strings. Is this to avoid a reference equality result?

          Eric


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: images and equality

            JDeats <jeremy@pdq.net > wrote:[color=blue]
            > This is a great spot to showcase the power of the .NET Framework, this
            > fairly complex task can be done in less than ten lines of code:
            >
            > public bool CompareImages(I mage first, Image second)
            > {
            > System.IO.Memor yStream ms1 = new System.IO.Memor yStream();
            > System.IO.Memor yStream ms2 = new System.IO.Memor yStream();
            >
            > first.Save( ms1, System.Drawing. Imaging.ImageFo rmat.Bmp);
            > byte[] First = ms1.ToArray();
            >
            > second.Save( ms2, System.Drawing. Imaging.ImageFo rmat.Bmp );
            > byte[] Second = ms2.ToArray();
            >
            > return System.Text.Enc oding.ASCII.Get String(First) ==
            > System.Text.Enc oding.ASCII.Get String(Second);
            >
            > }[/color]

            However, that isn't accurate, because for some reason you're converting
            the two byte arrays into text using ASCII, which only has 128 values.
            Forget Encoding.ASCII - just use:

            if (First.Length != Second.Length)
            return false;

            for (int i=0; i < First.Length; i++)
            {
            if (First[i] != Second[i])
            return false;
            }

            return true;

            Slightly longer, but more reliable and also more efficient (it doesn't
            leave two potentially long strings on the heap for no reason).

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: images and equality

              <"Eric Eggermann" <<none>>> wrote:[color=blue]
              > Thanks a million Jeremy. I hadn't gotten around to trying the suggestions in
              > the first reply I got, but anyway, I'd have never thought to compare the
              > bytes as strings. Is this to avoid a reference equality result?[/color]

              Yes, but it's flawed - see my response to Jeremy's post.

              If you're commonly comparing byte arrays for equality, you might want
              to put the code for it in a utility method somewhere.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Eric Eggermann

                #8
                Re: images and equality


                "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                news:MPG.1a367c b9caca45d6989b8 6@msnews.micros oft.com...[color=blue]
                >
                > However, that isn't accurate, because for some reason you're converting
                > the two byte arrays into text using ASCII, which only has 128 values.
                > Forget Encoding.ASCII - just use:
                >
                > if (First.Length != Second.Length)
                > return false;
                >
                > for (int i=0; i < First.Length; i++)
                > {
                > if (First[i] != Second[i])
                > return false;
                > }
                >
                > return true;[/color]

                I thought about this when I saw the reply, and I'm planning on doing tests
                for efficiency. I'm not sure how the string compare is implemented, but
                because of the way this section of the code will run, I'm more interested in
                speed than memory use. It will run in bursts, so to speak, called many times
                consecutively, but then not again for awhile.

                Thanks for advice Jon, and I'll try things out both ways, and post back to
                the group what I find.

                Eric


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: images and equality

                  <"Eric Eggermann" <<none>>> wrote:

                  <snip>
                  [color=blue]
                  > I thought about this when I saw the reply, and I'm planning on doing tests
                  > for efficiency. I'm not sure how the string compare is implemented, but
                  > because of the way this section of the code will run, I'm more interested in
                  > speed than memory use. It will run in bursts, so to speak, called many times
                  > consecutively, but then not again for awhile.
                  >
                  > Thanks for advice Jon, and I'll try things out both ways, and post back to
                  > the group what I find.[/color]

                  The main concern should be *accuracy* though, and using a string
                  conversion is just not a good idea when it comes to comparing *binary*
                  data. I'd actually expect the "natural" version to be just as fast
                  anyway - you only need to go through the data once, as opposed to once
                  to convert and then once to compare. It also means that if the first
                  pixel is different, you don't need to go through the rest of the arrays
                  at all.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • JDeats

                    #10
                    Re: images and equality

                    Really, well it may not be the most optimal approach, but I tested it
                    multiple times before posting and it seemed to do the trick, I
                    wouldn't have posted something that didn't work.

                    The two long strings will be picked up by the garbage collector, so it
                    should be do problem, but avoiding their creation would be more
                    efficient. I was just trying to demonstrate the simplest approach
                    possible, but it's worth the few extra lines of code to do it right.

                    Thanks,
                    Jeremy


                    [color=blue]
                    > However, that isn't accurate, because for some reason you're converting
                    > the two byte arrays into text using ASCII, which only has 128 values.
                    > Forget Encoding.ASCII - just use:
                    >
                    > if (First.Length != Second.Length)
                    > return false;
                    >
                    > for (int i=0; i < First.Length; i++)
                    > {
                    > if (First[i] != Second[i])
                    > return false;
                    > }
                    >
                    > return true;
                    >
                    > Slightly longer, but more reliable and also more efficient (it doesn't
                    > leave two potentially long strings on the heap for no reason).[/color]

                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: images and equality

                      JDeats <jeremy@pdq.net > wrote:[color=blue]
                      > Really, well it may not be the most optimal approach, but I tested it
                      > multiple times before posting and it seemed to do the trick, I
                      > wouldn't have posted something that didn't work.[/color]

                      I'm afraid you did though. Try this:

                      using System;
                      using System.Text;

                      class Test
                      {
                      static void Main()
                      {
                      byte[] b1 = {0x20};
                      byte[] b2 = {0xa0};

                      String s1 = Encoding.ASCII. GetString (b1);
                      String s2 = Encoding.ASCII. GetString (b2);

                      Console.WriteLi ne (s1==s2);
                      }
                      }

                      It prints "True" because the ASCII encoding truncates the top bit of
                      each byte. I'm sure your version would always correctly pick up byte
                      arrays which are equal, but it wouldn't always correctly pick up byte
                      arrays which *aren't* equal.
                      [color=blue]
                      > The two long strings will be picked up by the garbage collector, so it
                      > should be do problem, but avoiding their creation would be more
                      > efficient.[/color]

                      They would be picked up by the garbage collector, yes, but if the
                      images are of significant size the strings will be quite large (double
                      the size of whatever size the byte array is)
                      [color=blue]
                      > I was just trying to demonstrate the simplest approach
                      > possible, but it's worth the few extra lines of code to do it right.[/color]

                      Avoiding the extra memory isn't the main problem here - it's
                      correctness. The extra speed and memory efficiency of the "brute
                      force" solution I suggested (which I think is actually simpler too,
                      fundamentally - it doesn't involve the concepts of text conversion etc)
                      are just bonuses.

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      Working...