0% found this document useful (0 votes)
9 views

Lab DIP Week2

Uploaded by

phailinkpc0874
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab DIP Week2

Uploaded by

phailinkpc0874
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ອາທິດທີ 2_3: Gray Scale, Black and White and Invert

ຂຽນໂປຼແກຼມ:
1. ເພື່ອຊອກຄ່າລະດັບເທົາ (Gray scale) ຂອງຮູບພາບທີ່ກຳນົດ, ໂດຍນຳໃຊ້
ສູດທີ1: ຄ່າໃໝ່ = (R+G+B)/3;
ສູດທີ2: ຄ່າໃໝ່ = (0.3*R+0.59*G+0.11*B);
2. ເພື່ອປ່ຽນຄ່າຟິກເຊວ ໃຫ້ເປັນຂາວດຳ ໂດຍນຳໃຊ້ສູດ Thresholding (t ແມ່ນຄ່າ threshold ທີ່ກຳນົດ)
ຖ້າ ຄ່າຟິກເຊວ > t ຄ່າໃໝ່ = 255;
ຖ້າ ຄ່າຟິກເຊວ <= t ຄ່າໃໝ່ = 0;
3. ເພື່ອປ່ຽນຄ່າກົງກັນຂ້າມຂອງຮູບຂາວດຳ ໂດຍນຳໃຊ້ສູດ:
ຖ້າ ຄ່າຟິກເຊວ = 0 ຄ່າໃໝ່ = 255;
ຖ້າ ຄ່າຟິກເຊວ = 255 ຄ່າໃໝ່ = 0;
4. ເພື່ອປ່ຽນຄ່າລົບ (Negative) ຂອງຟິກເຊວ, ໂດຍນຳໃຊ້ສູດ: (s = L-1-r)
ຄ່າໃໝ່ = 255 – ຟິກເຊວປະຈຸບັນ;

ວຽກບ້ານ: ໃຫ້ນັກສຶກສາຂຽນໂປຼແກຼມ ເພື່ອນໍາໃຊ້ສູດ Log Transformations (s = c*log(1+r)) ແລະ


Power-Law Transformations ( s=cr γ )

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

1
namespace Gray_Scale1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

Bitmap pict_C;
Bitmap pict_O = (Bitmap)Image.FromFile("Im1.jpg");

public Bitmap ConvertToGrayScale(Bitmap source) {


Bitmap bmp = new Bitmap(source.Width,source.Height);
for(int i=0;i<source.Width;i++){
for (int j = 0; j<source.Height;j++)
{
Color c = source.GetPixel(i,j);
int avg = (int)((c.R+c.G+c.B)/3);
bmp.SetPixel(i,j,Color.FromArgb(avg,avg,avg));
}
}
return bmp;
}

public Bitmap ConvertToGrayScale2(Bitmap source)


{
Bitmap bmp = new Bitmap(source.Width, source.Height);
for (int i = 0; i < source.Width; i++)
{
for (int j = 0; j < source.Height; j++)
{
Color c = source.GetPixel(i, j);
//Luminance
int nP = (int)(0.3*c.R + 0.59*c.G + 0.11*c.B);
bmp.SetPixel(i, j, Color.FromArgb(nP, nP, nP));
}
}
return bmp;
}

//setting threshold
public Bitmap Thresholding1(Bitmap source)
{
Bitmap bmp = new Bitmap(source.Width, source.Height);
int t = int.Parse(textBox1.Text);

for (int i = 0; i < source.Width; i++)


{
for (int j = 0; j < source.Height; j++)
{
Color c = source.GetPixel(i, j);
int avg = (int)((c.R + c.G + c.B) / 3);
if (avg > t)

2
avg = 255;
else avg = 0;

bmp.SetPixel(i, j, Color.FromArgb(avg, avg, avg));


}
}
return bmp;
}

//setting invert
public Bitmap Invert(Bitmap source)
{
Bitmap bmp = new Bitmap(source.Width, source.Height);
int t = int.Parse(textBox1.Text);

for (int i = 0; i < source.Width; i++)


{
for (int j = 0; j < source.Height; j++)
{
Color c = source.GetPixel(i, j);
int avg = (int)((c.R + c.G + c.B)/3);
if (avg == 255)
avg = 0;
else if (avg == 0)
avg = 255;

bmp.SetPixel(i, j, Color.FromArgb(avg, avg, avg));


}
}
return bmp;
}

//setting Negative
public Bitmap Negative(Bitmap source)
{
Bitmap bmp = new Bitmap(source.Width, source.Height);
int t = int.Parse(textBox1.Text);

for (int i = 0; i < source.Width; i++)


{
for (int j = 0; j < source.Height; j++)
{
Color c = source.GetPixel(i, j);
int r1,g1,b1;
r1 = 255 - (int)(c.R);
g1 = 255 - (int)(c.G);
b1 = 255 - (int)(c.B);
bmp.SetPixel(i, j, Color.FromArgb(r1, g1, b1));
}
}
return bmp;
}

private void Form1_Load(object sender, EventArgs e)


{

3
pictureBox1.Image = pict_O;
pictureBox1.Refresh();
}
//origin
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = pict_O;
}
//grayscale1
private void button2_Click(object sender, EventArgs e)
{
pict_C = new Bitmap(pictureBox1.Image);
pictureBox1.Image = ConvertToGrayScale(pict_C);
}
//Black and White1
private void button4_Click(object sender, EventArgs e)
{
pict_C = new Bitmap(pictureBox1.Image);
pictureBox1.Image = Thresholding1(pict_C);
}
//invert
private void button3_Click(object sender, EventArgs e)
{
pict_C = new Bitmap(pictureBox1.Image);
pictureBox1.Image = Invert(pict_C);
}
//grayscale2
private void button5_Click(object sender, EventArgs e)
{
pict_C = new Bitmap(pictureBox1.Image);
pictureBox1.Image = ConvertToGrayScale2(pict_C);
}

private void btnNagative_Click(object sender, EventArgs e)


{
pict_C = new Bitmap(pictureBox1.Image);
pictureBox1.Image = Negative(pict_C);
}
}
}

You might also like