码迷,mamicode.com
首页 > 编程语言 > 详细

图像处理 基于Visual C++编程 学习笔记 持续更新中。。。

时间:2015-04-28 13:58:36      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

2015-4-26

新建一个工程,安装MSDN文档

File -new - win32application- a simple win32 app

Dos操作系统是 16位操作系统 2^16=65535 ,内存为16k

win32操作系统(window95以后的系统) 32位 2^32 内存约为4G

进入后可以试着编译运行这样一段话

 技术分享

tip:选中MessageBox 按F1可以看到MSDN的相关文档, 选中MB_OK 按F12可以看到它的宏定义

int MessageBox(
  HWND hWnd,          // handle to owner window
  LPCTSTR lpText,     // text in message box
  LPCTSTR lpCaption,  // message box title
  UINT uType          // message box style
);
uint uType 定义了按钮图标的方式,可以位组合,详见MSDN文档
HWND hWnd 是定义消息框的父窗口 可以用FindWindow来找需要附加的父窗口

messagebox有返回值,可以通过返回值来判断用户点了哪个按钮
    int entry=MessageBox(NULL,"A new win32 app","hello~",MB_YESNOCANCEL);//提示内容,提示
    if(entry==IDYES)
    {
        MessageBox(NULL,"YES","你点击了:",MB_OK);
    }
    else if(entry==IDNO)
    {
        MessageBox(NULL,"NO","你点击了:",MB_OK);
    }
    else
    {
        MessageBox(NULL,"CANCEL","你点击了:",MB_OK);
    }  

 

进入目录后可以看到以下文件:

技术分享

.cpp源代码文件

.dsp vc开发环境生成的工程文件,文本格式

.dsw vc开发环境生成的项目文件,用来把多个工程组织到一个项目中,文本格式

.dat data的简写,用来保存工程中的数据

.ncb “No Compile Browser”的缩写,其中存放了供ClassView、WizardBar和Component Gallery使用的信息

.plg  是编译信息文件,编译时的error和warning信息文件(实际上是一个html文件)

 

进入debug后有

技术分享

.ilk 连接过程中生成的一种中间文件,只供LINK工具使用

.obj 是目标文件,一般是程序编译后的二进制文件,在通过链接器和资源文件链接就成exe文件了。OBJ只给出了程序的相对地址,而EXE是绝对地址。

.pch 是预编译头文件的后缀名,可以加快编译速度

.idb 一种 MSDev 中间层文件

.pdb 一种 3Com PalmPilot 数据库文件

.pbd 一种 PowerBuilder 动态库,作为本地DLL的一个替代物

 

tip:vc6.0文件打开奔溃的问题见 

http://blog.csdn.net/makenothing/article/details/8677682

http://blog.csdn.net/beyond_cn/article/details/20305877

 

2015-4-27

在现有工程上添加资源脚本文件(图标、光标、对话框、快捷键等)

选中工程- new -File- Resource Script

在刚才建的资源脚本文件中 插入图标icon等

如果有多个图标,ID最小的图标会被作为应用程序的图标

 

开发一个对话框程序

在刚才建的资源脚本文件中 插入对话框Dialog

设置对话框字体:

选中对话框-右键选择property- 选择字体 如微软雅黑 10 , 带@的是竖直方向的字体,不带@的是水平方向的字体

 

在主函数中调用对话框程序

DialogBox

INT_PTR DialogBox(
  HINSTANCE hInstance,  // handle to module
  LPCTSTR lpTemplate,   // dialog box template
  HWND hWndParent,      // handle to owner window
  DLGPROC lpDialogFunc  // dialog box procedure
);
第一个参数是对话框的句柄,有了句柄就可以对对话框执行任何操作
第二个参数是指定对话框ID,要包含resouse.h 使得对话框ID可见
第三个参数的父窗口ID,没有为NULL
第四个参数的指定消息回调函数的地址,消息回调函数是用来截获输入的信息
消息=消息ID+附带数据

 回调函数:

INT_PTR CALLBACK DialogProc(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
);

WM_COMMAND

This message is sent when the user selects a command item from a menu, when a control sends a notification message to its parent window, or when an accelerator keystroke is translated.

WM_COMMAND wNotifyCode = HIWORD(wParam); 
wID = LOWORD(wParam); 
hwndCtl = (HWND) lParam;

wParam和lParam都是四个字节的变量
lParam记录点击按钮后产生的句柄
wParam 分为HIWORD和LOWWORD两部分,
高两位记录按下快捷键时产生的消息(记为1),或者是菜单上的按钮(记为0)
低两位记录按钮、菜单项、快捷键的编号

 

