Python/CV Tips †OpenCV + Python †matplotlibで画像を表示する †File not found: "number" at page "Python/CV Tips"; 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を取得する †File not found: "number" at page "Python/CV Tips"; 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 パフォーマンスを計測する †File not found: "number" at page "Python/CV Tips"; 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 † |