// 直方图Dlg.cpp : implementation file
//二维图像的直方图均衡化 V1.0版
//本程序可用于jpg、bmp、png等格式的二维图像的直方图均衡化,并显示直方图与处理结果
//——By周靖昆 2013.1.6
//
#include "stdafx.h"
#include "直方图.h"
#include "直方图Dlg.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <windows.h>
#include <stdio.h>
#define HDIM 256
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, Onclick)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_BN_CLICKED(IDC_BUTTON2, OnCLICK)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
}
void CMyDlg::OnCLICK()
{
// TODO: Add your control notification handler code here
CString FilePathName;
CFileDialog dlg(TRUE,NULL,NULL,NULL, "JPEG Files(*.jpg)|*.jpg|BMP Files(*.bmp)|*.bmp|PNG Files(*.png)|*.png|All Files(*.*)|*.*||",NULL);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
if(dlg.DoModal()==IDOK)
FilePathName=dlg.GetPathName();
GetDlgItem(IDC_EDIT1)->SetWindowText(FilePathName);
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::Onclick()
{
// TODO: Add your control notification handler code here
CString s;
GetDlgItemText(IDC_EDIT1,s);//读取输入框地址
IplImage *dst=0,*src=0; //IplImage结构体定义
CvHistogram *histogram;
CvHistogram *HistEqualization;
IplImage *histOriginal;//原图像的直方图
IplImage *HistEquAfter;//均衡化后直方图
float HistogramRange1[2]={0,255};
float *HistogramRange[1]={&HistogramRange1[0]};
int n=HDIM;//HDIM=256
double nn0[HDIM];
int srcnn00[HDIM];
uchar T0[HDIM];
CvMat *T_mat0;
int i=0;
int sum = 0; //图片的像素总数
double val0=0;//累加计数
int val1=0;
if((src=cvLoadImage((LPCTSTR)s,0)) == NULL) //图片灰度
{
MessageBox("Read Image Error!","Error!",MB_OK|MB_ICONEXCLAMATION|MB_SYSTEMMODAL);
}
else
{
ShowWindow(SW_HIDE); //隐藏窗口
//原图像的直方图
histOriginal = cvCreateImage(cvSize(300,400),8,3);//图像的大小是300×400,图像颜色深度8位,3通道图像
histOriginal->origin=1;/*图像原点位置: 0表示顶-左结构,1表示底-左结构 */
histogram = cvCreateHist(1,&n,CV_HIST_ARRAY,HistogramRange,1); //建立原图像的直方图
cvCalcHist(&src,histogram,0,0); //计算直方图
// 计算总像素数
sum = src->height * src->width;
for( i = 0; i < n; i++ )
{
val0 = val0 + cvGetReal1D (histogram->bins, i);
nn0[i]=val0;
T0[i] = (uchar) (255*nn0[i]/sum); // 范围是[0,255]
srcnn00[i]=(int)(nn0[i]*255/sum);
cvLine(histOriginal,cvPoint(i,0),cvPoint(i,(int)(cvQueryHistValue_1D(histogram,i)/3)),CV_RGB(127,127,127));
}
// 直方图均衡化
dst = cvCloneImage(src);
T_mat0 = cvCreateMatHeader(1,256,CV_8UC1);
cvSetData(T_mat0,T0,src->width );
cvLUT( src, dst, T_mat0);
//均衡化后的直方图
HistEquAfter = cvCreateImage(cvSize(300,400),8,3);
HistEquAfter->origin=1;
HistEqualization = cvCreateHist(1,&n,CV_HIST_ARRAY,HistogramRange,1);
cvCalcHist(&dst,HistEqualization,0,0);
for (i=0;i<n;i++)
{
cvLine(HistEquAfter,cvPoint(i,0),cvPoint(i,(int)(cvQueryHistValue_1D(HistEqualization,i)/3)),CV_RGB(127,127,127));
}
cvNamedWindow( "原图像", 1 );
cvMoveWindow("原图像", 0, 0);
cvNamedWindow( "处理后", 1 );
cvMoveWindow("处理后", 900, 0);
cvNamedWindow( "原图像直方图", 1 );
cvMoveWindow("原图像直方图", 340, 290);
cvNamedWindow( "处理后的直方图", 1 );
cvMoveWindow( "处理后的直方图", 640, 290);
cvShowImage( "原图像", src );//原图像
cvShowImage( "处理后", dst );//均衡化后图像
cvShowImage("原图像直方图",histOriginal);//原图像直方图
cvShowImage("处理后的直方图",HistEquAfter);//均衡化后直方图
cvWaitKey(0);
cvReleaseImage( &src );
cvReleaseImage( &dst );
cvReleaseImage(&histOriginal);
cvReleaseImage(&HistEquAfter);
cvReleaseHist ( &histogram);
cvReleaseHist(&HistEqualization);
cvDestroyWindow("原图像");
cvDestroyWindow("处理后");
cvDestroyWindow("原图像直方图");
cvDestroyWindow("处理后的直方图");
ShowWindow(SW_SHOW);
}
}
void CMyDlg::OnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::