*Python/CV Tips [#oc212011]
#contents
**OpenCV + Python [#q61acc7f]
***matplotlibで画像を表示する [#c75afefd]
#code(Python,nooutline,number){{
import cv2
import matplotlib.pyplot as plt
if __name__ == "__main__":
img_bgr = cv2.imread("lena.jpg", cv2.IMREAD_UNCHANGED)
## matplotlibで表示するためにRGBにする
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
## matplotlibの表示設定
plt.imshow(img_rgb, interpolation='bicubic')
plt.title('Image')
plt.xticks([]), plt.yticks([])
plt.show()
}}
***幅,高さ,チャンネル数,depthを取得する [#nb49d35f]
#code(Python,nooutline,number){{
import cv2
if __name__ == "__main__":
img = cv2.imread("lena.jpg", cv2.IMREAD_UNCHANGED)
## カラーとグレースケールで場合分け
if len(img.shape) == 3:
height, width, channels = img.shape[:3]
else:
height, width = img.shape[:2]
channels = 1
## 取得結果(幅,高さ,チャンネル数,depth)を表示
print "width: " + str(width)
print "height: " + str(height)
print "channels: " + str(channels)
print "dtype: " + str(img.dtype)
}}
出力例:
width: 512
height: 512
channels: 3
dtype: uint8
***パフォーマンスを計測する [#nd616b37]
#code(Python,nooutline,number){{
import cv2
## パフォーマンス計測用関数
def measuring_func(image):
## 処理時間計測
start = cv2.getTickCount()
blur = cv2.medianBlur(image, 49)
end = cv2.getTickCount()
time = (end - start)/cv2.getTickFrequency()
## SIMD最適化が有効か否かを表示
print "cv2.useOptimized: " + str(cv2.useOptimized())
## 処理時間表示
print " " + str(time) + "[sec]"
if __name__ == "__main__":
img = cv2.imread("building.jpg", cv2.IMREAD_UNCHANGED)
## SIMD最適化を無効にする
cv2.setUseOptimized(False)
measuring_func(img)
## SIMD最適化を有効にする
cv2.setUseOptimized(True)
measuring_func(img)
}}
出力例:
cv2.useOptimized: False
0.515112325[sec]
cv2.useOptimized: True
0.17345505[sec]
**参考URL [#n780ea9e]
-[[OpenCV-Python Tutorials — OpenCV 3.0.0-dev documentation:http://docs.opencv.org/trunk/doc/py_tutorials/py_tutorials.html]]
-[[Welcome to OpenCV-Python Tutorials’s documentation! — OpenCV-Python Tutorials 1 documentation:https://opencv-python-tutroals.readthedocs.org/en/latest/index.html]]