Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 Homework NoEffort
I am a new user (also new in programin), I am creating a program to compare two images pixel by pixel in parallel form, and I need help from anyone to transform this part of C# code from serial form in parallel. This program is for my subjet in school. The code is:
for (int pikselat_ne_rresht = 0; pikselat_ne_rresht < a.Width; pikselat_ne_rresht++)
{
    for (int pikselat_ne_kolon = 0; pikselat_ne_kolon < a.Height; pikselat_ne_kolon++)
    {
        if (a.GetPixel(pikselat_ne_rresht, pikselat_ne_kolon) == b.GetPixel(pikselat_ne_rresht, pikselat_ne_kolon))
        {
            //Rezultati.SetPixel(pikselat_ne_rresht, pikselat_ne_kolon, mire);
            pikselat_e_njejet++;
        }
        else
        {
            b.SetPixel(pikselat_ne_rresht, pikselat_ne_kolon, gabim);
            //Rezultati.SetPixel(pikselat_ne_rresht, pikselat_ne_kolon, gabim);
            pikselat_e_ndryshem++;
            te_njejta = false;
        }
    }
}
Or if someone have such a program and would share with me (and others)i will be gearful. I will wait for some reply fro amyone as soon as posible. All the best from me.
Posted 9-Nov-12 6:05am
Edited 9-Nov-12 14:40pm
v4
Comments
Sergey Alexandrovich Kryukov at 9-Nov-12 12:13pm
   
You need to explain what library you are using. Is it System.Drawing? Or, what is the type of a (never give such short names to variable/members). In all cases, GetPixel/SetPixel is prohibitively slow.
--SA
Andreas Gieriet at 9-Nov-12 12:14pm
   
What exactly is the problem?
What did you try out? If you are too "new in programming", then try simpler problems solving first.
Or you might try out parallel programming in .Net 4 - getting started.
Cheers
Andi
Marcus Kramer at 9-Nov-12 12:17pm
   
Looks to me like this code was given to you by your instructor as the starting point for your assignment. We are not going to do your homework for you no matter how you try to word your question. We've been around a while and can smell a GimmeCode question from a mile away.
Member 8500756 at 9-Nov-12 13:13pm
   
Thanks anyway but it's not my instructors code but my code in serial, and I just ask to transffor this part of code and not to do my homework. Thanks for your sugestions.
BillWoodruff at 9-Nov-12 15:52pm
   
fyi: use of the word "urgent" will usually not get you answers here. No one, I believe, is going to actually re-write your code for you. We're here to learn from, and teach each other.
 
You need to clearly define "parallel" here: do you mean using two threads on one core, or using two cores ? If you intend either of those two choices for your implementation, then, I assume that you know each thread/core process should be operating independently on different "areas" of both images, since any thread/process inter-operation to compare each pair of bytes is going to "cost" you more than if you were using a byte-by-byte comparison in one thread with nested for-loops, as you are doing now.
Member 8500756 at 9-Nov-12 17:24pm
   
I mean to use all cores of computer so to make the multicore application. The application compares two images of the same size. If one of images are rotated it would tell as that images are different. (because i assume that image is changed from someone else and isn't the original image.)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Sergey Alexandrovich Kryukov at 9-Nov-12 13:28pm
   
I don't think this is related to the problem. If one uses GetPixel/SetPixel, it would be a bottleneck, and parallel processing won't help. Besides, it can help if you have considerably under-used CPU cores, otherwise the overhead of parallel processing with only make performance worse. Please see my answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

For a detailed discussion, and timed comparisons, of ways of comparing two identically sized images ... via byte-by-byte (ints), via md5 (hash), via mask, via calling memcmp function in msvcrt.dll, using lockbits ...
 
... where you assume no rotation, no flipping, and that both images are in an identical format, for example: Format32bppRgb ... ... complete with C# code ... see: [^]
 
Speculation on other issues that may not be relevant to you:
 
When trying to solve a problem like this, it is helpful to know as much as possible about the possible variance in "content" of whatever it is you are comparing. If the variance can't be predicted at all: your solution must be, of course, more general.
 
An important aspect in this problem is: whether you are going to have to deal with two pictures of different sizes that contain the same number of pixels, which may be, possibly, identical, but one picture has been rotated and/or flipped.
 
Can you expect, now, that by simply looking for differences in image dimensions, you can automatically reject comparing, for example, a 400x300 image with a 300x400 image ? Or do you have to handle those cases ?
 
The case of comparing two pictures where the total number of pixels match but their relative sizes preclude one being a transformation (rotation, flipping) of the other: do you need to handle such cases ?
 
So: consider two pictures whose sizes, as in the example above, are not identical but match if one picture is rotated either 90 or -90 degrees. Do you need to handle such cases ?
 
And what, if they are the same size, but one image has been flipped 180 degrees horizontally or vertically. What if one has been both rotated and "flipped" ? Do you need to handle such cases ?
  Permalink  
v2
Comments
Nelek at 10-Nov-12 6:22am
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question; don't use GetPixel/SetPixel. If this is System.Drawing, you need to use System.Drawing.Bitmap.LockBits:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^].
 
The MSDN help page of first of these two functions contains a short code sample.
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100