回复 2楼 newstar10
#include "stdafx.h"
#include "imageWorm.h"
#include "iostream"
#include "imageWormDoc.h"
#include <MATH.H>
#include "time.h"
#include <ctime>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CImageWormDoc
IMPLEMENT_DYNCREATE(CImageWormDoc, CDocument)
BEGIN_MESSAGE_MAP(CImageWormDoc, CDocument)
//{{AFX_MSG_MAP(CImageWormDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
//
DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CImageWormDoc construction/destruction
CImageWormDoc::CImageWormDoc()
{
// TODO: add one-time construction code here
m_Width = 256;
m_Height = 256;
}
CImageWormDoc::~CImageWormDoc()
{
}
BOOL CImageWormDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CImageWormDoc serialization
void CImageWormDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CImageWormDoc diagnostics
#ifdef _DEBUG
void CImageWormDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CImageWormDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CImageWormDoc commands
BOOL CImageWormDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
BeginWaitCursor();
CFile file;
if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite))
{
EndWaitCursor();
return FALSE;
}
if (this->ReadBmp(file) == 0)
{
file.Abort();
EndWaitCursor();
CString strMsg("读取图像时出错!可能是不支持该类型的图像文件!");
MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
return FALSE;
}
EndWaitCursor();
return TRUE;
}
BOOL CImageWormDoc::ReadBmp(CFile &file)
{
file.Read(&this->m_bmfHdr, sizeof(BITMAPFILEHEADER) ); //14);
file.Read(&this->m_bmiHdr.bmiHeader, sizeof(BITMAPINFOHEADER) ); //40);
if(this->m_bmiHdr.bmiHeader.biBitCount != 8)
{
AfxMessageBox("this is not a grey BMP!", MB_OK, NULL);
file.Close();
return false;
}
m_Height = this->m_bmiHdr.bmiHeader.biHeight;
m_Width
= this->m_bmiHdr.bmiHeader.biWidth;
this->m_imageBuffer = new unsigned char[ (m_Height)
* (m_Width) ];
this->m_bmPalette = new unsigned char[256*4];
file.Read(this->m_bmPalette, 256*4);
file.Read(this->m_imageBuffer,
(m_Height) * (m_Width));
file.Close();
//
m_Height = int (m_Height);
//
m_Width
= int (m_Width);
this->m_pixel = new float*[m_Height];
for ( int i=0; i<m_Height; i++ )
{
this->m_pixel[i] = new float[m_Width];
}
this->m_pixelPrevious = new float*[m_Height];
for ( int i=0; i<m_Height; i++ )
{
this->m_pixelPrevious[i] = new float[m_Width];
}
DWORD SingleLineWidth = WIDTHBYTES(m_Width*8);
for ( int i=0; i<m_Height; i++ )
{
for ( int j=0; j<m_Width; j++ )
{
this->m_pixel[i][j] =
this->m_imageBuffer[(m_Height-i-1) * SingleLineWidth + j];
}
}
this->m_pixelPrevious = this->m_pixel;
this->UpdateAllViews(NULL);
return true;
}
void CImageWormDoc::Inverse()
{
for ( int i=0; i<m_Height; i++ )
for ( int j=0; j<m_Width; j++ )
{
m_pixel[i][j] = 256 - m_pixel[i][j];
}
}
void CImageWormDoc::Convolution()
{
float **newPixel;
float tempSum;
int i, j, m, n; //循环变量
// 给newPixel分配空间
newPixel = new float *[m_Height];
for (
i=0; i<m_Height; i++ )
{
newPixel[i] = new float [m_Width];
}
/*
for ( i=0; i<m_Height; i++ )
for ( j=0; j<m_Width; j++ )
m_pixel[i][j] = m_pixel[i][j]/255;
*/
//卷积运算
int retraction = (m_module.size-1) / 2;
for ( i=0; i<m_Height; i++ )
for ( j=0; j<m_Width; j++ )
if ( i<retraction || m_Height-i<=retraction ||
j<retraction || m_Width-j <=retraction )
newPixel[i][j] = m_pixel[i][j];
else {
tempSum = 0;
for ( m=-retraction; m<=retraction; m++ )
for ( n=-retraction; n<=retraction; n++ ) {
tempSum = tempSum +
m_pixel[i+m][j+n] * m_module.module[m+retraction][n+retraction];
}
newPixel[i][j] = tempSum;
}
//归一化
/*
float max = 0, min = 255;
for ( i=0; i<m_Height; i++ )
for ( j=0; j<m_Width; j++ ) {
if ( newPixel[i][j] > max )
max = newPixel[i][j];
if ( newPixel[i][j] < min )
min = newPixel[i][j];
}
for ( i=0; i<m_Height; i++ )
for ( j=0; j<m_Width; j++ )
//
newPixel[i][j] = ( 255 * (newPixel[i][j]-min) / (max - min) );
newPixel[i][j] = newPixel[i][j]/255;
*/
m_pixel = newPixel;
}
void CImageWormDoc::ZeroCrossDetection()
{
float **newPixel;
int
b, c,
d, e, f,
g,
h
;
int i, j; //循环变量
// 给newPixel分配空间
newPixel = new float*[m_Height];
for (
i=0; i<m_Height; i++ )
{
newPixel[i] = new float[m_Width];
}
m_pixelSign = new float*[m_Height];
for (
i=0; i<m_Height; i++ )
{
m_pixelSign[i] = new float[m_Width];
}
int retraction = (m_module.size+1) / 2;
for ( i=0; i<m_Height; i++ )
for ( j=0; j<m_Width; j++ ) {
newPixel[i][j] = 0;
m_pixelSign[i][j] = 0;
if ( i<retraction || m_Height-i<=retraction ||
// 卷积得到的图像边缘
j<retraction || m_Width-j <=retraction )
// retraction行不处理
newPixel[i][j] = 0;
else {
b = SignOf(m_pixel[i-1][j]);
d = SignOf(m_pixel[i][j-1]);
e = SignOf(m_pixel[i][j]);
f = SignOf(m_pixel[i][j+1]);
h = SignOf(m_pixel[i+1][j]);
if ( e==0 ) {
// Predicate A, B, C, D
if ( d>0 && f<0) {
// Predicate A
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
// End of the A
else if ( d<0 && f>0 ) {
// Predicate B
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
// End of the B
else if ( b>0 && h<0 ) {
// Predicate C
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
// End of the C
else if ( b<0 && h>0 ) {
// Predicate D
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
// End of the D
}
// End of the Predicate A, B, C, D
else {
//
Predicate E, F, G, H, I, K
/////////////////////////////////////////////////////////////////////////////////////
if ( d<0 && e>0 && f>0 ) {
// Predicate E
if ( b>0 && h<0 ) {
// E.1
if ( -m_pixel[i+1][j] < m_pixel[i][j] )
// E.1.1: |h|<e
{
newPixel[i+1][j] = 255;
//
==> h = 0
m_pixelSign[i][j] = 255;
}
else if ( -m_pixel[i][j-1] < m_pixel[i][j] ) // E.1.2: |d|<e
{
newPixel[i][j-1] = 255;
//
==> d = 0
m_pixelSign[i][j] = 255;
}
else
{
newPixel[i][j] = 255;
// E.1.3: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the E.1
else if ( b<0 && h>0 ) {
// E.2
if ( -m_pixel[i-1][j] < m_pixel[i][j] )
// E.2.1: |b|<e
{
newPixel[i-1][j] = 255;
//
==>
b = 0
m_pixelSign[i][j] = 255;
}
else if ( -m_pixel[i][j-1] < m_pixel[i][j] ) // E.2.2: |d|<e
{
newPixel[i][j-1] = 255;
//
==> d = 0
m_pixelSign[i][j] = 255;
}
else
{
newPixel[i][j] = 255;
// E.2.3: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the E.2
else if ( b>0 && h>0 ) {
// E.3
if ( -m_pixel[i][j-1] < m_pixel[i][j] )
// E.3.1: |d|<e
{
newPixel[i][j-1] = 255;
//
==> d = 0
m_pixelSign[i][j] = 255;
}
else
{
newPixel[i][j] = 255;
// E.2.3: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the E.3
else
{
newPixel[i][j] = 255;
// E.otherwise: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the E
///////////////////////////////////////////////////////////////////////////////
else if ( d<0 && e>0 && f<0 ) {
// Predicate F
if ( b!=-1 && h<0 ) {
// F.1
if ( b>0 && -m_pixel[i+1][j] < m_pixel[i][j] ) // F.1.1: b>0 && |h|<e
{
newPixel[i+1][j] = 255;
//
==> h = 0
m_pixelSign[i][j] = 255;
}
else
{
newPixel[i][j] = 255;
// F.1.2: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the F.1
else if ( b<0 && h!=-1 )
// F.2
{
newPixel[i][j] = 255;
// End of the F.2
==> e = 0
m_pixelSign[i][j] = 255;
}
else if ( -m_pixel[i][j-1] < m_pixel[i][j] )
// F.3
|d|<e
{
newPixel[i][j-1] = 255;
// End of the F.3
==> d = 0
m_pixelSign[i][j] = 255;
}
else
{
newPixel[i][j] = 255;
// F.otherwise: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the F
///////////////////////////////////////////////////////////////////////////////
else if ( b<0 && e>0 && h>0 ) {
// Predicate G
if ( d>0 && -m_pixel[i-1][j] < m_pixel[i][j] )
// G.1: |b|<e && d = 1
{
newPixel[i-1][j] = 255;
// End of the G.1
==>
b = 0
m_pixelSign[i][j] = 255;
}
else if ( d>0 && f<0 ) {
// G.2
if ( abs(m_pixel[i][j+1]) < m_pixel[i][j] )
// G.2.1: |f| < e
{
newPixel[i][j+1] = 255;
//
==>
f = 0;
m_pixelSign[i][j] = 255;
}
else if ( abs(m_pixel[i][j+1]) > m_pixel[i][j] )// G.2.2: |f| > e
{
newPixel[i][j] = 255;
//
==>
e = 0;
m_pixelSign[i][j] = 255;
}
}
// End of the G.2
else
{
newPixel[i][j] = 255;
// G.otherwise: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the G
////////////////////////////////////////////////////////////////////////////////
else if ( b<0 && e>0 && h<0 ) {
// Predicate H
g = SignOf(m_pixel[i+1][j-1]);
if ( f<0 && g!=-1 )
// H.1:
{
newPixel[i+1][j] = 255;
//
==> h = 0
m_pixelSign[i][j] = 255;
}
else
{
newPixel[i][j] = 255;
// H.otherwise: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the H
/////////////////////////////////////////////////////////////////////////////
else if ( e>0 && f<0 && h>0 ) {
// Predicate I
if ( -m_pixel[i][j+1] < m_pixel[i][j] )
// I.1: |f| < e
{
newPixel[i][j+1] = 255;
// ==>
f = 0;
m_pixelSign[i][j] = 255;
}
else
{
newPixel[i][j] = 255;
// I.otherwise: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the I
/////////////////////////////////////////////////////////////////////////////
else if ( e>0 && f<0 && h<0 ) {
// Predicate J
g = SignOf(m_pixel[i+1][j-1]);
c = SignOf(m_pixel[i-1][j+1]);
if ( -m_pixel[i+1][j] < m_pixel[i][j] )
// J.1: |h|<e
{
newPixel[i+1][j] = 255;
//
==> h = 0
m_pixelSign[i][j] = 255;
}
else if ( c>0 && -m_pixel[i][j+1] < m_pixel[i][j] )
// J.2: c>0 && |f| < e
{
newPixel[i][j+1] = 255;
// ==>
f = 0;
m_pixelSign[i][j] = 255;
}
else if ( c<0 && -m_pixel[i][j+1] < m_pixel[i][j] )
// J.3: c<0 && |f| < e
{
newPixel[i][j] = 255;
//
m_pixelSign[i][j] = 255;
}
else if ( g>0 )
// J.4: g>0
{
newPixel[i+1][j] = 255;
//
==> h = 0
m_pixelSign[i][j] = 255;
}
else
{
newPixel[i][j] = 255;
// I.otherwise: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the J
////////////////////////////////////////////////////////////////////////////////
else if ( b>0 && d>0 && e>0 && f>0 && h<0 ) { // Predicate K
if ( -m_pixel[i+1][j] < m_pixel[i][j] )
// K.1: |h|<e
{
newPixel[i+1][j] = 255;
//
==> h = 0
m_pixelSign[i][j] = 255;
}
else
{
newPixel[i][j] = 255;
// K.otherwise: e = 0
m_pixelSign[i][j] = 255;
}
}
// End of the K
//////////////////////////////////////////////////////////////////////////////////
}
// End of the Predicate E, F, G, H, I, K
}
}// End of the i_j_for
m_pixelPrevious = m_pixel;
m_pixel = newPixel;
//
m_pixel = m_pixelSign;
}
void CImageWormDoc::ZeroCrossDetectionOfMatlab()
{
float **newPixel;
int
b,
d, e, f,
h
;
int i, j; //循环变量
// 给newPixel分配空间
newPixel = new float*[m_Height];
for (
i=0; i<m_Height; i++ )
{
newPixel[i] = new float[m_Width];
}
m_pixelSign = new float*[m_Height];
for (
i=0; i<m_Height; i++ )
{
m_pixelSign[i] = new float[m_Width];
}
int retraction = (m_module.size+1) / 2;
float threshold;
/*
float sum = 0;
for ( i=retraction; i<m_Height-retraction; i++ )
for ( j=retraction; j<m_Width-retraction; j++ )
sum = sum + abs( newPixel[i][j] );
*/
threshold = 0.1; // 0.75 * sum/(m_Height*m_Width);
for ( i=0; i<m_Height; i++ )
for ( j=0; j<m_Width; j++ )
{
m_pixelSign[i][j] = 0;
newPixel[i][j] = 0;
if ( i<retraction || m_Height-i<=retraction ||
// 卷积得到的图像边缘
j<retraction || m_Width-j <=retraction )
// retraction行不处理
newPixel[i][j] = 0;
else {
b = SignOf(m_pixel[i-1][j]);
d = SignOf(m_pixel[i][j-1]);
e = SignOf(m_pixel[i][j]);
f = SignOf(m_pixel[i][j+1]);
h = SignOf(m_pixel[i+1][j]);
if ( e==0 ) {
// 当前点是零值
if ( b<0 && h>0 && abs(m_pixel[i-1][j]-m_pixel[i+1][j]) > 2*threshold )
// [- 0 +]'
{
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
if ( b>0 && h<0 && abs(m_pixel[i-1][j]-m_pixel[i+1][j]) > 2*threshold )
// [+ 0 -]'
{
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
if ( d<0 && f>0 && abs(m_pixel[i][j-1]-m_pixel[i][j+1]) > 2*threshold )
// [- 0 +]
{
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
if ( d>0 && f<0 && abs(m_pixel[i][j-1]-m_pixel[i][j+1]) > 2*threshold )
// [+ 0 -]
{
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
}
// End of the e==0
else if ( e<0 ) {
//
if ( f>0 && abs(m_pixel[i][j+1]-m_pixel[i][j]) > threshold ) // [- +]
{
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
if ( d>0 && abs(m_pixel[i][j-1]-m_pixel[i][j]) > threshold ) // [+ -]
{
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
if ( h>0 && abs(m_pixel[i+1][j]-m_pixel[i][j]) > threshold ) // [- +]'
{
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
if ( b>0 && abs(m_pixel[i-1][j]-m_pixel[i][j]) > threshold ) // [+ -]'
{
newPixel[i][j] = 255;
m_pixelSign[i][j] = 255;
}
}
// End of the e<0
}
}
// 当前点处理完
m_pixelPrevious = m_pixel;
m_pixel = newPixel;
//
m_pixel = m_pixelSign;
}
void CImageWormDoc::DownResolution()
{
float **newPixel;
int i, j;
//循环变量
// 给newPixel分配空间
// 这里假设输入图像的宽和高都是2的倍数
newPixel = new float*[m_Height/2];
for ( i=0; i<m_Height/2; i++ )
{
newPixel[i] = new float[m_Width/2];
}
// 将输入图像进行降分辨率处理
for ( i=0; i<m_Height/2; i++ )
for ( j=0; j<m_Width/2; j++ ){
newPixel[i][j] = (m_pixel[2*i][2*j]
+
m_pixel[2*i+1][2*j] +
m_pixel[2*i][2*j+1] +
m_pixel[2*i+1][2*j+1]) / 4;
}
m_bmiHdr.bmiHeader.biHeight = m_bmiHdr.bmiHeader.biHeight / 2;
m_bmiHdr.bmiHeader.biWidth
= m_bmiHdr.bmiHeader.biWidth / 2;
m_Height = m_bmiHdr.bmiHeader.biHeight;
m_Width
= m_bmiHdr.bmiHeader.biWidth;
m_pixel = newPixel;
}
void CImageWormDoc::SubpixelAccuracyFitting()
{
//time_t start=clock();
clock_t start,end;
start=clock();
float **newPixel;
int i, j;
//循环变量
// 给newPixel分配空间
newPixel = new float*[2*m_Height];
for ( i=0; i<2*m_Height; i++ )
{
newPixel[i] = new float[2*m_Width];
}
for ( i=0; i<2*m_Height; i++ )
for ( j=0; j<2*m_Width; j++ )
newPixel[i][j] = 0;
//
用切比雪夫离散正交多项式(Chevyshev discrete orthogonal polynomial)
//
进行所谓的亚像素精度的配准。 其实际上是把原图升高分辨率即放大,在已
//
测过零点的周围区域进行插值,插值是根据上述多项式来计算的。
for ( i=0; i<m_Height; i++ )
for ( j=0; j<m_Width; j++ )
if ( m_pixel[i][j] ) {
newPixel[2*i][2*j]
= m_pixel[i][j];
newPixel[2*i+1][2*j]
= m_pixel[i][j];
newPixel[2*i][2*j+1]
= m_pixel[i][j];
newPixel[2*i+1][2*j+1]= m_pixel[i][j];
}
/*
if ( m_pixel[i][j]) ==255 ) { // 如果是过零点
newPixel[2*i][2*j]
= 255;
newPixel[2*i+1][2*j]
= 255;
newPixel[2*i][2*j+1]
= 255;
newPixel[2*i+1][2*j+1]= 255;
}
*/
m_bmiHdr.bmiHeader.biHeight = 2 * m_bmiHdr.bmiHeader.biHeight;
m_bmiHdr.bmiHeader.biWidth
= 2 * m_bmiHdr.bmiHeader.biWidth;
m_Height = m_bmiHdr.bmiHeader.biHeight;
m_Width
= m_bmiHdr.bmiHeader.biWidth;
/*
m_pixel = new float*[m_Height];
for ( i=0; i<m_Height; i++ )
{
m_pixel[i] = new float[m_Width];
}
*/
m_pixel = newPixel;
//time_t end=clock();
//double dur = static_cast<double>(end -start)/CLOCKS_PER_SEC * 1000;
//cout<<"\n您的程序执行所耗费的时间为:" << dur << " 毫秒" << endl;
end=clock();
printf("The different is %6.3f\n",(double)(end-start));
}
我按你说的把计算时间的程序加进去,怎么不显示啊?