⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clusterkltransformationview.cpp

📁 KL变换的实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ClusterKLTransformationView.cpp : implementation of the CClusterKLTransformationView class
//

#include "stdafx.h"
#include "ClusterKLTransformation.h"

#include "ClusterKLTransformationDoc.h"
#include "ClusterKLTransformationView.h"

#include "cdib.h"
#include "Matrix.h"//加入附加的dib和矩阵计算类

#include "KL.h"
#include "Histo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define WORD_HEIGHT 18 //数字的高度,根据系统的字体调整 


#define WORD_WIDTH 7 //数字的宽度,根据系统的字体调整 


#define OFF_X 15 //坐标显示的位置与鼠标的距离 


#define OFF_Y 10 //一般在鼠标的右下角 


#define TEXT_COLOR RGB(255,0,0) //坐标文字的颜色 

/////////////////////////////////////////////////////////////////////////////
// CClusterKLTransformationView

IMPLEMENT_DYNCREATE(CClusterKLTransformationView, CScrollView)

BEGIN_MESSAGE_MAP(CClusterKLTransformationView, CScrollView)
	//{{AFX_MSG_MAP(CClusterKLTransformationView)
	ON_COMMAND(ID_TM_IN, OnTmIn)
	ON_COMMAND(ID_FIRST, OnFirst)
	ON_COMMAND(ID_SECOND, OnSecond)
	ON_COMMAND(ID_THIRD, OnThird)
	ON_COMMAND(ID_LISTSHOW, OnListshow)
	ON_COMMAND(ID_HISTOGRAM, OnHistogram)
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClusterKLTransformationView construction/destruction

CClusterKLTransformationView::CClusterKLTransformationView()
{
	// TODO: add construction code here
	hwnd = NULL;
	AllPixel=0;
	
}

CClusterKLTransformationView::~CClusterKLTransformationView()
{
}

BOOL CClusterKLTransformationView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}
void CClusterKLTransformationView::Histogram() 
{	
	// 创建对话框
	CHisto dlgHistShow;
	dlgHistShow.GetpDoc(GetDocument());
	// 初始化变量值
    dlgHistShow.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CClusterKLTransformationView drawing

void CClusterKLTransformationView::OnDraw(	CDC* pDC)
{

	CClusterKLTransformationDoc* pDoc = GetDocument();
 	ASSERT_VALID(pDoc);
	CSize sizeDibDisplay;
	if(!pDoc->pDib->IsEmpty())
	{	
		sizeDibDisplay = pDoc->pDib->GetDimensions();
		pDoc->pDib->Draw(pDC,CPoint(0,0),sizeDibDisplay);	
	}

	
	

	CString strPixels;//在状态栏显示像素数目
	CStatusBar* pStatus=(CStatusBar*)AfxGetApp()->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR);
	if(pStatus)
	{
		
 		strPixels.Format("当前像素数:%d",AllPixel);
		pStatus->SetPaneText(1,strPixels);
	}
}

void CClusterKLTransformationView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CClusterKLTransformationView printing



BOOL CClusterKLTransformationView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CClusterKLTransformationView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CClusterKLTransformationView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CClusterKLTransformationView diagnostics

