0
0

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 3 years have passed since last update.

画像合成差分

Posted at
  • 画像単色化
import cv2
import numpy as np

# 画像を読み込む。
img = cv2.imread("b.jpeg")

# グレースケールに変換する。
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 2値化する。
thresh, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
print(thresh)

# RGBA に変換する。
rgba = cv2.cvtColor(binary, cv2.COLOR_RGB2RGBA)

# 白 (背景) のアルファチャンネルは0にして、透明にする。
rgba[..., 3] = np.where(np.all(rgba == 255, axis=-1), 0, 255)

# 画像の黒い部分を白に置き換える
black = [0, 0, 0, 255]
blue = [255, 0, 0, 200]
green = [0, 255, 0, 200]
rgba[np.where((rgba == black).all(axis=2))] = green

# アパーチャーサイズ 3, 5, or 7 など 1 より大きい奇数。数値が大きいほどぼかしが出る。
ksize=5
# 中央値フィルタ
img_mask = cv2.medianBlur(rgba,ksize)

# 保存する。
cv2.imwrite(r"result2.png", img_mask)
  • 画像合成
import cv2
import matplotlib.pyplot as plt
import numpy as np

img1 = cv2.imread('result2.png', -1)
img2 = cv2.imread('result.png', -1)

# img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
# img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

# img1 =cv2.resize(img1,(600,800))
# img2 =cv2.resize(img2,(600,800))

blended = cv2.addWeighted(src1=img1,alpha=0.5,src2=img2,beta=0.3,gamma=0)
# 保存する。
cv2.imwrite(r"blended.jpg", blended)

参考
OpenCV 画像の二値化
Python/OpenCVで任意色を透過させたpng画像に変換
OpenCVを使って初めての背景透過
画像のノイズ除去

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?