2017년 9월 22일 금요일

[Win32] 버튼에서 WM_MOUSEMOVE 이벤트 처리하기



#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK BtnProc(HWND, UINT, WPARAM, LPARAM);

WNDPROC _fpBtnProc;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArg, int nCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS WndClass;
WndClass.style = NULL;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = "Hello";

if (!RegisterClass(&WndClass)) return NULL;

hWnd = CreateWindow(
"Hello",
"My",
WS_OVERLAPPEDWINDOW,
0,
0,
320,
240,
NULL, NULL, hInstance,
NULL
);

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

#include <stdio.h>
#include <string.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT mesg, WPARAM wParam, LPARAM lParam)
{
static HWND hBtn;

static char szPos[256];

switch (mesg)
{
case WM_CREATE:
hBtn = CreateWindow("button", "",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 100, 100,
hWnd,
(HMENU)999,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);

_fpBtnProc = (WNDPROC)GetWindowLong(hBtn, GWL_WNDPROC);
SetWindowLong(hBtn, GWL_WNDPROC, (LONG)BtnProc);
break;

case WM_MOUSEMOVE:
HDC hdc;
hdc = GetDC(hWnd);
sprintf_s(szPos, "%04d %04d", LOWORD(lParam), HIWORD(lParam));
TextOut(hdc, 100, 0, szPos, strlen(szPos));
ReleaseDC(hWnd, hdc);
break;

case WM_DESTROY:
PostQuitMessage(0);
return FALSE;
}
return DefWindowProc(hWnd, mesg, wParam, lParam);
}
LRESULT CALLBACK BtnProc(HWND hWnd, UINT mesg, WPARAM wParam, LPARAM lParam)
{
static char szPos[256];

switch (mesg)
{
case WM_MOUSEMOVE:
HDC hdc;
HWND hParent;
hParent = GetParent(hWnd);
hdc = GetDC(hParent);
sprintf_s(szPos, "%04d %04d", LOWORD(lParam), HIWORD(lParam));
TextOut(hdc, 100, 16, szPos, strlen(szPos));
ReleaseDC(hParent, hdc);
break;
}
return CallWindowProc(_fpBtnProc, hWnd, mesg, wParam, lParam);
}

2017년 9월 12일 화요일

[python] file

data.csv
1,2,3,4,5,6,7,8,9,10

[numpy]
import numpy as np
data = np.loadtxt('data.csv', delimiter=',', dtype=np.int32)
print(data)
for i, v in enumerate(data):
  print(i, v)

[pickle]
import pickle
color = [1,2,3,4]
pickle.dump(color, open('colors.txt', 'wb'))
color = pickle.load(open('colors.txt', 'rb'))
print(color)

2017년 9월 11일 월요일

[python] sort, sorted, id, copy

In [34]: x = [5, 7, 3, 38, 7]
    ...: y = sorted(x)

In [35]: x
Out[35]: [5, 7, 3, 38, 7]

In [36]: y
Out[36]: [3, 5, 7, 7, 38]

In [37]: xx = x

In [38]: xx
Out[38]: [5, 7, 3, 38, 7]

In [39]: id(xx)
Out[39]: 1635703548744

In [40]: id(x)
Out[40]: 1635703548744

In [41]: xx = x.copy()

In [42]: xx
Out[42]: [5, 7, 3, 38, 7]

In [43]: id(xx)
Out[43]: 1635703377480

In [44]: x.sort()

In [45]: x
Out[45]: [3, 5, 7, 7, 38]

2017년 9월 9일 토요일

[python] link

http://nullege.com/

--------------------------------------------------------------------------------
last updated: 2017/9/10

[python] MP3출력 pygame

[인스톨]
conda install -c cogsci pygame
https://anaconda.org/cogsci/pygame

[코드]
pygame.mixer.init()
pygame.mixer.music.load("file.mp3")
pygame.mixer.music.play()

[python] OpenCV 필터링 (라이브러리활용)

import numpy as np
import cv2

img = cv2.imread('demo.jpg')
print(img.shape, img.dtype)

cv2.imshow('input', img)
print("press anykey on the image")
cv2.waitKey(0)

height = img.shape[0]
width = img.shape[1]

dst = np.zeros((height, width, 3), np.uint8)
kernel = np.ones((5,5),np.float32)/25

print("filtering...", end='')
dst = cv2.filter2D(img,-1,kernel)
print("done.")    

cv2.imshow('result', dst)
print("press anykey on the image")
cv2.waitKey(0)
cv2.destroyAllWindows()

----------
http://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html

blur = cv2.blur(img,(5,5))
blur = cv2.boxFilter(img, (5,5))
blur = cv2.GaussianBlur(img,(5,5),0)
median = cv2.medianBlur(img,5)
blur = cv2.bilateralFilter(img,9,75,75)

[python] OpenCV 박스필터링 (직접구현)

import numpy as np
import cv2

img = cv2.imread('demo.jpg')
print(img.shape, img.dtype)

cv2.imshow('input', img)
print("press anykey on the image")
cv2.waitKey(0)

height = img.shape[0]
width = img.shape[1]

dst = np.zeros((height, width, 3), np.uint8)
filter = np.ones((5,5))/25

