Question

S'il vous plaît aidez-moi à faire ce parallèle en utilisant le code OpenMP ce code est exécuté sur le bouton clic et la zone de texte est 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();
    }
 }
}

S'il vous plaît aidez-moi dès que u puis-je croire ce site et tous les programmeurs ici ...

Était-ce utile?

La solution

Vous frappez quelques ralentisseurs, si OpenMP est sur votre radar. OpenMP est une bibliothèque multi-threading pour non géré C / C ++, il faut le soutien du compilateur pour être efficace. Pas une option en C #.

Prenons un pas en arrière. Qu'est-ce que vous avez est maintenant aussi mauvais que vous pouvez éventuellement obtenir. Obtenez / SetPixel () est terriblement lent. Une étape importante serait d'utiliser Bitmap.LockBits (), il vous donne un IntPtr aux bits bitmap. Vous pouvez faire la fête sur ces bits avec un pointeur d'octet dangereux. Ce sera au moins un ordre de grandeur plus rapide.

Prenons un autre pas en arrière, vous écrivez du code clairement pour convertir une image en couleur à une image échelle de gris. couleur sont transformées nativement pris en charge par GDI + dans la classe ColorMatrix. Ce code pourrait ressembler à ceci:

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;
  }
}

Bob Powell le code ci-dessus.

Autres conseils

Pourquoi voulez-vous faire ce parallèle de code? Pour gagner en efficacité de traitement (temps)? Si oui, je pense avant de ce parallèle de code, vous pouvez essayer une approche différente de la façon dont vous accédez à l'information de pixel dans l'image.

La façon dont vous faites dans ce code est très lent. Vous pouvez essayer de traiter votre image à l'aide du code non sécurisé pour accéder aux pixels de la classe Bitmap.

Jetez un oeil à ce fil pour voir ce dont je parle

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top