IVY DOM


 

人生就像一副复杂拼图
每个人总有属於自己的记忆碎片
优质美国空间-老薛主机|IVY DOM|Flowline|

联系我

RSS




五月 12, 2012

[iwalk]用VC++写AI识别图片

//用VC++写AI识别图片

//填色
//遇到符合色就true
//不符合就false
//记录所有符合点

AI.CPP

// AI.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

//user's definition
#define PIXEL_FOUND 0x00000001
//Api definition:No more Pixel in a Pixel Array
#define PIXEL_NULL 0x00000000
//API definition:No more PixelPt in a PixelPt Array
#define PIXELPT_NULL 0x00000000

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                                // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];                                // The title bar text

// Foward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR     lpCmdLine,
int       nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_AI, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_AI);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style            = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = (WNDPROC)WndProc;
wcex.cbClsExtra        = 0;
wcex.cbWndExtra        = 0;
wcex.hInstance        = hInstance;
wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_AI);
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName    = (LPCSTR)IDC_AI;
wcex.lpszClassName    = szWindowClass;
wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

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;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND    - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY    - post a quit message and return
//
//

struct RGB
{
float r;
float g;
float b;
};

struct HSL
{
float h;
float s;
float l;
};

struct Pixel
{
POINT pt;
RGB rgbColor;
HSL hslColor;
COLORREF val;
DWORD UsrFlag;
DWORD SysFlag;
};

struct BitMap
{
Pixel** Bmp;
int Width;
int Height;
};

struct PixelPt
{
int x;
int y;
DWORD SysFlag;
DWORD UsrFlag;
};

struct Shape
{
Pixel* pt;
int ptCount;
};

inline float Max(float a,float b)
{
if(a>=b)
return a;
else
return b;
}

inline float Min(float a,float b)
{
if(a<=b)
return a;
else
return b;
}

typedef bool (__stdcall* PIXELPROC)(BitMap& Bmp,int x,int y,PVOID Parameter1,PVOID Parameter2);

inline void RGBtoHSL(RGB& rgb,HSL& hsl)
{
float h,s,l;
float r,g,b;

r=rgb.r;
g=rgb.g;
b=rgb.b;

r/=255;
g/=255;
b/=255;

float max=Max(Max(r, g), b);
float min=Min(Min(r, g), b);

l=(max+min)/2;
if(max==min)
{
h=0;
s=0;
}
else
{
float d=max-min;
if(l<0.5)
{
s=d/(max+min);
}
else
{
s=d/(2-(max+min));
}
if(max==r)
{
h=(g-b)/d;
}
else if(max==g)
{
h=2+(b-r)/d;
}
else if(max==b)
{
h=4+(r-g)/d;
}
h*=0.166667f;
if(h<0)
{
h+=1;
}
}
hsl.h=h*240;
hsl.s=s*240;
hsl.l=l*240;
}

struct Rect
{
int x1;
int y1;
int x2;
int y2;
};

Rect face;

bool CALLBACK PixelProc(BitMap& Bmp,int x,int y,PVOID Parameter1,PVOID Parameter2)
{
static bool first=true;
Pixel** BmpTmp=Bmp.Bmp;
Pixel* Current;
Pixel* Source;
PixelPt* ScrPt=(PixelPt*)Parameter2;
Current=&BmpTmp[x][y];
Source=&BmpTmp[ScrPt->x][ScrPt->y];
if(    abs(Current->hslColor.l - Source->hslColor.l)<5
&& abs(Current->hslColor.h - Source->hslColor.h)<5
&& abs(Current->hslColor.s - Source->hslColor.s)<5
&& Current->UsrFlag!=PIXEL_FOUND)
{
if(first)
{
face.x1=face.x2=x;
face.y1=face.y2=y;
first=false;
}
else
{
if(x<face.x1)
face.x1=x;
if(y<face.y1)
face.y1=y;
if(x>face.x2)
face.x2=x;
if(y>face.y2)
face.y2=y;
}
Current->UsrFlag=PIXEL_FOUND;
return true;
}
else
{
return false;
}
}

