이미지를 그레이 스케일로 변환하십시오
-
20-09-2019 - |
문제
각 R, G 및 B 구성 요소를 휘도로 설정하는 대신 이미지를 픽셀 형식 당 16 비트로 변환하는 방법이 있습니까? 현재 파일에서 BMP가 있습니다.
Bitmap c = new Bitmap("filename");
나는 비트 맵 d를 원한다. 그것은 C의 그레이 스케일 버전이다. System.Drawing.Imaging.pixelforMat을 포함하는 생성자가 표시되지만 사용 방법을 이해하지 못합니다. 이미지 처리 및 관련 C# 라이브러리를 처음 접했지만 C# 자체에 대한 적당한 경험이 있습니다.
모든 도움, 온라인 소스에 대한 참조, 힌트 또는 제안에 감사드립니다.
편집 : D는 C의 그레이 스케일 버전입니다.
해결책
"나는 비트 맵 d, 그것은 그레이 스케일이다. 나는 System.Drawing.Imaging.pixelforMat을 포함하는 양심자가 있지만, 그것을 사용하는 방법을 이해하지 못한다."
이 작업을 수행하는 방법은 다음과 같습니다
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
더 빠른 버전 스위치 폰테 코드 전체 분석을 위해 링크를 따르십시오.
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의 의견 링크 : (보관) https://web.archive.org/web/20110827032809/http://www.www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-con-mor-mage-trayscale 훌륭합니다.
그는 세 가지 옵션을 실행하며 각각의 타이밍을 포함합니다.
위의 예 중 어느 것도 8 비트 (8bpp) 비트 맵 이미지를 만들지 않습니다. 이미지 처리와 같은 일부 소프트웨어는 8bpp 만 지원합니다. 불행히도 MS .NET 라이브러에는 솔루션이 없습니다. 그만큼 pixelformat.format8bppindexed 형식은 유망 해 보이지만 많은 시도 후에는 작동하지 못했습니다.
진정한 8 비트 비트 맵 파일을 만들려면 적절한 헤더. 궁극적으로 나는 그것을 발견했다 그레이 스케일 도서관 8 비트 비트 맵 (BMP) 파일을 만드는 솔루션. 코드는 매우 간단합니다.
Image image = Image.FromFile("c:/path/to/image.jpg");
GrayBMP_File.CreateGrayBitmapFile(image, "c:/path/to/8bpp/image.bmp");
이 프로젝트의 코드는 예쁘지 않지만 약간의 간단한 문제와 함께 작동합니다. 저자는 이미지 해상도를 10x10으로 하드 코딩했습니다. 이미지 처리 프로그램은 이것을 좋아하지 않습니다. 수정은 열린 Graybmp_file.cs (예, Funky Files Naming, I Know)이며 50과 51 행을 아래 코드로 바꿉니다. 이 예제는 해상도를 200x200으로 설정하지만 적절한 숫자로 변경해야합니다.
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);
아래 코드는 가장 간단한 솔루션입니다.
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");