具体内容:对彩色图像进行补色变换。
具体实现:在Win32控制台下创建1个新窗口,然后在窗口上添加5个按钮,分别对应“打开图像”,“二值化处理”,”对数变换“,”伽马变换“,”补色变换“这5个功能。
具体代码:
// Test.cpp : 定义控制台应用程序的入口点。
//
#pragma comment(lib,"user32")
#pragma comment(lib,"gbi32")
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#define MAX_STR 100
#define NUM 5
//全局变量
HINSTANCE hInst;//当前实例
TCHAR szTitle[MAX_STR] = _TEXT("Console_Win Demo"); //标题栏文本
TCHAR szWindowClass[MAX_STR] = _TEXT("Console_Win Demo");//主窗口类名
TCHAR* btnName[NUM]={_T("打开图像"),_T("二值化处理"),_T("对数变换"),_T("伽马变换"),_T("补色变换")};
//此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void handleImage(IplImage* src, IplImage* dst, int threshold);
void handleLogImage(IplImage* src, IplImage* dst);
int logfun(int color);
void handleGamaImage(IplImage* src, IplImage* dst);
int gama(int color);
void handleBuseImage(IplImage* src, IplImage* dst);
void buSe(CvScalar &px);
HWND hwndBtn[NUM];
HWND hWnd;
char str[255]="";//保存输出的字符串
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Console_Win Demo" << std::endl << std::endl;
std::cout << "================================" << std::endl << std::endl;
HINSTANCE hInstance = NULL;
int nCmdShow = SW_SHOW; // 该变量取值参见MSDN
hInstance = GetModuleHandle(NULL);
std::cout << "hInstance: " << hInstance << std::endl;
std::cout << "hInstance->unused: " << hInstance->unused << std::endl << std::endl;
std::cout << "================================" << std::endl << std::endl;
MSG msg;
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
std::cout << "Error in InitInstance()!" << std::endl;
return FALSE;
}
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.style &=~CS_VREDRAW;/*新添加*/
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
//wcex.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
//wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
//wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL;
//wcex.hIconSm =LoadIcon(NULL,IDI_INFORMATION);
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HANDLE, int)
//
// 目的: 保存实例句柄并创建主窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
HDC hdc;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, unsigned, WORD, LONG)
//
// 目的: 处理主窗口的消息。
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
IplImage *src;
src=cvLoadImage("1.jpg",1);//原图
IplImage *dst=cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
switch (message)
{
case WM_CREATE://这里创建一个按钮,这里没有用到ID_BUTTON绑定
for(int i=0;iheight;i++){
for(int j=0;jwidth;j++){
CvScalar s = cvGet2D(src, i, j);
if(s.val[0]>=threshold){
s.val[0] = 255;
}else{
s.val[0] = 0;
}
cvSet2D(dst, i, j, s);
}
}
}
void handleLogImage(IplImage* src, IplImage* dst){
for(int i=0; iheight;i++){
for(int j=0;jwidth;j++){
CvScalar s = cvGet2D(src, i, j);
s.val[0] = logfun(s.val[0]);
cvSet2D(dst, i, j, s);
}
}
}
//s = c * log(1+r*v)/log(v)
int logfun(int color){
double c = 1.0;
double v = 5.0;
double r, s;
int d;
r = color/255.0;
s = c*log(1+r*v)/log(v+1);
d = (int)(s*255);
if(d<0){
d=0;
}else if(d>255){
d=255;
}
return d;
}
void handleGamaImage(IplImage* src, IplImage* dst){
for(int i=0; iheight;i++){
for(int j=0;jwidth;j++){
CvScalar s = cvGet2D(src, i, j);
s.val[0] = gama(s.val[0]);
cvSet2D(dst, i, j, s);
}
}
}
int gama(int color){
double c = 1.0;
double gama = 0.4;
double r = color/255.0;
double s = c*pow(r, gama);
int d;
d = (int)(255*s);
if(d<0){
d=0;
}else if(d>255){
d=255;
}
return d;
}
void handleBuseImage(IplImage* src, IplImage* dst){
for(int i=0; iheight;i++){
for(int j=0;jwidth;j++){
CvScalar s = cvGet2D(src, i, j);
buSe(s);
cvSet2D(dst, i, j, s);
}
}
}
void buSe(CvScalar &px){
px.val[0] = 255-px.val[0];
px.val[1] = 255-px.val[1];
px.val[2] = 255-px.val[2];
}