int DyePixel(BitMap& Dest,PixelPt* Old,PixelPt* New,PIXELPROC PixelProc)
{
int count=0;
int newCount=0;
int x,y;
int Width=Dest.Width;
int Height=Dest.Height;
PixelPt pt;
pt=Old[count];
while(pt.SysFlag!=PIXELPT_NULL)
{
x=pt.x;
y=pt.y;
if(x>0 && y>=0 && x<=Width && y<Height && (*PixelProc)(Dest,pt.x-1,pt.y,Old,&pt))
{
New[newCount]=pt;
New[newCount].x--;
newCount++;
}
if(x>=0 && y>0 && x<Width && y<=Height && (*PixelProc)(Dest,pt.x,pt.y-1,Old,&pt))
{
New[newCount]=pt;
New[newCount].y--;
newCount++;
}
if(x>=0 && y>=0 && x<Width-1 && y<Height && (*PixelProc)(Dest,pt.x+1,pt.y,Old,&pt))
{
New[newCount]=pt;
New[newCount].x++;
newCount++;
}
if(x>=0 && y>=0 && x<Width && y<Height-1 && (*PixelProc)(Dest,pt.x,pt.y+1,Old,&pt))
{
New[newCount]=pt;
New[newCount].y++;
newCount++;
}
count++;
pt=Old[count];
}
New[newCount].SysFlag=PIXELPT_NULL;
return newCount;
}

