이미지를 회색조로 변환
r, g 및 b 구성 요소를 각각 휘도로 설정하는 대신 이미지를 픽셀 당 16 비트 회색조로 변환하는 방법이 있습니까? 현재 파일에서 bmp가 있습니다.
Bitmap c = new Bitmap("filename");
나는 c의 회색조 버전 인 비트 맵 d를 원합니다. System.Drawing.Imaging.PixelFormat을 포함하는 생성자가 표시되지만 사용 방법을 이해하지 못합니다. 저는 이미지 처리 및 관련 C # 라이브러리를 처음 사용하지만 C # 자체에 대한 적절한 경험이 있습니다.
모든 도움, 온라인 소스에 대한 참조, 힌트 또는 제안을 주시면 감사하겠습니다.
편집 : d는 c의 회색조 버전입니다.
"저는 회색조 인 비트 맵 d를 원합니다. System.Drawing.Imaging.PixelFormat을 포함하는 consructor가 있지만 사용 방법을 이해하지 못합니다."
방법은 다음과 같습니다.
Bitmap grayScaleBP = new
System.Drawing.Bitmap(2, 2, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
편집 : 회색조 로 변환하려면
Bitmap c = new Bitmap("fromFile");
Bitmap d;
int x, y;
// Loop through the images pixels to reset color.
for (x = 0; x < c.Width; x++)
{
for (y = 0; y < c.Height; y++)
{
Color pixelColor = c.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
c.SetPixel(x, y, newColor); // Now greyscale
}
}
d = c; // d is grayscale version of c
switchonthecode의 더 빠른 버전은 전체 분석을 위해 링크를 따릅니다.
public static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
Bitmap d = new Bitmap(c.Width, c.Height);
for (int i = 0; i < c.Width; i++)
{
for (int x = 0; x < c.Height; x++)
{
Color oc = c.GetPixel(i, x);
int grayScale = (int)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
Color nc = Color.FromArgb(oc.A, grayScale, grayScale, grayScale);
d.SetPixel(i, x, nc);
}
}
이렇게하면 알파 채널도 유지됩니다.
즐겨.
ToolStripRenderer
클래스에라는 정적 메서드가 CreateDisabledImage
있습니다. 사용법은 다음과 같이 간단합니다.
Bitmap c = new Bitmap("filename");
Image d = ToolStripRenderer.CreateDisabledImage(c);
수락 된 답변의 매트릭스와 약간 다른 매트릭스를 사용하고 여기에 투명도 0.7의 값을 추가로 곱하므로 효과는 회색조와 약간 다르지만 이미지를 회색으로 표시하려면 가장 간단하고 최고의 솔루션.
여기에 몇 가지 항목을 요약하면 다음과 같습니다. 단순하지만 빠르지 않은 픽셀 단위 옵션이 있습니다.
@Luis' comment linking to: (archived) https://web.archive.org/web/20110827032809/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale is superb.
He runs through three different options and includes timings for each.
None of the examples above create 8-bit (8bpp) bitmap images. Some software, such as image processing, only supports 8bpp. Unfortunately the MS .NET libraries do not have a solution. The PixelFormat.Format8bppIndexed format looks promising but after a lot of attempts I couldn't get it working.
To create a true 8-bit bitmap file you need to create the proper headers. Ultimately I found the Grayscale library solution for creating 8-bit bitmap (BMP) files. The code is very simple:
Image image = Image.FromFile("c:/path/to/image.jpg");
GrayBMP_File.CreateGrayBitmapFile(image, "c:/path/to/8bpp/image.bmp");
The code for this project is far from pretty but it works, with one little simple-to-fix problem. The author hard-coded the image resolution to 10x10. Image processing programs do not like this. The fix is open GrayBMP_File.cs (yeah, funky file naming, I know) and replace lines 50 and 51 with the code below. The example sets the resolution to 200x200 but you should change it to the proper number.
int resX = 200;
int resY = 200;
// horizontal resolution
Copy_to_Index(DIB_header, BitConverter.GetBytes(resX * 100), 24);
// vertical resolution
Copy_to_Index(DIB_header, BitConverter.GetBytes(resY * 100), 28);
The code below is the simplest solution:
Bitmap bt = new Bitmap("imageFilePath");
for (int y = 0; y < bt.Height; y++)
{
for (int x = 0; x < bt.Width; x++)
{
Color c = bt.GetPixel(x, y);
int r = c.R;
int g = c.G;
int b = c.B;
int avg = (r + g + b) / 3;
bt.SetPixel(x, y, Color.FromArgb(avg,avg,avg));
}
}
bt.Save("d:\\out.bmp");
ReferenceURL : https://stackoverflow.com/questions/2265910/convert-an-image-to-grayscale
'developer tip' 카테고리의 다른 글
Rails 개발 로그를 어떻게 볼 수 있습니까? (0) | 2021.01.10 |
---|---|
뒤에 aspx 코드를 통해 CSS 클래스 추가 (0) | 2021.01.10 |
UITableView에서 첫 번째 행을 기본값으로 선택하십시오. (0) | 2021.01.10 |
NSMutableArray에서 요소 교체 (0) | 2021.01.10 |
분수를 이진수로 어떻게 변환합니까? (0) | 2021.01.10 |