2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python + OpenCVによるマスク画像の動的生成方法

Posted at

マスク画像の生成を動的に生成する方法

下記では、100×100ピクセル画像に対し四角形のマスク範囲を描画している

test.py
import cv2

# マスク用単一色画像を作成
height = 100 # 生成画像の高さ
width = 100 # 生成画像の幅
imgMask = np.full((height, width, 1), 1, dtype=np.uint8)

# マスク範囲を四角形で描画
boxFromX = 20 #マスク範囲開始位置 X座標
boxFromY = 40 #マスク範囲開始位置 Y座標
boxToX = 80 #マスク範囲終了位置 X座標
boxToY = 60 #マスク範囲終了位置 Y座標
cv2.rectangle(imgMask, (boxFromX, boxFromY), (boxToX, boxToY),(255), cv2.FILLED)

# マスク結果画像を保存
cv2.imwrite("testMaskImg.png", imgMask)

上記で生成されたマスク画像
testMaskImg.png

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?