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

📄 nggolekiginambarandlg.cpp

📁 基于图片内容进行匹配查找的VC 代码
💻 CPP
字号:
// NggolekiGinambaranDlg.cpp : implementation file
//
// Danang Suharno
// Ngadinegaran MJ 3/156
// Yogyakarta 55143
// Indonesia
//
// kinclong2@yahoo.co.uk
// www.geocities.com/kinclong2
//
//

#include "stdafx.h"
#include "NggolekiGinambaran.h"
#include "NggolekiGinambaranDlg.h"

#include "DirDialog.h"
#include "TokenEx.h"

#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()

/////////////////////////////////////////////////////////////////////////////
// CNggolekiGinambaranDlg dialog

CNggolekiGinambaranDlg::CNggolekiGinambaranDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNggolekiGinambaranDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNggolekiGinambaranDlg)
		// 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 CNggolekiGinambaranDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNggolekiGinambaranDlg)
	DDX_Control(pDX, IDC_LIST_FOUNDED, m_ListFound);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNggolekiGinambaranDlg, CDialog)
	//{{AFX_MSG_MAP(CNggolekiGinambaranDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_LOAD, OnButtonLoad)
	ON_LBN_SELCHANGE(IDC_LIST_FOUNDED, OnSelchangeListFounded)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNggolekiGinambaranDlg message handlers

BOOL CNggolekiGinambaranDlg::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 CNggolekiGinambaranDlg::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 CNggolekiGinambaranDlg::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();
		RefreshDisplay();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNggolekiGinambaranDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CNggolekiGinambaranDlg::OnButtonLoad() 
{
	
	//
	//
	//
	char szFilter[] = "Jpg File (*.jpg)\0*.jpg\0All Files (*.*)\0*.*\0\0";
	CFileDialog dlg(TRUE);
	CString strTemp;
	CString strTemp1;
	CString strNamaFile;
	ULONG ulJumlahRecords;
	int iLoopRecNo = 0;

	//
	CxImage imgTemp;
	CxImage rfft1;
	CxImage ifft1;
	int iWidth = 256;
	int iHeight = 256;
	
	int iPixelSize = 1; // 1=GrayScale, 3=24bpp, 4=32bpp
	int iBlockSize = 16;
	int i = iWidth/iBlockSize;
	int j = iHeight/iBlockSize;
	
	int iRow;
	int iCol;
	int iLoopRow;
	int iLoopCol;
	double dTemp;
	int iBlockNum = 0;
	long lVal1;
	BYTE *pImage;
	
	//
	//
	//
	ulJumlahRecords = 0L;
	//dlg.m_ofn.lpstrInitialDir = m_strLokasiLogFile;
	dlg.m_ofn.lpstrFilter = szFilter;

	if( dlg.DoModal() == IDOK )
	{
		strNamaFile = dlg.GetPathName();
		m_imgLoaded.Load(strNamaFile, CXIMAGE_FORMAT_JPG);
		RefreshDisplay();

		if( !m_imgLoaded.IsValid() ) return;
		
		//
		imgTemp.Copy(m_imgLoaded);
		imgTemp.Resample(iWidth, iHeight);
		imgTemp.FFT2(&imgTemp, &imgTemp, &rfft1, &ifft1);		
		pImage = rfft1.GetBits();

		//
		for(iRow = 0; iRow < iHeight; iRow += iBlockSize)
		{		
			for(iCol = 0; iCol < iWidth; iCol += iBlockSize )
			{
				// Block Processing
				m_KeySignature[iBlockNum] = 0.0;
				dTemp = 0.0;
				for(iLoopRow = 0; iLoopRow < iBlockSize; iLoopRow++ )
				{
					for(iLoopCol = 0; iLoopCol < iBlockSize; iLoopCol++ )
					{
						lVal1 = pImage[((iRow+iLoopRow)*iPixelSize*iWidth) + ((iCol+iLoopCol)*iPixelSize)];
						dTemp += (double) lVal1;
					}
				}
				
				m_KeySignature[iBlockNum] = sqrt(dTemp);
				iBlockNum++;
			}
		}
		
		//
		// Normalize
		//
		for(i = 0; i < 256; i++ )
		{
			dTemp += m_KeySignature[i];
		}
		
		for(i = 0; i < 256; i++ )
		{
			m_KeySignature[i] /= dTemp;
		}
		
	}
	else
	{
		AfxMessageBox("Please select a JPEG file!");
		return;
	}	
}

