加快位灰度转换,是OpenMP的在C#中的选项?
-
19-09-2019 - |
题
请帮我做使用OpenMP这个代码的并行 这个代码在按钮点击运行和文本框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();
}
}
}
请尽快ü我可以相信,在这个网站,在这里所有的程序员......
帮我解决方案
您会打几个速度凸块,如果OpenMP是在雷达上。 OpenMP是用于非托管C / C ++的多线程库,它需要的编译器支持是有效的。在C#不是一种选择。
让我们退一步。你现在所得到的是一样糟糕,你都不可能得到的。获取/ SetPixel()是可怕缓慢。一个重要的步骤将是使用Bitmap.LockBits(),它给你一个IntPtr位图位。您可以党与不安全字节的指针这些位。这将是至少大小更快的顺序。
让我们再退一步,你都清楚地编写代码来将彩色图像转换为灰度图像。颜色变换的原生GDI +通过ColorMatrix类的支持。该代码可能看起来像这样:
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;
}
}
信用到鲍勃鲍威尔,在上面的代码。
其他提示
为什么要进行并行代码?为了获得处理效率(时间)?如果是这样,我想打开该并联代码之前,你可以尝试在您访问您的图像的像素信息的方式不同的方法。
您在此代码正在做的方式是非常缓慢的。您可以尝试使用不安全的代码来访问Bitmap类的像素处理您的图像。
不隶属于 StackOverflow