#ifdef _DEBUG
void CClusterKLTransformationView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CClusterKLTransformationView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CClusterKLTransformationDoc* CClusterKLTransformationView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CClusterKLTransformationDoc)));
	return (CClusterKLTransformationDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CClusterKLTransformationView message handlers

void CClusterKLTransformationView::OnTmIn()  //TM图象数据读入
{
	// TODO: Add your command handler code here
	AfxMessageBox("操 作 提 示 :\n\n请 将 6 幅 TM 图 像 一 次 读 入 !",MB_ICONINFORMATION,0);//读图象的方法

	int flag=0;

	while(flag<6)
	{
	CFileDialog dlg(TRUE,"bmp",".bmp");
	if(dlg.DoModal()==IDOK)
	{
		m_NewFileName[flag]=dlg.GetFileName();
		for(int j=0;j<5;j++)
		{
			if(flag==j||m_NewFileName[flag]!=m_NewFileName[j])  continue;
			else 
			{
				AfxMessageBox("已打开过的图象!请按方法重新打开!");
				flag=0;
	            for(int i=0;i<6;i++) m_NewFileName[i]="";
				return;
			}
		}
		NewFileName=m_NewFileName[flag];
		flag++;
	}//判断是否是已经打开的图象,若已经打开,则要重新按方法打开

	else //不读图象,或是只是要看图象
	{
		for(int i=0;i<6;i++) m_NewFileName[i]="";
		return;
	}

	CDC *pDC = GetWindowDC();
	CFile f(NewFileName,0);	
	m_pDib.Read(&f);//读图象

	if(m_pDib.m_nColorTableEntries!=256)//判断是否是灰度图象
	{
		AfxMessageBox("请打开灰度图象!");
		return;
	}

	CClusterKLTransformationDoc* pDoc = GetDocument();
    pDoc->pDib=&m_pDib;
	OnDraw(pDC);//调用OnDraw函数绘图

	sizeImage = m_pDib.GetDimensions();
    sizeSaveImage = m_pDib.GetDibSaveDim();
    a=sizeSaveImage.cx * sizeSaveImage.cy;//象素数

	if(flag==1) tm=CMatrix(6,a);//开第一副图象时为矩阵分配内存

    //将每幅图象象素灰度值放入矩阵中
	if(flag==1)
		for(int i=0;i<a;i++)
		{
			tm.m_pData[i]=*(m_pDib.m_lpImage+sizeSaveImage.cx*(i/sizeImage.cx)+i%sizeImage.cx);
		}

	if(flag==2)
		for(int i=0;i<a;i++)
		{
			tm.m_pData[a+i]=*(m_pDib.m_lpImage+sizeSaveImage.cx*(i/sizeImage.cx)+i%sizeImage.cx);
		}

	if(flag==3)
		for(int i=0;i<a;i++)
		{
			tm.m_pData[2*a+i]=*(m_pDib.m_lpImage+sizeSaveImage.cx*(i/sizeImage.cx)+i%sizeImage.cx);
		}

	if(flag==4)
		for(int i=0;i<a;i++)
		{
			tm.m_pData[3*a+i]=*(m_pDib.m_lpImage+sizeSaveImage.cx*(i/sizeImage.cx)+i%sizeImage.cx);
		}

	if(flag==5)
		for(int i=0;i<a;i++)
		{
			tm.m_pData[4*a+i]=*(m_pDib.m_lpImage+sizeSaveImage.cx*(i/sizeImage.cx)+i%sizeImage.cx);
		}

	if(flag==6)
		for(int i=0;i<a;i++)
		{
			tm.m_pData[5*a+i]=*(m_pDib.m_lpImage+sizeSaveImage.cx*(i/sizeImage.cx)+i%sizeImage.cx);
		}
	}
		
}

void CClusterKLTransformationView::Draw() //图象输出
{
	int i;
	for(i=0;i<a;i++)//处理越界的灰度值
	{
		if(result.m_pData[i]<0) result.m_pData[i]=0;
        if(result.m_pData[i]>255) result.m_pData[i]=255;
	}

    for (i = 0; i <a; i ++)//重新给图象每个象素赋灰度值
	{
		unsigned char *pData=m_pDib.m_lpImage+sizeSaveImage.cx*(i/sizeImage.cx)+i%sizeImage.cx;
		*pData=(BYTE)(long)(result.m_pData[i]+0.5);
	}
	CDC *pDC = GetWindowDC();
    CClusterKLTransformationDoc* pDoc = GetDocument();
    pDoc->pDib=&m_pDib;
	OnDraw(pDC);//将处理后图象绘出
}

void CClusterKLTransformationView::OnFirst() 
{

 //第一主分量

    if(NewFileName=="") {AfxMessageBox("请先读入图象数据!");return;}
	if(fea.m_pData[0]==0) OnKl();
	result.Init(1,a);
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<6;j++)
        result.m_pData[i]+=fea.m_pData[index[5]+j*6]*tm.m_pData[j*a+i];
	}
    Draw();

	CClusterKLTransformationDoc * pDoc = GetDocument();
	CDib *m_pDib;
	m_pDib = pDoc->pDib ;
	int m_bcount[256];
	int m_gcount[256];
	int m_rcount[256];
	int l,j;
	unsigned char * lpSrc;
	CSize sizeImage;
	sizeImage = m_pDib->GetDimensions();


	// 获得图象数据存储的高度和宽度
	CSize sizeSaveImage;
	sizeSaveImage = m_pDib->GetDibSaveDim();

	// 重置计数为0
	for (l = 0; l < 256; l ++)
	{
		// 清零
		m_bcount[l] = 0;
		m_gcount[l] = 0;
		m_rcount[l] = 0;
	}
	
	// 计算各个灰度值的计数,即得到直方图
	for (l = 0; l < sizeImage.cy; l ++)
	{
		for (j = 0; j < sizeImage.cx; j ++)
		{
			if(m_pDib->NumberColors()==8)
			{
			lpSrc = (unsigned char *)m_pDib->m_lpImage + sizeSaveImage.cx * l + j;
			
			// 计数加1
			m_bcount[*(lpSrc)]++;
			}
			else if(m_pDib->NumberColors()==24)
			{
			lpSrc = (unsigned char *)m_pDib->m_lpImage + sizeSaveImage.cx * l + j*3;
			
			// 计数加1
			m_bcount[*(lpSrc)]++;
			m_gcount[*(lpSrc+1)]++;
            m_rcount[*(lpSrc+2)]++;
			}
			
		}
	}


	double bcount=0;
	double gcount=0;
	double rcount=0;

	for(l=0;l<256;l++)
	{

		bcount+=m_bcount[l];
		gcount+=m_gcount[l];
		rcount+=m_rcount[l];
		
	}

	double x=bcount+gcount+rcount;
	AllPixel=&x;
}

