算法依赖的环境,动态库包括libtiff3.dll、jpeg62.dll、convert.dll、imgConv.dll。其中前两个从网上下载tiff、jpeg类后可以自动生成。后面两个类是Che同学给的,在此感谢他,我把这些类放到图片里面,如果能够上传就好了。CSDN做的真不够人性,不能添加附件。先试试。貌似没有成功。放到资源里面了大家可以免费下载。
我在MFC的CView类中实现函数的功能调用。
1)头文件
// ImgProcessView.h : CImgProcessView 类的接口
//
#pragma once
#include "ArdpsImg.h"
class CImgProcessDoc;
class CImgProcessView : public CScrollView
{
protected: // 仅从序列化创建
CImgProcessView();
DECLARE_DYNCREATE(CImgProcessView)
// 属性
public:
CImgProcessDoc* GetDocument() const;
// 操作
public:
CArdpsImg *pArdImg;
// 重写
public:
virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual void OnInitialUpdate(); // 构造后第一次调用
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
void OnRealizePal(WPARAM wParam, LPARAM lParam);
// 实现
public:
virtual ~CImgProcessView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// 生成的消息映射函数
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnImgBinary();
public:
afx_msg void OnGrayMean();
public:
afx_msg void OnBinaryOtsu();
public:
afx_msg void OnBinaryMean();
public:
afx_msg void OnGrayMax();
public:
afx_msg void OnGrayStretch();
public:
afx_msg void OnGrayEqual();
public:
afx_msg void OnEliminateDirt();
public:
afx_msg void OnMakeWhite();
public:
afx_msg void OnBinaryGray();
public:
afx_msg void OnAutoBinary();
public:
afx_msg void OnRotateImg();
public:
afx_msg void OnAutoRotate();
public:
afx_msg void OnLightcontAdjust();
public:
afx_msg void OnZoom();
public:
afx_msg void OnOvlaodEqual();
public:
afx_msg void OnCutFrame();
public:
afx_msg void OnAdjustBinaryPos();
public:
afx_msg void OnColorAdjust();
public:
afx_msg void OnMergeImage();
};
#ifndef _DEBUG // ImgProcessView.cpp 中的调试版本
inline CImgProcessDoc* CImgProcessView::GetDocument() const
{ return reinterpret_cast<CImgProcessDoc*>(m_pDocument); }
#endif
2)源文件
// ImgProcessView.cpp : CImgProcessView 类的实现
//
#include "stdafx.h"
#include "ImgProcess.h"
#include "MainFrm.h"
#include "ImgProcessDoc.h"
#include "ImgProcessView.h"
#include "api/dibapi.h"
#include "ArdpsImg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CImgProcessView
IMPLEMENT_DYNCREATE(CImgProcessView, CScrollView)
BEGIN_MESSAGE_MAP(CImgProcessView, CScrollView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CScrollView::OnFilePrintPreview)
ON_COMMAND(ID_IMG_BINARY, &CImgProcessView::OnImgBinary)
ON_COMMAND(ID_GRAY_MEAN, &CImgProcessView::OnGrayMean)
ON_COMMAND(ID_BINARY_OTSU, &CImgProcessView::OnBinaryOtsu)
ON_COMMAND(ID_BINARY_MEAN, &CImgProcessView::OnBinaryMean)
ON_COMMAND(ID_GRAY_MAX, &CImgProcessView::OnGrayMax)
ON_COMMAND(ID_GRAY_STRETCH, &CImgProcessView::OnGrayStretch)
ON_COMMAND(ID_GRAY_EQUAL, &CImgProcessView::OnGrayEqual)
ON_COMMAND(ID_ELIMINATE_DIRT, &CImgProcessView::OnEliminateDirt)
ON_COMMAND(ID_MAKE_WHITE, &CImgProcessView::OnMakeWhite)
ON_COMMAND(ID_BINARY_GRAY, &CImgProcessView::OnBinaryGray)
ON_COMMAND(ID_AUTO_BINARY, &CImgProcessView::OnAutoBinary)
ON_COMMAND(ID_ROTATE_IMG, &CImgProcessView::OnRotateImg)
ON_COMMAND(ID_AUTO_ROTATE, &CImgProcessView::OnAutoRotate)
ON_COMMAND(ID_LightCont_Adjust, &CImgProcessView::OnLightcontAdjust)
ON_COMMAND(ID_ZOOM, &CImgProcessView::OnZoom)
ON_COMMAND(ID_OVLAOD_EQUAL, &CImgProcessView::OnOvlaodEqual)
ON_COMMAND(ID_CUT_FRAME, &CImgProcessView::OnCutFrame)
ON_COMMAND(ID_ADJUST_BINARY, &CImgProcessView::OnAdjustBinaryPos)
ON_COMMAND(ID_COLOR_ADJUST, &CImgProcessView::OnColorAdjust)
ON_COMMAND(ID_MERGE_IMAGE, &CImgProcessView::OnMergeImage)
END_MESSAGE_MAP()
// CImgProcessView 构造/析构
CImgProcessView::CImgProcessView()
{
// TODO: 在此处添加构造代码
pArdImg = NULL;
}
CImgProcessView::~CImgProcessView()
{
if( pArdImg )
delete []pArdImg;
}
BOOL CImgProcessView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CScrollView::PreCreateWindow(cs);
}
// CImgProcessView 绘制
void CImgProcessView::OnDraw(CDC* pDC)
{
CImgProcessDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
if( !pDoc->m_pDib->IsEmpty())
pDoc->m_pDib->Display(pDC,0,0);
// TODO: 在此处为本机数据添加绘制代码
}
void CImgProcessView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
//// TODO: 计算此视图的合计大小
CImgProcessDoc* pDoc = GetDocument();
CSize sizeTotal(pDoc->m_pDib->GetWidth(), pDoc->m_pDib->GetHeight());
SetScrollSizes(MM_TEXT, sizeTotal);
CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
ASSERT_KINDOF(CMainFrame, pAppFrame);
CRect rc;
pAppFrame->GetClientRect(&rc);
if (rc.Width() >= sizeTotal.cx && rc.Height() >= sizeTotal.cy &&
(sizeTotal.cx>0 || sizeTotal.cy>0))
ResizeParentToFit(FALSE);
}
// CImgProcessView 打印
BOOL CImgProcessView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CImgProcessView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}
void CImgProcessView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清除过程
}
// CImgProcessView 诊断
#ifdef _DEBUG
void CImgProcessView::AssertValid() const
{
CScrollView::AssertValid();
}
void CImgProcessView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CImgProcessDoc* CImgProcessView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CImgProcessDoc)));
return (CImgProcessDoc*)m_pDocument;
}
#endif //_DEBUG
// CImgProcessView 消息处理程序
void CImgProcessView::OnImgBinary()
{
// TODO: 在此添加命令处理程序代码
}
void CImgProcessView::OnGrayMean()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc *pDoc = GetDocument();
pDoc->m_pDib->ConvertGray(0);
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
void CImgProcessView::OnGrayMax()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc *pDoc = GetDocument();
pDoc->m_pDib->ConvertGray(1);
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
void CImgProcessView::OnBinaryOtsu()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc *pDoc = GetDocument();
pDoc->m_pDib->ConvertBinary(0, 0);
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
void CImgProcessView::OnBinaryMean()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc *pDoc = GetDocument();
pDoc->m_pDib->ConvertBinary(1, 0);
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
//重新实现调色板
void CImgProcessView::OnRealizePal(WPARAM wParam, LPARAM lParam)
{
ASSERT(wParam != NULL);
CImgProcessDoc* pDoc = GetDocument();
if (pDoc->m_pDib->IsEmpty())
return; // must be a new document
CPalette* pPal = pDoc->m_pDib->GetPalette();
if (pPal != NULL)
{
CWnd* pAppFrame = AfxGetApp()->m_pMainWnd;
CClientDC appDC(pAppFrame);
// All views but one should be a background palette.
// wParam contains a handle to the active view, so the SelectPalette
// bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
if (oldPalette != NULL)
{
UINT nColorsChanged = appDC.RealizePalette();
if (nColorsChanged > 0)
GetDocument()->UpdateAllViews(NULL);
appDC.SelectPalette(oldPalette, TRUE);
}
else
{
TRACE0("\tSelectPalette failed!\n");
}
}
}
void CImgProcessView::OnGrayStretch()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
int nImgWid = pDoc->m_pDib->GetWidth();
int nImgHei = pDoc->m_pDib->GetHeight();
//bool bSuccess = pDoc->m_pDib->GrayStretch(0, nImgWid/4, nImgHei/4, nImgWid*3/4, nImgHei *3 / 4 );
bool bSuccess = pDoc->m_pDib->GrayStretch(1, nImgWid/4, nImgHei/4, nImgWid*3/4, nImgHei *3 / 4 );
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnGrayEqual()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
int nImgWid = pDoc->m_pDib->GetWidth();
int nImgHei = pDoc->m_pDib->GetHeight();
bool bSuccess = pDoc->m_pDib->GrayEqual();
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnEliminateDirt()
{
// TODO: 在此添加命令处理程序代码
//for Rect
CImgProcessDoc* pDoc = GetDocument();
CRect rect(150,150, 800, 800);
bool bSuccess = pDoc->m_pDib->EliminateDirt(0, rect); // for rect
bSuccess = pDoc->m_pDib->EliminateDirt(0, 1200, 1200, 400); // for circle
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnMakeWhite()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
CRect rect(150,150, 800, 800);
bool bSuccess = pDoc->m_pDib->EliminateDirt(1, rect);
bSuccess = pDoc->m_pDib->EliminateDirt(1, 1200, 1200, 400);
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnBinaryGray()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
bool bSuccess = pDoc->m_pDib->SaveGrayDIB();
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnAutoBinary()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
CSize size;
size.cx = 10;
size.cy = 20;
CSize minSize;
minSize.cx = 5;
minSize.cy = 5;
pDoc->m_pDib->SetDirtSize(size, minSize);
int nNum = 0;
//返回矩形框,由界面显示在图像上。
CRect *pRect = pDoc->m_pDib->GetDirtPos(0, &nNum);
//通过界面交互,选定去除的矩形框,用数组表示,该数组和pRect,作为输入
//消除污点.0为算法误标记。
int *nFlag = new int[nNum];
for( int i = 0; i < nNum; i++ )
{
if( i %2 == 0 )
{
nFlag[i] = 0;
}
else
{
nFlag[i] = 1;
}
}
bool bSuccess = pDoc->m_pDib->AutoEliminateDirt(0, pRect, nFlag, nNum);
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
pDoc->UpdateAllViews(NULL);
}
if( nFlag )
delete []nFlag;
}
void CImgProcessView::OnRotateImg()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
bool bSuccess = pDoc->m_pDib->RotateImage(5);
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnAutoRotate()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
RECT rt;
rt.top = 0;
rt.bottom = 0;
rt.left = 0;
rt.right = 0;
bool bSuccess = pDoc->m_pDib->AutoRotatelImage(rt);
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnLightcontAdjust()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
bool bSuccess = pDoc->m_pDib->AjustLightAndContrast(50, -50);
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnZoom()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
bool bSuccess = pDoc->m_pDib->Zoom(0.5, 0.5);
CSize sizeTotal(pDoc->m_pDib->GetWidth(),pDoc->m_pDib->GetHeight());
SetScrollSizes(MM_TEXT,sizeTotal);
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnOvlaodEqual()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
CArdpsImg *mm;
mm = pDoc->m_pDib;
}
void CImgProcessView::OnCutFrame()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
bool bSuccess = pDoc->m_pDib->CutFrame();
OnInitialUpdate();
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnAdjustBinaryPos()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
bool bSuccess = pDoc->m_pDib->AdjustBinPos(2);
OnInitialUpdate();
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnColorAdjust()
{
// TODO: 在此添加命令处理程序代码
CImgProcessDoc* pDoc = GetDocument();
bool bSuccess = pDoc->m_pDib->ColorEqual();
OnInitialUpdate();
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
}
}
void CImgProcessView::OnMergeImage()
{
// TODO: 在此添加命令处理程序代码
CArdpsImg newArd;
BOOL bBmp = TRUE;
char lpszPathName[] = "D:\\工作文档\\program\\样本\\明嘉靖刻本\\0003.jpg";
HDIB hDib = newArd.LoadNonDibHandle(&bBmp, (char*) lpszPathName );
if( !bBmp )
{
newArd.Attach(hDib);
}
CImgProcessDoc* pDoc = GetDocument();
bool bSuccess = pDoc->m_pDib->MergeImage(&newArd, 1500, 1000, 0);
OnInitialUpdate();
if (bSuccess)
{
pDoc->SetModifiedFlag( TRUE );
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
}
}