void GetColorBlock(BitMap Bmp,PixelPt& pt,PIXELPROC PixelProc)
{
PixelPt *p1=(PixelPt*)malloc(sizeof(PixelPt)*4000);
PixelPt *p2=(PixelPt*)malloc(sizeof(PixelPt)*4000);
p1[0]=pt;
p1[1].SysFlag=PIXELPT_NULL;
p2[0].SysFlag=PIXELPT_NULL;
int ret=1;
bool Invert=false;
while(ret)
{
if(Invert)
{
ret=DyePixel(Bmp,p2,p1,PixelProc);
free(p2);
PixelPt *p2=(PixelPt*)malloc(sizeof(PixelPt)*4000);
p2[0].SysFlag=PIXELPT_NULL;
}
else
{
ret=DyePixel(Bmp,p1,p2,PixelProc);
free(p1);
PixelPt *p1=(PixelPt*)malloc(sizeof(PixelPt)*4000);
p1[0].SysFlag=PIXELPT_NULL;
}
Invert^=1;
}
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
static HBITMAP hBitmap;
static HDC hBmpDC;
static BITMAP bm;
static Pixel **Bitmap;
static int Width,Height;
int i,j;
Pixel tmp;

switch (message)
{
case WM_CREATE:
hBitmap=(HBITMAP)LoadImage(NULL,"C:aasd.bmp",IMAGE_BITMAP,0,0,
LR_DEFAULTCOLOR|LR_LOADFROMFILE);
GetObject(hBitmap,sizeof(bm),&bm);
Width=bm.bmWidth;
Height=bm.bmHeight;

Bitmap=(Pixel**)new DWORD[Width];
for(i=0;i<Width;i++)
Bitmap[i]=new Pixel[Height];
MoveWindow(hWnd,500,500,Width+20,Height+60,true);
break;
case WM_COMMAND:
wmId    = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
hBitmap=(HBITMAP)LoadImage(NULL,"C:aasd.bmp",IMAGE_BITMAP,0,0,
LR_DEFAULTCOLOR|LR_LOADFROMFILE);
hBmpDC=CreateCompatibleDC(hdc);
SelectObject(hBmpDC,hBitmap);
StretchBlt(hdc,0,0,Width,Height,hBmpDC,0,0,Width,Height,SRCCOPY);

for(i=0;i<Width;i++)
{
for(j=0;j<Height;j++)
{
tmp.pt.x=i;
tmp.pt.y=j;
tmp.val=GetPixel(hBmpDC,i,j);
tmp.rgbColor.r=GetRValue(tmp.val);
tmp.rgbColor.g=GetGValue(tmp.val);
tmp.rgbColor.b=GetBValue(tmp.val);
RGBtoHSL(tmp.rgbColor,tmp.hslColor);
memcpy(&Bitmap[i][j],&tmp,sizeof(Pixel));
}
}
BitMap Bmp;
Bmp.Bmp=Bitmap;
Bmp.Height=Height;
Bmp.Width=Width;
PixelPt pt;
pt.x=236;
pt.y=71;
GetColorBlock(Bmp,pt,(PIXELPROC)PixelProc);

/*
for(i=0;i<Width;i++)
{
for(j=0;j<Height;j++)
{
if(Bitmap[i][j].UsrFlag==PIXEL_FOUND)
SetPixel(hdc,i,j,RGB(255,255,255));
}
}
*/

POINT point[4];
point[0].x=face.x1;
point[0].y=face.y1;
point[1].x=face.x2;
point[1].y=face.y1;
point[2].x=face.x2;
point[2].y=face.y2;
point[3].x=face.x1;
point[3].y=face.y2;

HPEN hPen;
hPen=CreatePen(0,1,RGB(255,0,0));
SelectObject(hdc,hPen);
MoveToEx(hdc,point[0].x,point[0].y,NULL);
LineTo(hdc,point[1].x,point[1].y);
MoveToEx(hdc,point[1].x,point[1].y,NULL);
LineTo(hdc,point[2].x,point[2].y);
MoveToEx(hdc,point[2].x,point[2].y,NULL);
LineTo(hdc,point[3].x,point[3].y);
MoveToEx(hdc,point[3].x,point[3].y,NULL);
LineTo(hdc,point[0].x,point[0].y);

EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}

相关文章

返回
    • ivy
    • 五月 12th, 2012

    全部文件等下次有时间了再发= =

  1. 。。目前对AI没有兴趣。。

    • ivy
    • 五月 13th, 2012

    AI真的是很神奇的东西哟~

  2. Thanks, This helped use the Task Dialogue wihoutt a class library I filled in some of the missing code while I was at it CheersSteven Public Enum TaskDialogIcon Information = UInt16.MaxValue 2 Warning = UInt16.MaxValue [Stop] = UInt16.MaxValue 1 Question = UInt16.MaxValue 3 SecurityWarning = UInt16.MaxValue 5 SecurityError = UInt16.MaxValue 6 SecuritySuccess = UInt16.MaxValue 7 SecurityShield = UInt16.MaxValue 3 SecurityShieldBlue = UInt16.MaxValue 4 SecurityShieldGray = UInt16.MaxValue 8 NoIcon = -0 End Enum Public Enum TaskDialogResult None OK = 1 Cancel = 2 Yes = 6 No = 7 Retry = 4 Close = 8 End Enum

  3. Excellent example; can be used as a tutroial for writing a wide variety of applications where the writer needs to grab the next song as it begins playing. Your straightforward approach should make it easy for anyone with a little knowledge of C++. Thanks!

  4. It’s good to get a fresh way of looikng at it.

  5. Me salif3 postear en ingle9s, perdf3n.Mire1, esutve analizando la aplicacif3n y entiendo mas o menos lo que hace, pero no logro armar el cf3digo fuente compilable. Tengo entendido que eso es cf3digo C compilable y que utilizaste MOSDEF para compilarlo.Puedo ver la declaracif3n del mf3dulo remote, la llamada al la funcif3n desde esa DLL; luego intente9 armar una funcif3n con nombre GetDataPicture y lo que posteaste como contenido de la funcif3n en cuestif3n. Luego puse el resto en la funcif3n main, me fije9 de que estuviese todo correcto e intente9 compilarlo con MOSDEF, pero me dif3 error de sintaxis.Igual todo bien, no se9 nada de C, voy a seguir estudiando e investigando, si me quere9s ayudar joya. :)

  6. Hey not bad would you rent me banner space for a monthly fee??

  7. these kind of articles really help me. so thank you.http://www.hpbrasil.org

  8. amazing peice would you link to my article please?

    • ivy
    • 七月 5th, 2012

    can u explain it clearly?i was not able to understand the words u said.

    • jaguarsy
    • 七月 13th, 2012

    不写注释真是一种不太好的习惯。

  9. nice site pal

  10. hey mate stay WELL away and keep your family AWAY from london 2012 olympics … you helped me out in the past so i thought id give you this tip, when you watch it this summer youll remember this comment……. i dont mind if you delete this comment but you will remember what i said…. god bless you. Peace.

  11. i like your piece mate please post more frequently

  12. I just want to say I’m new to blogs and actually enjoyed this blog site. Almost certainly I’m likely to bookmark your blog . You absolutely come with remarkable posts. Thanks a bunch for sharing your website.

  13. I experimented with taking a look at your web site with my blackberry and the structure doesnt seem to be right. Might want to check it out on WAP as well as it seems most mobile phone layouts are not really working with your web site.

  1. 暂无 Trackback

You must be logged in to post a comment.