EndDialog

The EndDialog function destroys a modal dialog box, causing the system to end any processing for the dialog box.

BOOL EndDialog(
  HWND hDlg,        // handle to dialog box
  INT_PTR nResult   // value to return
);

WM_COMMAND==uMsg表示该消息是命令消息,然后再进一步进行区分

#include "stdafx.h"
#include "resource.h"
BOOL CALLBACK MainProc(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
)
{
    if(WM_COMMAND==uMsg)
    {
        if(LOWORD(wParam)==IDCANCEL)
        {
            EndDialog(hwndDlg,IDCANCEL);
        }
    }
    return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    DialogBox(hInstance,(LPCSTR)IDD_DIALOG1,NULL,MainProc);
    return 0;
}

 tip:如果控件栏不见了, 在vc6的窗口上右键->controls

 

读取对话框数据,输出数据到对话框

GetDlgItemInt

This function translates the text of a specified control in a dialog box into an integer value.

UINT GetDlgItemInt( 
HWND hDlg,           指定对话框句柄
int nIDDlgItem,      指定需要转换的文本框ID
BOOL *lpTranslated,  
BOOL bSigned);       输入是否要转为正数

SetDlgItemInt

This function sets the text of a control in a dialog box to the string representation of a specified integer value.

BOOL SetDlgItemInt( 
HWND hDlg, 
int nIDDlgItem, 
UINT uValue,    要输出的值
BOOL bSigned);  输出是否要转问正数

 tip:IDC是 控件ID,control ,  IDD是对话框ID, dialog

window的变量类型

1.简单重定义。所有L开头的变量是window对c语言变量的简单重定义

BOOL, BYTE, INT, UINT, LPARAM

2.句柄类型。所有H开头的变量是window的句柄变量

 HWND, HINSTANCE, HICON

3.结构体类型。

POINT, SIZE, RECT

    if(WM_COMMAND==uMsg)
    {
        if(LOWORD(wParam)==IDCANCEL)
        {
            EndDialog(hwndDlg,IDCANCEL);
            return TRUE;
        }
        if(LOWORD(wParam)==IDOK)
        {
            int left=GetDlgItemInt(hwndDlg,IDC_LEFT,NULL,TRUE);
            int right=GetDlgItemInt(hwndDlg,IDC_RIGHT,NULL,TRUE);
            int result=left+right;
            SetDlgItemInt(hwndDlg,IDC_RESULT,result,TRUE);
        }
    }
    

 

 ----------------------------------------------华丽的分割线--------------------------------------------------------

建立MFC窗体程序

http://wenku.baidu.com/view/2c1dcf533c1ec5da50e27018.html

http://www.cnblogs.com/moondark/p/4147187.html

添加菜单栏

选择工程-插入-资源-menu 然后在窗体属性中选择这个新建的menu

 动态添加菜单栏http://www.cnblogs.com/mx113/archive/2009/12/05/1617678.html

选中menu- 右键-建立类向导 http://blog.sina.com.cn/s/blog_69e905cd0100kghw.html

 

2015-4-28

 

打开bmp文件:

 

void CBMPALGORITHMDlg::OnMenuopen() 
{
    // TODO: Add your command handler code here
    char szFilter[]="BMP Files (*.bmp)|*.bmp|All Files(*.*)|*.*||";
    CFileDialog dlg( TRUE,"BMP",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter ); 
    if(dlg.DoModal() == IDOK) 
    { 
        CString strPathName = dlg.GetPathName();   
        SetDlgItemText(IDC_OUTPUT,strPathName);//输出路径
        MessageBox("ok","hi~",MB_OK);
    } 
}

 文件过滤:

char szFilter[]="BMP Files (*.bmp)|*.bmp|All Files(*.*)|*.*||";

CFileDialog 

CFileDialog( 
BOOL bOpenFileDialog,
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL);

输出bmp文件信息,显示bmp图像

void CBMPALGORITHMDlg::OnMenuopen() 
{
    CDC *pDC = m_image1.GetDC();
    CRect myRECT;
    m_image1.GetClientRect(&myRECT);
    // TODO: Add your command handler code here
    char szFilter[]="BMP Files (*.bmp)|*.bmp|All Files(*.*)|*.*||";
    CFileDialog dlg( TRUE,"BMP",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter ); 
    if(dlg.DoModal() == IDOK) 
    { 
        CString strPathName = dlg.GetPathName();   
        imgbasic im(strPathName.GetBuffer(0));
        SetDlgItemText(IDC_OUTPUT,im.getInfo());//输出路径
        im.imshow(pDC,myRECT);
       // MessageBox("ok","hi~",MB_OK);
    } 
    m_image1.ReleaseDC(pDC);
}

