开发者

Accelerating bitmap grayscale conversion, is OpenMP an option in C#?

开发者 https://www.devze.com 2022-12-16 12:16 出处:网络
please help me to make this code parallelusing openmp this code is run on button click and the text box is 128

please help me to make this code parallel using openmp this code is run on button click and the text box is 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 (开发者_如何学GoopenFileDialog1.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();
    }
 }
}

please help me as soon as u can i believe in this site and all programmers here...


You'll hit a couple of speed-bumps if OpenMP is on your radar. OpenMP is a multi-threading library for unmanaged C/C++, it requires compiler support to be effective. Not an option in C#.

Let's take a step back. What you've got now is as bad as you can possibly get. Get/SetPixel() is dreadfully slow. A major step would be to use Bitmap.LockBits(), it gives you an IntPtr to the bitmap bits. You can party on those bits with an unsafe byte pointer. That will be at least an order of magnitude faster.

Let's take another step back, you are clearly writing code to convert a color image to a gray-scale image. Color transforms are natively supported by GDI+ through the ColorMatrix class. That code could look like this:

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

Credit to Bob Powell: How to convert a colour image to grayscale for the above code.


Why do you want to make this code parallel? To gain processing efficiency (time) ? If so, I think before turning this code parallel you can try a different approach in the way you are accessing the pixel information in your image.

The way you are doing in this code is very slow. You may try to process your image using unsafe code to access the pixels of the Bitmap class.

Take a look at this thread to see what I'm talking about

0

精彩评论

暂无评论...
验证码 换一张
取 消