void CClusterKLTransformationView::OnSecond() 
{
 //第二主分量
	if(NewFileName=="") {AfxMessageBox("请先读入图象数据!");return;}
	if(fea.m_pData[0]==0) OnKl();
	result.Init(1,a);
	for(int i=0;i<a;i++)
	{
		for(int j=0;j<6;j++)
        result.m_pData[i]+=fea.m_pData[index[4]+j*6]*tm.m_pData[j*a+i];
	}
	Draw();


	CClusterKLTransformationDoc * pDoc = GetDocument();
	CDib *m_pDib;
	m_pDib = pDoc->pDib ;
	int m_bcount[256];
	int m_gcount[256];
	int m_rcount[256];
	int l,j;
	unsigned char * lpSrc;
	CSize sizeImage;
	sizeImage = m_pDib->GetDimensions();


	// 获得图象数据存储的高度和宽度
	CSize sizeSaveImage;
	sizeSaveImage = m_pDib->GetDibSaveDim();

	// 重置计数为0
	for (l = 0; l < 256; l ++)
	{
		// 清零
		m_bcount[l] = 0;
		m_gcount[l] = 0;
		m_rcount[l] = 0;
	}
	
	// 计算各个灰度值的计数,即得到直方图
	for (l = 0; l < sizeImage.cy; l ++)
	{
		for (j = 0; j < sizeImage.cx; j ++)
		{
			if(m_pDib->NumberColors()==8)
			{
			lpSrc = (unsigned char *)m_pDib->m_lpImage + sizeSaveImage.cx * l + j;
			
			// 计数加1
			m_bcount[*(lpSrc)]++;
			}
			else if(m_pDib->NumberColors()==24)
			{
			lpSrc = (unsigned char *)m_pDib->m_lpImage + sizeSaveImage.cx * l + j*3;
			
			// 计数加1
			m_bcount[*(lpSrc)]++;
			m_gcount[*(lpSrc+1)]++;
            m_rcount[*(lpSrc+2)]++;
			}
			
		}
	}


	double bcount=0;
	double gcount=0;
	double rcount=0;

	for(l=0;l<256;l++)
	{

		bcount+=m_bcount[l];
		gcount+=m_gcount[l];
		rcount+=m_rcount[l];
		
	}

	double x=bcount+gcount+rcount;
	AllPixel=&x;
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -