PDA

View Full Version : سوال: تبديل تصوير رنگي به خاكستري (GrayScale- Image Processing)



sahar_5800
سه شنبه 15 اردیبهشت 1388, 15:05 عصر
باسلام

من ميخوام تو محيط ‍#C يه عكس رنگي رو GrayScale كنم. ولي نمي دونم با چه توابعي بايد كاركنم.
تا اين حد مي دونم كه بايد ميزان R و G و B هر پيكس رو (دوتا حلقه تودرتو) به دست بيارم . باهم جمع و تقسيم بر 3 كنم.

اينكه ميزان رنگ عكس رو با چه تابعي به دست بيارم رو نميدونم.

(پروژه درس محيط چند رسانه اي)

باتشكر

esmaeily-hosein
سه شنبه 15 اردیبهشت 1388, 17:10 عصر
way 1:
Convert Time :1,135ms

public static Bitmap MakeGrayscale(Bitmap original)
{
//make an empty bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);

for (int i = 0; i < original.Width; i++)
{
for (int j = 0; j < original.Height; j++)
{
//get the pixel from the original image
Color originalColor = original.GetPixel(i, j);

//create the grayscale version of the pixel
int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
+ (originalColor.B * .11));

//create the color object
Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);

//set the new image's pixel to the grayscale version
newBitmap.SetPixel(i, j, newColor);
}
}

return newBitmap;
}


way 2
convert time :188ms

public static Bitmap MakeGrayscale2(Bitmap original)
{
unsafe
{
//create an empty bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);

//lock the original bitmap in memory
BitmapData originalData = original.LockBits(
new Rectangle(0, 0, original.Width, original.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

//lock the new bitmap in memory
BitmapData newData = newBitmap.LockBits(
new Rectangle(0, 0, original.Width, original.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

//set the number of bytes per pixel
int pixelSize = 3;

for (int y = 0; y < original.Height; y++)
{
//get the data from the original image
byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);

//get the data from the new image
byte* nRow = (byte*)newData.Scan0 + (y * newData.Stride);

for (int x = 0; x < original.Width; x++)
{
//create the grayscale version
byte grayScale =
(byte)((oRow[x * pixelSize] * .11) + //B
(oRow[x * pixelSize + 1] * .59) + //G
(oRow[x * pixelSize + 2] * .3)); //R

//set the new image's pixel to the grayscale version
nRow[x * pixelSize] = grayScale; //B
nRow[x * pixelSize + 1] = grayScale; //G
nRow[x * pixelSize + 2] = grayScale; //R
}
}

//unlock the bitmaps
newBitmap.UnlockBits(newData);
original.UnlockBits(originalData);

return newBitmap;
}
}



way 3
convert time : : 62ms

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;
}

sahar_5800
سه شنبه 22 اردیبهشت 1388, 14:08 عصر
ممنون.
ولي مگه تو #C ميشه از كد PHP استفاده كرد؟

esmaeily-hosein
سه شنبه 22 اردیبهشت 1388, 14:13 عصر
به نظر خودت کدش php !
tag php مثلیکه بعضی از جاها از کد رو رنگی میکنه به همین خاطر من ازش استفاده کردم توی سایت یه چیزه معموله .