Python OpenCV3で画像のアスペクト比(縦横比)を求める
0.2回分の内容
[amazonjs asin="4839952965″ locale="JP" title="OpenCV 3 プログラミングブック"]
前提
画像のアスペクト比を求める
この星風の画像のアスペクト比を求めてみる。
source/image ディレクトリにhoshi.pngを設置する。
画像のアスペクト比を求める、get_aspectratio関数を作成する。
sourceディレクトリに
[python title="aspect.pyの内容"]
# -*- coding: UTF-8 -*-
import cv2
import math
import numpy as np
import os
def get_aspectratio(img_src):
# 画素の行数、列数を取得
rows, cols = img_src.shape
x_min = cols
x_max = 0
y_min = rows
y_max = 0
# 各行ごとに操作
for y in range(rows):
# 各列ごとに操作
for x in range(cols):
if img_src[y, x] == 255:
if x < x_min:
x_min = x
elif x > x_max:
x_max = x
if y < y_min:
y_min = y
elif y > y_max:
y_max = y
aspectratio = float(y_max – y_min) / float(x_max – x_min)
return aspectratio
if __name__ == '__main__’:
# 画像の読み込み
img_src = cv2.imread("./image/hoshi.png", 0)
aspectratio = get_aspectratio(img_src)
print(aspectratio)
[/python]
実行してみる。
[shell]
(opencv_python)$ python aspect.py
0.9765765765765766
[/shell]
ディスカッション
コメント一覧
まだ、コメントがありません