pythonのメモ
pythonを使った時のテストコード。
2014/07/30
閾値処理(threshold)
python 2.7
numpy 1.8.1
openCV 2.4.9
Windows 7 64bit
fig.1 gray
fig.2 cv2.THRESH_BINARY
fig.3 cv2.THRESH_BINARY_INV
fig.4 cv2.THRESH_OTSU
fig.5 cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU
fig.6 cv2.THRESH_TOZERO_INV
fig.7 cv2.THRESH_TOZERO_INV, mask=cv2.THRESH_BINARY
threshold
import cv2.cv as cv import cv2 import numpy as np src = cv2.imread('lena.jpg') #fig.1 gray = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) cv2.imshow('gray',gray) cv2.imwrite('gray.png',gray) #fig.2 ret, thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY) print ret cv2.imshow('binary',thresh) cv2.imwrite('thresh.png',thresh) #fig.3 ret_inv, thresh_inv = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV) print ret_inv cv2.imshow('binary_inv',thresh_inv) cv2.imwrite('thresh_inv.png',thresh_inv) #fig.4 ret_otsu, thresh_otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU) print ret_otsu cv2.imshow('binary_otsu',thresh_otsu) cv2.imwrite('thresh_otsu.png',thresh_otsu) #fig.5 ret_inv_otsu, thresh_inv_otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) print ret_inv_otsu cv2.imshow('binary_inv_otsu',thresh_inv_otsu) cv2.imwrite('thresh_inv_otsu.png',thresh_inv_otsu) #fig.6 ret_tozero_inv, thresh_tozero_inv = cv2.threshold(gray, 100, 255, cv2.THRESH_TOZERO_INV) print ret_tozero_inv cv2.imshow('binary_tozero_inv',thresh_tozero_inv) cv2.imwrite('thresh_tozero_inv.png',thresh_tozero_inv) #fig.7 mask = thresh > 200 thresh_tozero_inv[mask] = 255 cv2.imshow('binary_tozero_inv_255',thresh_tozero_inv) cv2.imwrite('thresh_tozero_inv_255.png',thresh_tozero_inv) cv2.waitKey(0) cv2.destroyAllWindows()
LINK
How to define the markers for Watershed in OpenCV?
threshold
2014/07/28
線を引く
python 2.7
numpy 1.8.1
openCV 2.4.9
Windows 7 64bit
import cv2.cv as cv import cv2 import numpy as np h = 100 w = 200 canvas = np.zeros((h, w, 3), np.uint8) print canvas.shape[:2] cv2.line(canvas,(110,0),(10,80),(0,255,0)) sp = (30,0) ep = (70,80) cv2.line(canvas,sp,ep,(0,0,255),5) spl = [50,0] color = cv.CV_RGB(255,0,0) cv2.line(canvas,tuple(spl),(90,80),color,5,4) cv2.line(canvas,(70,0),(110,80),color,5,cv.CV_AA) cv2.line(canvas,(130,0),(30,80),(0,255,0),1,cv.CV_AA) cv2.imshow('Line', canvas) cv2.waitKey() cv2.destroyWindow('Line')
座標はタプルで。
2014/07/25
画像を読み込んで表示
python 2.7
numpy 1.8.1
openCV 2.4.9
Windows 7 64bit
openCV2
import cv2 img = cv2.imread('lena.jpg') cv2.imshow('Lena',img) cv2.waitKey() cv2.destroyAllWindows()
openCV
import cv2.cv as cv img = cv.LoadImage('lena.jpg') cv.ShowImage('Lena',img) cv.WaitKey() cv.DestroyWindow('Lena')
openCV -> openCV2
IplImage -> cvmat -> numpy.ndarray
import cv2.cv as cv import cv2 import numpy as np img = cv.LoadImage('lena.jpg') tmp = cv.GetMat(img) nimg = np.asarray(tmp) cv2.imshow('Lena',nimg) cv2.waitKey() cv2.destroyAllWindows()
openCV2 -> openCV
numpy.ndarray -> IplImage
import cv2.cv as cv import cv2 tmp = cv2.imread('lena.jpg') img = cv.CreateImageHeader((tmp.shape[1], tmp.shape[0]), cv.IPL_DEPTH_8U, 3) cv.SetData(img, tmp.tostring(), tmp.dtype.itemsize * 3 * tmp.shape[1]) cv.ShowImage('Lena',img) cv.WaitKey() cv.DestroyWindow('Lena')
print type(img)
で、型が確認できるので便利。
LINK
OpenCV 2.4.9.0 documentation » OpenCV API Reference »core. The Core Functionality
opencv2.2 クックブック NumPy と OpenCV
新しい投稿
ホーム
登録:
投稿 (Atom)