- 히스토그램 -
1. 기본 Form
2. Source Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace CV_HW_1
{
public partial class Form1 : Form
{
OpenFileDialogofd = new OpenFileDialog(); // OpenFileDialog 객체 생성
Bitmapimage;
BitmapimageGray = new Bitmap(256,256);
Bitmaphistogram = new Bitmap(768,256);
Bitmaphistogram_R = new Bitmap(256,256);
Bitmaphistogram_G = new Bitmap(256,256);
Bitmaphistogram_B = new Bitmap(256,256);
System.Drawing.Color[,]tmpColor;
ColorclrNew = new Color();
privatestring imageLocation = null; // 불러올 파일 경로 변수 초기화
privateint imageWidth = 0;
privateint imageHeight = 0;
double[]HIST_R = new double[256];
double[]HIST_G = new double[256];
double[]HIST_B = new double[256];
double[]HIST = new double[256];
publicForm1()
{
InitializeComponent();
}
privatevoid Form1_Load(objectsender, EventArgs e)
{
}
privatevoid btnFileOpen_Click(objectsender, EventArgs e)
{
inttValue;
imageLocation = getLocation();
image = newBitmap(@imageLocation);
pictureBox1.Image = image;
label15.Text = "프로그램 상태 : 이미지 파일 열었습니다.";
getPixel();
for(int y = 0; y <= 255; y++)
{
for(int x = 0; x <= 255; x++)
{
tValue = (tmpColor[x, y].R+ tmpColor[x, y].G + tmpColor[x, y].B) / 3;
clrNew = Color.FromArgb(tValue, tValue, tValue);
imageGray.SetPixel(x, y,clrNew);
}
}
pictureBox2.Image = imageGray;
}
privatevoid getPixel()
{
// 이미지 크기 얻기
imageWidth = image.Width;
imageHeight = image.Height;
// 이미지 크기에 맞는 동적배열 생성
//tmpColor = new Color[imageWidth, imageHeight];
tmpColor = newColor[256, 256];
// color값 얻어오기
for(int i = 0; i <= 255; i++)
{
for(int j = 0; j <= 255; j++)
{
tmpColor[i, j] =(image.GetPixel(i, j));
}
}
}
// 파일경로 리턴
public string getLocation()
{
if(ofd.ShowDialog() == DialogResult.OK)
{
returnofd.FileName;
}
returnnull;
}
// 책에있는 예제코드 수정되었음
public void Histogram()
{
intimageSize = imageWidth * imageHeight; // 이미지 사이즈 구하기
inttemp = 0;
// HIST 배열 초기화
for(int i = 0; i <= 255; i++)
{
HIST[i] = 0;
HIST_R[i] = 0;
HIST_G[i] = 0;
HIST_B[i] = 0;
}
label15.Text = "프로그램 상태 : 히스토그램 얻어오는 중.";
// 영상 읽기, 히스토그램 구하기 (수평방향진행)
for(int y = 0; y <= 255; y++)
{
for(int x = 0; x <= 255; x++)
{
temp = (int)((tmpColor[x, y].R * 0.299) + (tmpColor[x, y].G *0.587) + (tmpColor[x, y].B * 0.114));
HIST[temp]++; // 각 명암도가 있을때마다 1 증가
temp = tmpColor[x, y].R;
HIST_R[temp]++; // 각 명암도가 있을때마다 1 증가
temp = tmpColor[x, y].G;
HIST_G[temp]++; // 각 명암도가 있을때마다 1 증가
temp = tmpColor[x, y].B;
HIST_B[temp]++; // 각 명암도가 있을때마다 1 증가
}
}
label15.Text = "프로그램 상태 : 히스토그램 정규화 중. 기다려주세요.^^";
// 히스토그램 정규화
for(int i = 0; i <= 255; i++)
{ // 이미지 사이즈로 나눈다.
HIST[i] = HIST[i] / imageSize *10000;
HIST_R[i] = HIST_R[i] /imageSize * 10000;
HIST_G[i] = HIST_G[i] /imageSize * 10000;
HIST_B[i] = HIST_B[i] /imageSize * 10000;
}
label15.Text = "프로그램 상태 : 히스토그램 정규화 완료";
}
privatevoid btnDraqHist_Click(objectsender, EventArgs e)
{
Histogram();
label15.Text = "프로그램 상태 : 히스토그램 그리기 시작.";
drawHist(HIST_R, pictureBox4,histogram_R);
drawHist(HIST_G, pictureBox5,histogram_G);
drawHist(HIST_B, pictureBox6,histogram_B);
for(int i = 0; i <= 255; i++)
{
histogram.SetPixel(i * 3, (int)(256 - HIST[i] - 1), Color.Black);
for(int y = (int)(256- HIST[i] - 1); y < 256; y++) histogram.SetPixel(i * 3, y, Color.Black);
}
pictureBox3.Image = histogram;
label15.Text = "프로그램 상태 : 히스토그램 그리기 완료하였습니다.";
}
public void drawHist(double[]arr, PictureBox pic, Bitmap hist)
{
for(int i = 0; i <= 255; i++)
{
hist.SetPixel(i, (int)(256 - arr[i] - 1), Color.Black);
for(int y = (int)(256- arr[i] - 1); y < 256; y++) hist.SetPixel(i, y, Color.Black);
}
pic.Image = hist;
}
}
}
3. 실행화면
1) 그림파일열기 버튼 클릭해서 그림을 불러왔을 때
좌측에 원본이미지가 출력, 오른쪽에 그레이스케일로 변환된 이미지가 출력된다.
2) 히스토그램 그리기 버튼을 클릭했을 때,
가장 큰 picture box에 명암도 히스토그램을 출력한다.
그 아래로 R, G, B 각각의 값들에 대한 히스토그램을 출력한다.
'General > Graphics / Vision' 카테고리의 다른 글
[컴퓨터그래픽스] 스탠드 그리기 (0) | 2010.10.19 |
---|---|
[컴퓨터그래픽스] 곡간(곳간)그리기. 7면체 (1) | 2010.10.18 |
[컴퓨터그래픽스] OpenGL 라이브러리 설치 (0) | 2010.10.12 |
[컴퓨터그래픽스] Shear (1) | 2010.10.04 |
0902 - 컴퓨터그래픽스 (0) | 2010.09.08 |