Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.17/5 (5 votes)
See more:
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:
C#
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
Updated 9-Nov-12 14:40pm
v4
Comments
Sergey Alexandrovich Kryukov 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 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
fjdiewornncalwe 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.
Rafeti 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 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.

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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
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 ?
 
Share this answer
 
v2
Comments
Nelek 10-Nov-12 6:22am    
+5
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900