0% found this document useful (0 votes)
75 views23 pages

Tugas Citra 4

The document is a lab report from a student named Reynold Andika discussing image processing. It includes screenshots of the results of running a program to process an image and display its channels and histograms in various configurations. It also includes the coding and explanations of functions used to open the image, separate channels, calculate histograms, and display the results.

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views23 pages

Tugas Citra 4

The document is a lab report from a student named Reynold Andika discussing image processing. It includes screenshots of the results of running a program to process an image and display its channels and histograms in various configurations. It also includes the coding and explanations of functions used to open the image, separate channels, calculate histograms, and display the results.

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Laporan Praktikum Citra

MK 411
Nama : Reynold Andika
NIM : 4212001011
Laporan minggu ke :4
Hari, tanggal : Wednesday, 16 March 2022
Waktu : 19:35:43

Screen shoot GUI hasil running anda

Pengolahan Citra (MK-411) 1|


Screen shoot semua hasil running program anda

Three Channels
• RGB

Pengolahan Citra (MK-411) 2|


• RBG

• GRB

Pengolahan Citra (MK-411) 3|


• GBR

• BGR

Pengolahan Citra (MK-411) 4|


• BRG

One Channel
• R image

Pengolahan Citra (MK-411) 5|


• G image

• B image

Pengolahan Citra (MK-411) 6|


• Gray image

Coding :
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;

namespace Tugas_citra_4_Rey
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//fullscreen display

Pengolahan Citra (MK-411) 7|


WindowState = FormWindowState.Maximized;
//initialization textBox
textBox1.Text = "256";
chart1.Series.Clear();
}

//global variable
Bitmap sourceImage; //original image
int imageHeight, imageWidth;
int BIN = 256; //histogram bin

private void radioButton1_CheckedChanged(object sender, EventArgs e)


{
threeChannelDisplay(1);
}

private void pictureBox1_Click(object sender, EventArgs e)


