加速位图灰度转换,OpenMP 是 C# 中的一个选项吗? [英] Accelerating bitmap grayscale conversion, is OpenMP an option in C#?

查看:27
本文介绍了加速位图灰度转换,OpenMP 是 C# 中的一个选项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我使用 openmp 使此代码并行此代码在按钮单击时运行,文本框为 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 (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();
    }
 }
}

请尽快帮助我,我相信这个站点和这里的所有程序员......

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

推荐答案

如果 OpenMP 在您的视线范围内,您将遇到一些速度障碍.OpenMP 是一个用于非托管 C/C++ 的多线程库,它需要编译器支持才能生效.不是 C# 中的选项.

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#.

让我们退后一步.你现在所拥有的已经是你所能得到的最糟糕的了.Get/SetPixel() 非常慢.一个主要步骤是使用 Bitmap.LockBits(),它为您提供位图位的 IntPtr.您可以使用不安全的字节指针对这些位进行派对.这至少会快一个数量级.

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.

让我们再退一步,您显然是在编写将彩色图像转换为灰度图像的代码.GDI+ 通过 ColorMatrix 类本机支持颜色转换.该代码可能如下所示:

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

归功于 Bob Powell:如何转换上面代码的彩色图像到灰度.

这篇关于加速位图灰度转换,OpenMP 是 C# 中的一个选项吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