Nested for conversion to threads

Asked

Viewed 100 times

1

I got the following for nested throughout the length of an image bitmap in C#. I would like to rotate the second one with threads to be executed in parallel.

Bitmap alterado = new Bitmap(original.Width, original.Height);

//Convertendo para tons de Cinza
for (int i = 0; i < original.Width; i++)
{
    for (int j = 0; j < original.Height; j++)
    {
        Color corOriginal = original.GetPixel(i, j);
        int escalaCinza = (int)((corOriginal.R * 0.3) + (corOriginal.G * 0.49) + (corOriginal.B * 0.11));
        Color CorEmEscalaDeCinza = Color.FromArgb(escalaCinza, escalaCinza, escalaCinza);
        alterado.SetPixel(i, j, CorEmEscalaDeCinza);
    }
}

return alterado;

How can I do ?

  • But the second for does not depend on the first?

  • I wish that when the second is over, that he would increase the first.

  • And what is the meaning of the parallelism there?

  • It would rotate only the j’s output in parallel.

  • 1

    But why? If the first for you’ll have to wait for the second one to end?

  • And what do you want to do? That’s all that matters. In fact, you have a great chance of serving nothing, or even harming: http://answall.com/q/1946/101

  • If you have a way to rotate the i’s parallel without waiting for the j’s to finish, it’s better

  • The goal is to transform the color image into black and white. I already have the algorithm that works sequentially. would like to convert it to parallel

  • If it’s a small image, it doesn’t pay, if it’s multiple images, it’s better to parallelize the images and not the individual algorithm, if it’s a big one, but really big one, the gain will be small, if you have it. And if possible, not everything can be parallelized. That’s why you need to see what you want to do.

  • It’s a college exercise. I need to do it in a serial and parallel way and compare the two, to see if it pays or not to do it in parallel. I realized that for small images, the sequential algorithm was fast, for larger images (4K resolution) took 23 seconds. I’d like the parallel algorithm to compare.

  • @Raphaelpradodeoliveira but then the for are independent of each other?

  • They are. Each pixel of the image I walk through, is independent of each other.

  • Dude, how is it independent if you use the i within the second is?

  • Did the answer help to reach the solution? Do you think you can accept it?

Show 9 more comments

1 answer

1

Try this:

Parallel.For(0, original.Height, j => {
    for (int i = 0; i < original.Width; i++) {
        Color corOriginal = original.GetPixel(i, j);
        int escalaCinza = (int)((corOriginal.R * 0.3) + (corOriginal.G * 0.49) + (corOriginal.B * 0.11));
        Color CorEmEscalaDeCinza = Color.FromArgb(escalaCinza, escalaCinza, escalaCinza);
        alterado.SetPixel(i, j, CorEmEscalaDeCinza);
    }
});

I put in the Github for future reference.

Keep in mind that using the same algorithm in parallel that is used for sequential is not always the best solution. You are comparing wrong things. In parallel I should do it another way, then maybe I won. Other than this, read what I commented above and the link that I provided there.

Read about the LockBits. It would also work with lock, but is less efficient.

  • The following Exception is returned in this method : Additional information: The object is being used elsewhere.

  • I thought it would be a help. The use of LockBits probably would be more appropriate, I think this is another problem. If you consider a wrong answer, I can delete.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.