void CNggolekiGinambaranDlg::RefreshDisplay()
{
	if( m_imgLoaded.IsValid() )
	{
		CRect r;			
		CWnd *p = (CWnd*) GetDlgItem(IDC_STATIC_KEYIMAGE);
		CDC *pDC = p->GetDC();
		p->GetWindowRect(&r);
		
		
		m_imgLoaded.Stretch(pDC->m_hDC, 0, 0, r.Width(), r.Height());
		p->ReleaseDC(pDC);
	}

	if( m_imgFound.IsValid() )
	{
		CRect r;			
		CWnd *p = (CWnd*) GetDlgItem(IDC_STATIC_FOUNDIMAGE);
		CDC *pDC = p->GetDC();
		p->GetWindowRect(&r);
		
		
		m_imgFound.Stretch(pDC->m_hDC, 0, 0, r.Width(), r.Height());
		p->ReleaseDC(pDC);
	}
}

void CNggolekiGinambaranDlg::OnOK() 
{
	//CDialog::OnOK();
	CDirDialog dlg;

	//dlg.m_strInitDir = "C:\\";
	dlg.m_strSelDir  = "C:\\";
	dlg.m_strWindowTitle = "Select .JPEG File(s) Directory";
	dlg.m_strTitle = "Browse Folder for .JPG files location";
	m_strExtension = "*.JPG";
	
	if( dlg.DoBrowse(this) )
	{		
		//
		//
		//
		m_strBeginPath = dlg.m_strPath;
		m_dwJumlahFile = 0x0;

		CWinThread* pThreadAsu = AfxBeginThread(SiapCariRecursive, this);
	}
}

UINT __cdecl CNggolekiGinambaranDlg::SiapCariRecursive(LPVOID lpParam)
{
	CNggolekiGinambaranDlg *p = (CNggolekiGinambaranDlg *) lpParam;
	CWnd *w;
	
	w = (CWnd*) p->GetDlgItem(IDCANCEL);
	w->EnableWindow(FALSE);

	w = (CWnd*) p->GetDlgItem(IDOK);
	w->EnableWindow(FALSE);

	p->m_ListFound.ResetContent();

	//
	p->CariRecursive(p->m_strBeginPath, p->m_strExtension);

	w = (CWnd*) p->GetDlgItem(IDCANCEL);
	w->EnableWindow(TRUE);

	w = (CWnd*) p->GetDlgItem(IDOK);
	w->EnableWindow(TRUE);

	// Find the longest string in the list box.
	CString str;
	CSize   sz;
	int     dx=0;
	CDC*    pDC = p->m_ListFound.GetDC();
	for (int i=0;i < p->m_ListFound.GetCount();i++)
	{
		p->m_ListFound.GetText( i, str );
		sz = pDC->GetTextExtent(str);
		
		if (sz.cx > dx)
			dx = sz.cx;
	}
	p->m_ListFound.ReleaseDC(pDC);
	
	// Set the horizontal extent so every character of all strings 
	// can be scrolled to.
	p->m_ListFound.SetHorizontalExtent(dx);

	return TRUE;
}

void CNggolekiGinambaranDlg::CariRecursive(CString strInitPath, CString strFileName)
{
	CFileFind ff;
	CString strPath;
	BOOL bFound = TRUE;

	//
	// Cari Directory dahulu
	//
	strPath = strInitPath + "\\*";
	if( ff.FindFile(strPath) )
	{		
		while(bFound)
		{
			bFound = ff.FindNextFile();
			if(!ff.IsDots())
			{
				CString strFile = ff.GetFilePath();
				
				if(ff.IsDirectory())
				{
					CariRecursive(strFile, strFileName);
				}
			}
		}

		ScanFiles(strInitPath + "\\" + strFileName );
	}

	ff.Close();
}

