pythonのメモ
pythonを使った時のテストコード。
2014/08/17
normalizeとカラーヒストグラム
cv2.normalize()の使い方
RGBのカラーヒストグラムを例にnormalize()を使ってみる。
python 2.7
numpy 1.8.1
openCV 2.4.9
Windows 7 64bit
import cv2 import numpy as np img = cv2.imread('lena.jpg') channel = 3 calcRange = [0,256] lower = 0 upper = 100 hist = np.zeros([channel*upper,256],dtype=np.uint8) rows, cols= hist.shape hist = cv2.cvtColor(hist,cv2.COLOR_GRAY2BGR) for j in range(channel): chist = cv2.calcHist([img],[j], None, [256], calcRange) cv2.normalize(chist,chist,lower,upper,cv2.NORM_MINMAX) color = [0,0,0] color[j] = 255 y = rows - j*upper for i in range(0,256): v = chist[i] cv2.line(hist,(i,y),(i, y - v),color) cv2.imshow('result',hist) cv2.imshow('lena',img) cv2.waitKey() cv2.destroyAllWindows()
変数lower,upperが正規化するときの下限と上限。
2016/9/24 code修正 rangeに誤り
2014/08/05
'int' object is not callable
'int' object is not callable
というエラー
関数の仮引数にrangeを入れ、その中でrange関数を使ってしまったためにエラーになってしまった。
とにかく関数名を変数名として使わないように気をつける。
原因を見つけるのに時間がかかった。
気をつけねば。
LINK
ひきメモ
[python]変数名と関数名は同じにしてはいけない
2014/08/03
動的輪郭モデル(SnakeImage)を使ってみる。
CvPointがないため、座標はタプルで行うということは、わかっていたのだけど。リストで座標集めてタプルにして渡すというところまでは、なかなか思いつかず手間取った。
youtubeに
SnakeImageの例
があったのだけれど、リンク切れしててわからず。実際はどんなふうにしてるんでしょうね...
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 img = cv2.imread('en.bmp') h,w = img.shape[:2] length = 60; center_x = w/2 center_y = h/2 channel = 3 src = cv2.imread('en.bmp') iplimg = cv.CreateImageHeader((src.shape[1], src.shape[0]), cv.IPL_DEPTH_8U, channel) cv.SetData(iplimg, src.tostring(), src.dtype.itemsize * channel * src.shape[1]) iplimgGray = cv.CreateImage((w,h),cv.IPL_DEPTH_8U,1) cv.CvtColor(iplimg,iplimgGray,cv.CV_BGR2GRAY) cv2.imshow('original',src) pts = [] for i in range(length): pts.append((int(center_x * math.cos(2*math.pi*i/length)+center_x),int(center_y*math.sin(2*math.pi*i/length)+center_y))) cp = cv.SnakeImage(iplimgGray,tuple(pts),0.45,0.35,0.2,(15,15),(cv.CV_TERMCRIT_ITER,10,0)) for j in range(15): cp = cv.SnakeImage(iplimgGray,cp,0.45,0.35,0.2,(15,15),(cv.CV_TERMCRIT_ITER,10,0)) ncp = np.asarray(cp) gray = np.zeros((w, h, 3), dtype='uint8') cv2.polylines(gray,[ncp],1,(255,255,255)) cv2.imshow('SnakeImage',gray) cv2.waitKey() cv2.waitKey() cv2.destroyAllWindows()
LINK
OpenCV 2.2 (r4295) Python リファレンス
snakeimage
Youtube
cv.SnakeImage Active Contour OpenCV Python
2014/08/01
opencv2で開いてPILで輝度変更
python 2.7
numpy 1.8.1
openCV 2.4.9
PIL(python image library)
Windows 7 64bit
PIL - opencv2
import cv2 import numpy as np from PIL import Image, ImageEnhance src = cv2.imread('lena.jpg') pimg = Image.fromarray(src) img = ImageEnhance.Brightness(pimg).enhance(1.5) dst = np.asarray(img) cv2.imshow('Lena',dst) cv2.waitKey() cv2.destroyAllWindows()
2016/9/24今はもうPillowを使うんだって。
LINK
画像処理でのPythonの利用 - SlideShare
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)