CString中GetBuffer方法可以将CString转为char *

 GetBuffer(0)表示从头开始

imgbasic::imgbasic(char *fn)
{
    strcpy(filepath,fn);
    FILE *mybmp;
    if(mybmp=fopen(filepath,"rb"))//以二进制形式读取
    {
    //    fread(&MyHeader,10,1,mybmp);//位图头文件是14个字节,此处用结构体读入有问题
        fread(&MyHeader.B,1,1,mybmp);
        fread(&MyHeader.M,1,1,mybmp);
        fread(&MyHeader.FileSize,1,4,mybmp);
        fread(&MyHeader.Reserved1,1,4,mybmp);
        fread(&MyHeader.HeaderLength,1,4,mybmp);

        fread(&MyInfo,40,1,mybmp);//位图信息头40个字节
        raw_img=(unsigned char*)malloc(sizeof(unsigned char)*MyInfo.PixelBytes);
        fread(raw_img,MyInfo.PixelBytes,1,mybmp);
        fclose(mybmp);

        //查看raw图像,在ps中设置宽度,高度,通道数量3,隔行,8位
        FILE *f = fopen("t.raw","wb");
        fwrite(raw_img, MyInfo.PixelBytes,1, f);
        fclose(f);
    }
    else{
        AfxMessageBox("CAN‘T OPEN FILE!", MB_OK);
    }
}
void imgbasic::imshow(CDC *pDC, CRect rc)const
{
    SetDIBitsToDevice(                                                //显示图像速度快;
        pDC->m_hDC,
        (rc.Width()-vImageWidth())/2,
        (rc.Height()-vImageHeight())/2,
        vImageWidth(),
        vImageHeight(),
        0,0,0,
        vImageHeight(),
        raw_img,
        (tagBITMAPINFO *)(&MyInfo),
        DIB_RGB_COLORS
    ); 
}
char* imgbasic::getInfo()const
{
    char *info;
    info=(char*)malloc(sizeof(char)*500);
    info[0]=\0;
//    sprintf(info,"12132\r\n");
//    sprintf(info+7,"aaa\r\n");
    //path
    sprintf(info+strlen(info),"---------path---------\r\n");
    sprintf(info+strlen(info),"FilePath:%s\r\n",filepath);
    //BMPHeader
    sprintf(info+strlen(info),"-------BMPHeader-------\r\n");
    sprintf(info+strlen(info),"FileType:%c %c P\r\n",MyHeader.B,MyHeader.M);
    sprintf(info+strlen(info),"FileSize:%ld bytes\r\n",MyHeader.FileSize);
    sprintf(info+strlen(info),"HeaderLength:%ld \r\n",MyHeader.HeaderLength);
    //BMPFileInfo
    sprintf(info+strlen(info),"------BMPFileInfo------\r\n");
    sprintf(info+strlen(info),"InfoSize:%ld\r\n",MyInfo.InfoSize);
    sprintf(info+strlen(info),"ImageWidth:%ld\r\n",MyInfo.ImageWidth);
    sprintf(info+strlen(info),"ImageHeight:%ld\r\n",MyInfo.ImageHeight);
    sprintf(info+strlen(info),"Height*Width:%ld\r\n",MyInfo.ImageHeight*MyInfo.ImageWidth*3);
    sprintf(info+strlen(info),"Level:%ld\r\n",MyInfo.Level);
    sprintf(info+strlen(info),"PixelBytes:%ld\r\n",MyInfo.PixelBytes);
    sprintf(info+strlen(info),"ColorDepth:%ld\r\n",MyInfo.ColorDepth);
    sprintf(info+strlen(info),"PixelBytes:%ld\r\n",MyInfo.PixelBytes);
    sprintf(info+strlen(info),"Horizon:%ld\r\n",MyInfo.horizon);
    sprintf(info+strlen(info),"Vertical:%ld\r\n",MyInfo.vertical);
    sprintf(info+strlen(info),"ColorUsed:%ld\r\n",MyInfo.offset1);//不知为何显示为0
    sprintf(info+strlen(info),"ColorImportant:%ld\r\n",MyInfo.offset2);//不知为何显示为0

    return info;
}

 

 

 

形态学

http://www.cnblogs.com/tornadomeet/archive/2012/03/20/2408086.html

图像处理 基于Visual C++编程 学习笔记 持续更新中。。。

标签:

原文地址:http://www.cnblogs.com/kylehz/p/4458170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!