如何将彩色图片转换为灰色 [英] How do I convert a color picture to gray

查看:102
本文介绍了如何将彩色图片转换为灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ı有灰度图像。 ı写了代码,现在ı想转换为颜色ımage和ı想确定范围的rgbı写了关于那个但不起作用的代码



  private   void  button13_Click( object  sender,EventArgs e)
{



位图resim = 位图(pictureBox2.Image );
颜色renk,renk2;
int r,g,b,d;
for int i = 0 ; i < resim.Width; i ++)
{

for int j = 0 ; j < ; resim.Height; j ++)
{


renk = resim.GetPixel(i,j);
r = renk.R;
g = renk.G;
b = renk.B;
d =(r + g + b)/ 3 ;





如果(d < 55
{
d = renk.R;
}
如果(d > 55 && d < 155
{
d = renk.G;
}

如果(d > 155 && d < 255
{
d = renk.B;
}

resim.SetPixel(i,j,renk);


renk2 = Color.FromArgb(d,d,d);


}

pictureBox3.Image = resim;
}

解决方案

搜索文章图像处理傻瓜。是的,你读得对。



有一篇文章可以快速将图像转换为灰度。





您无法将灰度图像转换为彩色,因为图像或其元数据中没有信息可以知道哪种颜色的灰色会变成什么颜色。相同的阴影也可能是图像中的几种不同颜色,因此算法不可能知道任何一个像素的原始颜色。


你没有指定是否这样是WinForm或WPF。我在WPF中使用过它:

http ://msdn.microsoft.com/en-us/library/system.windows.media.imaging.formatconvertedbitmap.aspx [ ^ ]



 bmp =  BitmapImage( Uri(dlg.FileName))

Dim GreyBmp As FormatConvertedBitmap
GreyBmp.BeginInit()
GreyBmp.Source = bmp
GreyBmp.DestinationFormat = PixelFormats.Gray8
GreyBmp.EndInit()

Dim im As BitmapSource = GreyBmp

Image1.Source = im





H.如果可以将Bitmap图像转换为BitmapSource:

Bitmap to BitmapSource [ ^ ]


请参阅:

如何转换彩色图片灰色 [ ^ ],

如何将彩色图片转换为灰色 [ ^ ]。



什么还不够?



-SA


ı have a grayscale ımage. ı wrote thıs code and now ı want to convert to color ımage and ı want to determıned range of r g b ı wrote code about that but doesn t work

private void button13_Click(object sender, EventArgs e)
       {



           Bitmap resim = new Bitmap(pictureBox2.Image);
           Color renk, renk2;
           int r, g, b, d;
           for (int i = 0; i < resim.Width; i++)
           {

               for (int j = 0; j < resim.Height; j++)
               {


                   renk = resim.GetPixel(i, j);
                   r = renk.R;
                   g = renk.G;
                   b = renk.B;
                   d = (r + g + b) / 3;





                   if (d < 55)
                   {
                       d = renk.R;
                   }
                   if (d > 55 && d < 155)
                   {
                       d = renk.G;
                   }

                   if (d > 155 && d < 255)
                   {
                       d = renk.B;
                   }

                   resim.SetPixel(i, j, renk);


                   renk2 = Color.FromArgb(d, d, d);


               }

               pictureBox3.Image = resim;
           }

解决方案

Search the articles for "Image processing for dummies". Yes, you read that right.

There''s an article for quickly converting an image to grayscale.


You cannot convert a grayscale image to color as there is no information in the image or its metadata to know which shade of gray goes to what color. The same shade may also be several different colors in the image, so it''s impossible for an algorithm to know what the original color was for any one pixel.


You didnt specify if this was WinForm or WPF. I have used this in WPF:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.formatconvertedbitmap.aspx[^]

bmp = New BitmapImage(New Uri(dlg.FileName))

Dim GreyBmp As New FormatConvertedBitmap
GreyBmp.BeginInit()
GreyBmp.Source = bmp
GreyBmp.DestinationFormat = PixelFormats.Gray8
GreyBmp.EndInit()

Dim im As BitmapSource = GreyBmp

Image1.Source = im



However it is possible to trasform a Bitmap image to BitmapSource:
Bitmap to BitmapSource[^]


Please see:
How do I convert a color picture to gray[^],
How do I convert a color picture to gray[^].

What wasn''t enough?

—SA


这篇关于如何将彩色图片转换为灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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