{

private void textBox2_TextChanged(object sender, EventArgs e)


{

private void label2_Click(object sender, EventArgs e)


{

private void groupBox1_Enter(object sender, EventArgs e)


{
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;

Pengolahan Citra (MK-411) 8|


radioButton5.Checked = false;
radioButton6.Checked = false;
}

private void label3_Click(object sender, EventArgs e)


{

private void label4_Click(object sender, EventArgs e)


{

private void groupBox2_Enter(object sender, EventArgs e)


{
radioButton7.Checked = false;
radioButton8.Checked = false;
radioButton9.Checked = false;
radioButton10.Checked = false;
}
private void OpenFile()
{

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
sourceImage = (Bitmap)Bitmap.FromFile(openFileDialog1.FileName);

//display original image


pictureBox1.Image = sourceImage;

//determining image width and height


imageHeight = sourceImage.Height;
imageWidth = sourceImage.Width;
}
}

Pengolahan Citra (MK-411) 9|


private void button1_Click(object sender, EventArgs e)
{
OpenFile();
}
//converting RGB to other Channel Image
private Bitmap threeChannelImage(int channel)
{
//channel 1 = RGB, channel 2 = RBG, channel 3 = GRB
//channel 4 = GBR, channel 5 = BGR, channel 6 = BRG

//avoiding if the source image is null


if (sourceImage == null) return null;

Bitmap threeChannel = new Bitmap(sourceImage);

for (int x = 0; x < imageWidth; x++)


for (int y = 0; y < imageHeight; y++)
{
//get the RGB value of the pixel at (x,y)
Color w = sourceImage.GetPixel(x, y);

byte r = w.R; //red value


byte g = w.G; //green value
byte b = w.B; //blue value

//set the color of each channel


Color redgreenblueColor = Color.FromArgb(r, g, b);
Color redbluegreenColor = Color.FromArgb(r, b, g);
Color greenredblueColor = Color.FromArgb(g, r, b);
Color greenblueredColor = Color.FromArgb(g, b, r);
Color bluegreenredColor = Color.FromArgb(b, g, r);
Color blueredgreenColor = Color.FromArgb(b, r, g);

//set the image pixel


if (channel == 1)
{
threeChannel.SetPixel(x, y, redgreenblueColor);

Pengolahan Citra (MK-411) 10 |


}
else if (channel == 2)
{
threeChannel.SetPixel(x, y, redbluegreenColor);
}
else if (channel == 3)
{
threeChannel.SetPixel(x, y, greenredblueColor);
}
else if (channel == 4)
{
threeChannel.SetPixel(x, y, greenblueredColor);
}
else if (channel == 5)
{
threeChannel.SetPixel(x, y, bluegreenredColor);
}
else if (channel == 6)
{
threeChannel.SetPixel(x, y, blueredgreenColor);
}
}
return threeChannel;
}
private float[] threeChannelHistogram(int channel)
{
//channel 1 = RGB, channel 2 = RBG, channel 3 = GRB
//channel 4 = GBR, channel 5 = BGR, channel 6 = BRG
BIN = int.Parse(textBox1.Text);//init of bins

//initialization of arrayhistogram
float[] h = new float[BIN * 3];

//histogram init
for (int i = 0; i < BIN * 3; i++)
{
h[i] = 0;

Pengolahan Citra (MK-411) 11 |


}

//histogram calculation
for (int x = 0; x < sourceImage.Width; x++)
for (int y = 0; y < sourceImage.Height; y++)
{
Color w = sourceImage.GetPixel(x, y);
int r = (int)(w.R * BIN / 256);
int g = (int)(w.G * BIN / 256);
int b = (int)(w.B * BIN / 256);

//calculate histogram
if (channel == 1)
{
h[r] = h[r] + 1;
h[g + BIN] = h[g + BIN] + 1;
h[b + 2 * BIN] = h[b + 2 * BIN] + 1;
}
else if (channel == 2)
{
h[r] = h[r] + 1;
h[b + BIN] = h[b + BIN] + 1;
h[g + 2 * BIN] = h[g + 2 * BIN] + 1;
}
else if (channel == 3)
{
h[g] = h[g] + 1;
h[r + BIN] = h[r + BIN] + 1;
h[b + 2 * BIN] = h[b + 2 * BIN] + 1;
}
else if (channel == 4)
{
h[g] = h[g] + 1;
h[b + BIN] = h[b + BIN] + 1;
h[r + 2 * BIN] = h[r + 2 * BIN] + 1;
}
else if (channel == 5)

Pengolahan Citra (MK-411) 12 |


{
h[b] = h[b] + 1;
h[g + BIN] = h[g + BIN] + 1;
h[r + 2 * BIN] = h[r + 2 * BIN] + 1;
}
else if (channel == 6)
{
h[b] = h[b] + 1;
h[r + BIN] = h[r + BIN] + 1;
h[g + 2 * BIN] = h[g + 2 * BIN] + 1;
}
}
return h;
}

private void threeChannelDisplay(int channel)


{
//channel 1 = RGB, channel 2 = RBG, channel 3 = GRB
//channel 4 = GBR, channel 5 = BGR, channel 6 = BRG

if (sourceImage == null) return;

//delete the histogram


if (chart1.Series.Count > 0)
{
chart1.Series.RemoveAt(0);
}
//chart init
if (channel == 1)
{
chart1.Series.Add("RGB Channel Image");
chart1.Series["RGB Channel Image"].Color = Color.Red;
}
else if (channel == 2)
{
chart1.Series.Add("RBG Channel Image");
chart1.Series["RBG Channel Image"].Color = Color.DarkRed;

Pengolahan Citra (MK-411) 13 |


}
else if (channel == 3)
{
chart1.Series.Add("GRB Channel Image");
chart1.Series["GRB Channel Image"].Color = Color.Green;
}
else if (channel == 4)
{
chart1.Series.Add("GBR Channel Image");
chart1.Series["GBR Channel Image"].Color = Color.DarkGreen;
}
else if (channel == 5)
{
chart1.Series.Add("BGR Channel Image");
chart1.Series["BGR Channel Image"].Color = Color.Blue;
}
else if (channel == 6)
{
chart1.Series.Add("BRG Channel Image");
chart1.Series["BRG Channel Image"].Color = Color.DarkBlue;
}

foreach (var series in chart1.Series)


{
series.Points.Clear();
}

float[] his = new float[BIN * 3];


his = threeChannelHistogram(channel);

for (int i = 0; i < BIN * 3; i++)


{
if (channel == 1) chart1.Series["RGB Channel Image"].Points.AddXY(i,
his[i]);
else if (channel == 2) chart1.Series["RBG Channel Image"].Points.AddXY(i,
his[i]);

Pengolahan Citra (MK-411) 14 |


else if (channel == 3) chart1.Series["GRB Channel Image"].Points.AddXY(i,
his[i]);
else if (channel == 4) chart1.Series["GBR Channel Image"].Points.AddXY(i,
his[i]);
else if (channel == 5) chart1.Series["BGR Channel Image"].Points.AddXY(i,
his[i]);
else if (channel == 6) chart1.Series["BRG Channel Image"].Points.AddXY(i,
his[i]);
}
//displaying the image

if (channel == 1)
{
Bitmap rgbImage = threeChannelImage(channel);
pictureBox1.Image = rgbImage;
label1.Text = "RGB Channel Image";
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;
label2.Text = string.Format("RGB Image Histogram {0} Bins", BIN * 3);
}
else if (channel == 2)
{
Bitmap rbgImage = threeChannelImage(channel);
pictureBox1.Image = rbgImage;
label1.Text = "RBG Channel Image";
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;
label2.Text = string.Format("RBG Channel Image Histogram {0} Bins", BIN
* 3);
}
else if (channel == 3)
{
Bitmap grbImage = threeChannelImage(channel);
pictureBox1.Image = grbImage;
label1.Text = "GRB Channel Image";
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;

Pengolahan Citra (MK-411) 15 |


label2.Text = string.Format("GRB Channel Image Histogram {0} Bins", BIN
* 3);
}
else if (channel == 4)
{
Bitmap gbrImage = threeChannelImage(channel);
pictureBox1.Image = gbrImage;
label1.Text = "GBR Channel Image";
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;
label2.Text = string.Format("GBR Channel Image Histogram {0} Bins", BIN
* 3);
}
else if (channel == 5)
{
Bitmap bgrImage = threeChannelImage(channel);
pictureBox1.Image = bgrImage;
label1.Text = "BGR Channel Image";
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;
label2.Text = string.Format("BGR Channel Image Histogram {0} Bins", BIN
* 3);
}
else if (channel == 6)
{
Bitmap brgImage = threeChannelImage(channel);
pictureBox1.Image = brgImage;
label1.Text = "BRG Channel Image";
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;
label2.Text = string.Format("BRG Channel Image Histogram {0} Bins", BIN
* 3);
}
}

Pengolahan Citra (MK-411) 16 |


//converting RGB to One Channel Image
private Bitmap oneChannelImage(int channel)
{
//channel 1 = R, channel 2 = G, channel 3 = B, channel 4 = gray image
//avoiding if the source image is null
if (sourceImage == null) return null;

Bitmap oneChannel = new Bitmap(sourceImage);

for (int x = 0; x < imageWidth; x++)


for (int y = 0; y < imageHeight; y++)
{
//get the RGB value of the pixel at (x,y)
Color w = sourceImage.GetPixel(x, y);

byte r = w.R; //red value


byte g = w.G; //green value
byte b = w.B; //blue value

//set the color of each channel


Color redColor = Color.FromArgb(r, 0, 0);
Color greenColor = Color.FromArgb(0, g, 0);
Color blueColor = Color.FromArgb(0, 0, b);
Color grayImage = Color.FromArgb(g, g, g);

//set the image pixel


if (channel == 1)
{
oneChannel.SetPixel(x, y, redColor);
}
else if (channel == 2)
{
oneChannel.SetPixel(x, y, greenColor);
}
else if (channel == 3)
{
oneChannel.SetPixel(x, y, blueColor);

Pengolahan Citra (MK-411) 17 |


}
else if (channel == 4)
{
oneChannel.SetPixel(x, y, grayImage);
}
}
return oneChannel;
}

private float[] oneChannelHistogram(int channel)


{
//channel 1 = R, channel 2 = G, channel 3 = B, channel 4 = gray image
BIN = int.Parse(textBox1.Text);//init of bins

//initialization of arrayhistogram
float[] h = new float[BIN];

//histogram init
for (int i = 0; i < BIN; i++)
{
h[i] = 0;
}

//histogram calculation
for (int x = 0; x < sourceImage.Width; x++)
for (int y = 0; y < sourceImage.Height; y++)
{
Color w = sourceImage.GetPixel(x, y);
int r = (int)(w.R * BIN / 256);
int g = (int)(w.G * BIN / 256);
int b = (int)(w.B * BIN / 256);

//calculate gray channel = (r+g+b)/3


byte gray = (byte)(0.5 * r + 0.419 * g + 0.081 * b);

//calculate histogram
if (channel == 1)

Pengolahan Citra (MK-411) 18 |


h[r] = h[r] + 1;
else if (channel == 2)
h[g] = h[g] + 1;
else if (channel == 3)
{
h[b] = h[b] + 1;
}
else if (channel == 4)
{
h[gray] = h[gray] + 1;
}
}
return h;
}
private void oneChannelDisplay(int channel)
{
//channel 1 = R, channel 2 = G, channel 3 = B, channel 4 = gray image

if (sourceImage == null) return;

//delete the histogram


if (chart1.Series.Count > 0)
{
chart1.Series.RemoveAt(0);
}
//chart init
if (channel == 1)
{
chart1.Series.Add("Red Channel Image");
chart1.Series["Red Channel Image"].Color = Color.Red;
}
else if (channel == 2)
{
chart1.Series.Add("Green Channel Image");
chart1.Series["Green Channel Image"].Color = Color.Green;
}
else if (channel == 3)

Pengolahan Citra (MK-411) 19 |


{
chart1.Series.Add("Blue Channel Image");
chart1.Series["Blue Channel Image"].Color = Color.Blue;
}
else if (channel == 4)
{
chart1.Series.Add("Gray Channel Image");
chart1.Series["Gray Channel Image"].Color = Color.Gray;
}

foreach (var series in chart1.Series)


{
series.Points.Clear();
}

float[] his = new float[BIN];


his = oneChannelHistogram(channel);

for (int i = 0; i < BIN; i++)


{
if (channel == 1) chart1.Series["Red Channel Image"].Points.AddXY(i,
his[i]);
else if (channel == 2) chart1.Series["Green Channel
Image"].Points.AddXY(i, his[i]);
else if (channel == 3) chart1.Series["Blue Channel Image"].Points.AddXY(i,
his[i]);
else if (channel == 4) chart1.Series["Gray Channel Image"].Points.AddXY(i,
his[i]);
}
//displaying the image

if (channel == 1)
{
Bitmap redImage = oneChannelImage(channel);
pictureBox1.Image = redImage;
label1.Text = "Red Channel Image";
label1.ForeColor = Color.White;

Pengolahan Citra (MK-411) 20 |


label2.ForeColor = Color.White;
label2.Text = string.Format("Red Channel Image Histogram {0} Bins", BIN);
}
else if (channel == 2)
{
Bitmap greenImage = oneChannelImage(channel);
pictureBox1.Image = greenImage;
label1.Text = "Green Channel Image";
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;
label2.Text = string.Format("Green Channel Image Histogram {0} Bins",
BIN);
}
else if (channel == 3)
{
Bitmap blueImage = oneChannelImage(channel);
pictureBox1.Image = blueImage;
label1.Text = "Blue Channel Image";
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;
label2.Text = string.Format("Blue Channel Image Histogram {0} Bins",
BIN);
}
else if (channel == 4)
{
Bitmap grayImage = oneChannelImage(channel);
pictureBox1.Image = grayImage;
label1.Text = "Gray Channel Image";
label1.ForeColor = Color.White;
label2.ForeColor = Color.White;
label2.Text = string.Format("Gray Channel Image Histogram {0} Bins",
BIN);
}
}

private void radioButton7_CheckedChanged(object sender, EventArgs e)


{

Pengolahan Citra (MK-411) 21 |


oneChannelDisplay(1);
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)


{
threeChannelDisplay(2);
}

private void radioButton3_CheckedChanged(object sender, EventArgs e)


{
threeChannelDisplay(3);
}

private void radioButton4_CheckedChanged(object sender, EventArgs e)


{
threeChannelDisplay(4);
}

private void radioButton5_CheckedChanged(object sender, EventArgs e)


{

threeChannelDisplay(5);
}

private void radioButton6_CheckedChanged(object sender, EventArgs e)


{

threeChannelDisplay(6);
}

private void radioButton8_CheckedChanged(object sender, EventArgs e)


{
oneChannelDisplay(2);
}

private void radioButton9_CheckedChanged(object sender, EventArgs e)


{

Pengolahan Citra (MK-411) 22 |


oneChannelDisplay(3);
}

private void radioButton10_CheckedChanged(object sender, EventArgs e)


{
oneChannelDisplay(4);
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}

}
}

Pengolahan Citra (MK-411) 23 |

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy