//编译环境VS2005 选WIN32应用程序, 我从来不用MFC
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的连云港网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
#include GL/GL.h
#include gl/GLU.h
#include gl/GLAux.h
#include math.h
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
#define MAX_LOADSTRING 100
//定义递归调用阀值,即其中最小三角形边长
#define SIDELENGHT (0.05)
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
//自定义全局变量
HGLRC g_hRC = NULL; //OpenGL 绘图上下文
HDC g_hDC = NULL; //设备上下文
HWND g_hWnd = NULL; //保存当前窗口句柄
//定义三角形顶点指针
typedef struct tagTRIANGLEPOINT
{
GLfloat tpPointX;
GLfloat tpPointY;
}TRIANGLEPOINT,*LTRIANGLEPOINT;
//定义最外面大三角形三个点
TRIANGLEPOINT g_TrianglePoint[3] = {{0.0, 0.0}, {5.0, 0.0}, {2.5,2.5}};
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
//自定义函数
void EnableOpenGL(HWND hWnd);
void SceneInit();
void SceneResizeViewport();
void DisableOpenGL();
void Display();
void DrawTriangle(LTRIANGLEPOINT pTrianglePoint, int iPointNum = 3);
void DrawLargeTriangle(LTRIANGLEPOINT pTrianglePoint, int iPointNum);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_OPENGLTRIANGLE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
EnableOpenGL(g_hWnd);
SceneInit();
SceneResizeViewport();
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_OPENGLTRIANGLE));
// Main message loop:
ZeroMemory(msg, sizeof(msg));
while ( msg.message != WM_QUIT )
{
if (PeekMessage(msg, NULL, 0U, 0U, PM_REMOVE))
{
if(!TranslateAccelerator(msg.hwnd, hAccelTable, msg))
{
TranslateMessage(msg);
DispatchMessage(msg);
}
}
else
{
Display();
SwapBuffers(g_hDC);
}
}
DisableOpenGL();
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are 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;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OPENGLTRIANGLE));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_OPENGLTRIANGLE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, 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;
}
g_hWnd = hWnd;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 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
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, 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...
EndPaint(hWnd, ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
//初始化opengl
void EnableOpenGL(HWND hWnd)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
g_hDC = GetDC(hWnd);
ZeroMemory(pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW| PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat( g_hDC, pfd );
SetPixelFormat( g_hDC, iFormat, pfd );
g_hRC = wglCreateContext(g_hDC);
wglMakeCurrent( g_hDC, g_hRC);
}
//设置着色模式
void SceneInit()
{
glShadeModel(GL_SMOOTH);
glClearColor(1.0, 1.0, 1.0, 0.5);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
//设置视口
void SceneResizeViewport()
{
RECT rect;
int w,h;
GLfloat aspect;
GetClientRect( g_hWnd, rect );
w = rect.right - rect.left;
h =rect.bottom - rect.top;
aspect = (GLfloat)w/ (GLfloat)h;
glViewport(0,0,w,h);//设置视口
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 5.0, 0.0, 5.0/aspect);
}
void DisableOpenGL(){
wglMakeCurrent(NULL, NULL);
wglDeleteContext(g_hRC);
ReleaseDC(g_hWnd, g_hDC);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
DrawLargeTriangle(g_TrianglePoint, 3);
DrawTriangle(g_TrianglePoint, 3);
glFlush();
}
//画大三角形
void DrawLargeTriangle(LTRIANGLEPOINT pTrianglePoint, int iPointNum)
{
glBegin(GL_LINE_LOOP);
for ( int i = 0; i 3; ++i)
{
glColor3f(1.0,0.0,0.0);
glVertex2f(pTrianglePoint[i].tpPointX,pTrianglePoint[i].tpPointY);
}
glEnd();
}
//递归函数
void DrawTriangle(LTRIANGLEPOINT pTrianglePoint, int iPointNum)
{
//得到中间三角形的三个点
TRIANGLEPOINT trianglepoint[3];
for (int i = 0; i 3; ++i)
{
if ( i == 2)
{
trianglepoint[i].tpPointX = (pTrianglePoint[i].tpPointX + pTrianglePoint[0].tpPointX) / 2;
trianglepoint[i].tpPointY = (pTrianglePoint[i].tpPointY + pTrianglePoint[0].tpPointY) / 2;
}
else
{
trianglepoint[i].tpPointX = (pTrianglePoint[i].tpPointX + pTrianglePoint[i+1].tpPointX) / 2;
trianglepoint[i].tpPointY = (pTrianglePoint[i].tpPointY + pTrianglePoint[i+1].tpPointY) / 2;
}
}
glBegin(GL_LINE_LOOP);
for ( int i = 0; i 3; ++i)
{
glColor3f(1.0,0.0,0.0);
glVertex2f(trianglepoint[i].tpPointX,trianglepoint[i].tpPointY);
}
glEnd();
//构建其它三角
TRIANGLEPOINT trianglepoint1[3];
TRIANGLEPOINT trianglepoint2[3];
TRIANGLEPOINT trianglepoint3[3];
ZeroMemory(trianglepoint1, sizeof(trianglepoint1));
ZeroMemory(trianglepoint2, sizeof(trianglepoint2));
ZeroMemory(trianglepoint2, sizeof(trianglepoint2));
trianglepoint1[0] = pTrianglePoint[0];
trianglepoint1[1] = trianglepoint[0];
trianglepoint1[2] = trianglepoint[2];
trianglepoint2[0] = pTrianglePoint[1];
trianglepoint2[1] = trianglepoint[1];
trianglepoint2[2] = trianglepoint[0];
trianglepoint3[0] = pTrianglePoint[2];
trianglepoint3[1] = trianglepoint[2];
trianglepoint3[2] = trianglepoint[1];
//设定边界
if(pow(abs(trianglepoint1[0].tpPointX - trianglepoint1[1].tpPointX), 2)
+pow(abs(trianglepoint1[0].tpPointY - trianglepoint1[1].tpPointY), 2) SIDELENGHT)
DrawTriangle(trianglepoint1, iPointNum);
if(pow(trianglepoint2[0].tpPointX - trianglepoint2[1].tpPointX, 2)
+pow(trianglepoint2[0].tpPointY - trianglepoint2[1].tpPointY, 2) SIDELENGHT)
DrawTriangle(trianglepoint2, iPointNum);
if(pow(trianglepoint2[0].tpPointX - trianglepoint2[1].tpPointX, 2)
+pow(trianglepoint2[0].tpPointY - trianglepoint2[1].tpPointY, 2) SIDELENGHT)
DrawTriangle(trianglepoint3, iPointNum);
python阀值组大于返回结果小于返回结果啥意思简单阈值
对每一个像素都应用相同的阈值。如果像素值小于阈值,则将其设置为0,否则设置为最大值。
函数:
retval, dst=cv.threshold(src, thresh, maxval, type[, dst])对每个像素使用固定的阈值
参数:
src
输入图像 ,灰度图(多通道, 8-bit or 32-bit floating point).
dst
与src具有相同大小、类型和通道数的输出数组。
thresh
阈值
maxval
当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
type
返回值:
retVal:使用的阈值,在Otsu‘s中会用到
dst: 经过阈值处理的图像