print("converting...", end='')
for h in range(2, height-2):
    for w in range(2, width-2):
        sb = 0.0
        sg = 0.0
        sr = 0.0
        for y in range(-2, 3):
            for x in range(-2, 3):
                sb += img[h+y, w+x, 0]*filter[y+2, x+2]
                sg += img[h+y, w+x, 1]*filter[y+2, x+2]
                sr += img[h+y, w+x, 2]*filter[y+2, x+2]
             
        dst[h, w, 0] = sb.astype(np.uint8)    
        dst[h, w, 1] = sg.astype(np.uint8)
        dst[h, w, 2] = sr.astype(np.uint8)

print("done.")      
cv2.imshow('result', dst)
print("press anykey on the image")
cv2.waitKey(0)
cv2.destroyAllWindows()

[python] OpenCV 픽셀단위 처리 (그레이영상만들기)

영상 처리: 픽셀단위처리 - 그레이영상 만들기
--------------------
import numpy as np
import cv2

img = cv2.imread('demo.jpg')    #img는 numpy array
print(img.shape, img.dtype)

cv2.imshow('', img)
print("press anykey on the image")
cv2.waitKey(0)

height = img.shape[0]
width = img.shape[1]

gray = np.zeros((height, width, 3), dtype=np.uint8)

print("converting...", end='')
for h in range(height):
    for w in range(width):
        b = img[h, w, 0].astype(np.float)
        g = img[h, w, 1].astype(np.float)
        r = img[h, w, 2].astype(np.float)
     
        intensity = (b + g + r) / 3
        #intensity = intensity.astype(np.uint8)
     
        gray[h, w, 0] = intensity
        gray[h, w, 1] = intensity
        gray[h, w, 2] = intensity

print("done.")    
cv2.imshow('', gray)
print("press anykey on the image")
cv2.waitKey(0)
cv2.destroyAllWindows()

--------------------
b = img[:,:,0].astype(np.float)
g = img[:,:,1].astype(np.float)
r = img[:,:,2].astype(np.float)
gray = ((b+g+r)/3.0).astype(np.uint8)
--------------------

[python] OpenCV 인스톨, 영상읽기, 화면출력

[인스톨]
conda install -c menpo opencv3
https://anaconda.org/menpo/opencv3

[예제코드]

# 영상읽고 출력하기1
--------------------
import cv2
import numpy as np

img = cv2.imread('demo.jpg')
img.shape
img.dtype

cv2.imshow('', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 영상 출력하기2
--------------------
from matplotlib import pyplot as plt

plt.subplot(111), plt.imshow(img), plt.title('input'),plt.xticks([]), plt.yticks([])
plt.show()

# subplot(111)
# 111: 1x1 에서 1번째 위치
# 121: 1x2 에서 1번째 위치
# 122: 1x2 에서 2번째 위치



2017년 9월 2일 토요일

[Win32] 시작코드

#include <windows.h> 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,  LPSTR lpszArg, int nCmdShow) 

HWND hWnd; 
MSG msg; 
WNDCLASS WndClass;
WndClass.style = NULL; 
WndClass.lpfnWndProc = WndProc; 
WndClass.cbClsExtra = 0; 
WndClass.cbWndExtra = 0; 
WndClass.hInstance = hInstance; 
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
WndClass.lpszMenuName = NULL; 
WndClass.lpszClassName = "Hello"; 

if(!RegisterClass(&WndClass)) return NULL; 

hWnd = CreateWindow( 
"Hello", 
"My", 
WS_OVERLAPPEDWINDOW, 
CW_USEDEFAULT, 
CW_USEDEFAULT, 
CW_USEDEFAULT, 
CW_USEDEFAULT, 
NULL, NULL, hInstance, 
NULL 
);
 
ShowWindow(hWnd, nCmdShow); 
UpdateWindow(hWnd); 

while(GetMessage(&msg, NULL, 0, 0)) 

TranslateMessage(&msg); 
DispatchMessage(&msg); 

return msg.wParam; 
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT mesg, WPARAM wParam, LPARAM lParam)

switch(mesg) 
{
case WM_DESTROY : 
PostQuitMessage(0); 
return FALSE; 

return DefWindowProc(hWnd, mesg, wParam, lParam); 


[python] zip, map, lambda, list

a = [1,2,3,4,5]
b = ['a','b','c','d','e']
for x,y in zip (a,b):
    print (x, y)
--------------------
1 a
2 b
3 c
4 d
5 e

----------------------------------------
import numpy as np
def func(x):
   return x * 2
b = map(func, [1, 2, 3, 4])
for x in b:
    print(x)
--------------------
2
4
6
8

* map 오브젝트는 리스트 같지만 원소에 직접 접근은 불가
* b[0] 사용 불가

----------------------------------------
a = lambda x, y: x * y
print(a(3, 4))
--------------------
12

----------------------------------------
import numpy as np
k =[
[11,12,13,14,15,16],
[21,22,23,24,25,26],
[31,32,33,34,35,36],
[41,42,43,44,45,46],
[51,52,53,54,55,56]
]
a = np.array(k)
print(a[::2,1:3:)])
--------------------
[[12 13]
 [32 33]
 [52 53]]

* a[start : end : step] // row에 적용
* a[start : end : step, start, end] // row, column에 적용