Click here to Skip to main content
13,141,747 members (58,031 online)
Rate this:
Please Sign up or sign in to vote.
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:
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);
            b.SetPixel(pikselat_ne_rresht, pikselat_ne_kolon, gabim);
            //Rezultati.SetPixel(pikselat_ne_rresht, pikselat_ne_kolon, gabim);
            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
Updated 9-Nov-12 14:40pm
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.
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.
Marcus Kramer 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 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.
Member 8500756 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
Please Sign up or sign in to vote.

Solution 2

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.
Rate this: bad
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 ?
Nelek 10-Nov-12 6:22am
Rate this: bad
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:[^].

The MSDN help page of first of these two functions contains a short code sample.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2017
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