void CNggolekiGinambaranDlg::ScanFiles(CString strNamaFile)
{
	CFileFind ff;
	BOOL bFound = TRUE;

	if( ff.FindFile(strNamaFile) )
	{
		while(bFound)
		{
			bFound = ff.FindNextFile();
			if((!ff.IsDots()) && (!ff.IsDirectory()))
			{
				CString strFile = ff.GetFilePath();
				
				//
				// Do image analysis
				//
				Madhosi(strFile);				
				m_dwJumlahFile++;				
			}
		}
	}

	ff.Close();
}

double CNggolekiGinambaranDlg::Madhosi(CString strNamaFile)
{
	CString strTemp;
	CxImage imageAsli;
	CxImage rfft1;
	CxImage ifft1;
	int iWidth = 256;
	int iHeight = 256;

	int iPixelSize = 1; // 1=GrayScale, 3=24bpp, 4=32bpp
	int iBlockSize = 16;
	int i = iWidth/iBlockSize;
	int j = iHeight/iBlockSize;

	int iRow;
	int iCol;
	int iLoopRow;
	int iLoopCol;
	double *lSignature;
	double dTemp;
	int iBlockNum = 0;
	long lVal1;
	BYTE *pImage;

	imageAsli.Load(strNamaFile, CXIMAGE_FORMAT_JPG);
	if( !imageAsli.IsValid() ) return 1.0;

	imageAsli.Resample(iWidth, iHeight);
	imageAsli.FFT2(&imageAsli, &imageAsli, &rfft1, &ifft1);

	lSignature = (double*) malloc(sizeof(double)*i*j);
	pImage = rfft1.GetBits();
	for(iRow = 0; iRow < iHeight; iRow += iBlockSize)
	{		
		for(iCol = 0; iCol < iWidth; iCol += iBlockSize )
		{
			// Block Processing
			lSignature[iBlockNum] = 0.0;
			dTemp = 0.0;
			for(iLoopRow = 0; iLoopRow < iBlockSize; iLoopRow++ )
			{
				for(iLoopCol = 0; iLoopCol < iBlockSize; iLoopCol++ )
				{
					lVal1 = pImage[((iRow+iLoopRow)*iPixelSize*iWidth) + ((iCol+iLoopCol)*iPixelSize)];
					dTemp += (double) lVal1;
				}
			}

			lSignature[iBlockNum] = sqrt(dTemp);
			iBlockNum++;
		}
	}

	//
	// Normalize
	//
	for(i = 0; i < 256; i++ )
	{
		dTemp += lSignature[i];
	}

	for(i = 0; i < 256; i++ )
	{
		lSignature[i] /= dTemp;
	}


	//
	// There are 256 elements for each image. Compare them using Eucliean Distance
	// The smaller the value the similiar the image visually
	//
	dTemp = 0.0;
	for(i = 0; i < 256; i++ )
	{
		dTemp += (m_KeySignature[i]-lSignature[i])*(m_KeySignature[i]-lSignature[i]);
	}

	//
	dTemp = sqrt(dTemp);

	//
	free(lSignature);

	strTemp.Format("%.7f - %s", dTemp, strNamaFile);
	m_ListFound.AddString(strTemp);

	return dTemp;
}

void CNggolekiGinambaranDlg::OnSelchangeListFounded() 
{
	CString strTemp1;
	CString strTemp2;
	CStringArray strArr;
	CTokenEx t;

	m_ListFound.GetText(m_ListFound.GetCurSel(), strTemp1);
	t.Split(strTemp1, " ", strArr);

	m_imgFound.Load(strArr[2], CXIMAGE_FORMAT_JPG);
	RefreshDisplay();
	
}

⌨️ 快捷键说明

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