Pergunta

Por favor me ajude a tornar este código paralelo usando o OpenMP, este código é executado no botão Clique e a caixa de texto é 128

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IMG
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    string path = "";
    public void openimage()
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            path = openFileDialog1.FileName;
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);
            Bitmap curBitmap = new Bitmap(path);
            g.DrawImage(curBitmap, 200, 220, 200, 200);
        }
    }
    Bitmap bm;
    Bitmap gs;
    private void button1_Click(object sender, EventArgs e)
    {
        if (path == "")
        {
            openimage();
        }

        //mack image gray scale
        Graphics g = this.CreateGraphics();
        g.Clear(this.BackColor);
        // Create a Bitmap object
        bm = new Bitmap(path);
        // Draw image with no effects
        g.DrawImage(bm, 200, 220, 200, 200);


        gs = new Bitmap(bm.Width, bm.Height);
        for (int i = 0; i < bm.Width; i++)
        {
            for (int j = 0; j < bm.Height; j++)
            {
                Color c = bm.GetPixel(i, j);
                int y = (int)(0.3 * c.R + 0.59 * c.G + 0.11 * c.B);
                gs.SetPixel(i, j, Color.FromArgb(y, y, y));
            }
        }

        // Draw image with no effects
        g.DrawImage(gs, 405, 220, 200, 200);


        for (int i = 0; i < gs.Width; i++)
        {
            for (int j = 0; j < gs.Height; j++)
            {
                Color c = gs.GetPixel(i, j);
                int y1 = 0;

                if (c.R >= Convert.ToInt16(textBox19.Text))
                    y1 = 255;
                bm.SetPixel(i, j, Color.FromArgb(y1, y1, y1));

            }
        }
        g.DrawImage(bm, new Rectangle(610, 220, 200, 200), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel);
        // Dispose of objects
        gs.Dispose();

        g.Dispose();
    }
 }
}

Por favor me ajude assim que você posso acreditar neste site e em todos os programadores aqui ...

Foi útil?

Solução

Você acertará algumas canhões de velocidade se o OpenMP estiver no seu radar. O OpenMP é uma biblioteca multi-threading para C/C ++ não gerenciado, exige que o suporte do compilador seja eficaz. Não é uma opção em C#.

Vamos dar um passo atrás. O que você tem agora é tão ruim quanto você pode obter. Get/setPixel () é terrivelmente lento. Uma etapa importante seria usar o bitmap.lockbits (), fornece um INTPTR para os bits de bitmap. Você pode festejar nesses bits com um ponteiro de byte inseguro. Isso será pelo menos uma ordem de magnitude mais rápida.

Vamos dar mais um passo para trás, você está claramente escrevendo código para converter uma imagem colorida em uma imagem em escala de cinza. As transformações de cores são suportadas nativamente pelo GDI+ através da classe Colormatrix. Esse código pode ficar assim:

public static Image ConvertToGrayScale(Image srce) {
  Bitmap bmp = new Bitmap(srce.Width, srce.Height);
  using (Graphics gr = Graphics.FromImage(bmp)) {
    var matrix = new float[][] {
        new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
        new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
        new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
        new float[] { 0,      0,      0,      1, 0 },
        new float[] { 0,      0,      0,      0, 1 }
    };
    var ia = new System.Drawing.Imaging.ImageAttributes();
    ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(matrix));
    var rc = new Rectangle(0, 0, srce.Width, srce.Height);
    gr.DrawImage(srce, rc, 0, 0, srce.Width, srce.Height, GraphicsUnit.Pixel, ia);
    return bmp;
  }
}

Crédito para Bob Powell Para o código acima.

Outras dicas

Por que você deseja tornar esse código paralelo? Para obter eficiência de processamento (tempo)? Nesse caso, penso antes de transformar esse código paralelo, você pode tentar uma abordagem diferente da maneira como está acessando as informações do pixel em sua imagem.

A maneira como você está fazendo neste código é muito lenta. Você pode tentar processar sua imagem usando código inseguro para acessar os pixels da classe Bitmap.

Dê uma olhada neste tópico Para ver do que estou falando

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top