加速位图灰度转换,OpenMP是C#中的一个选项吗?
发布时间:2020-12-16 01:20:52 所属栏目:百科 来源:网络整理
导读:请帮我用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
|
请帮我用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);
}
}
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);
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,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,Color.FromArgb(y1,y1,y1));
}
}
g.DrawImage(bm,new Rectangle(610,200),bm.Width,bm.Height,GraphicsUnit.Pixel);
// Dispose of objects
gs.Dispose();
g.Dispose();
}
}
}
一旦你能相信这个网站和所有程序员,请尽快帮助我…… 解决方法
如果OpenMP在您的雷达上,您将遇到几个速度障碍. OpenMP是一个用于非托管C/C++的多线程库,它需要编译器支持才能生效.不是C#的选项.
我们退一步吧.你现在得到的就像你可能得到的一样糟糕. Get / 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 },new float[] { 0.587f,0.587f,new float[] { 0.114f,0.114f,new float[] { 0,1,1 }
};
var ia = new System.Drawing.Imaging.ImageAttributes();
ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(matrix));
var rc = new Rectangle(0,srce.Width,srce.Height);
gr.DrawImage(srce,rc,srce.Height,GraphicsUnit.Pixel,ia);
return bmp;
}
}
上述代码归功于Bob Powell. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
