白丸の背景で画像を滑らかにします

Aug 24 2020

このプロジェクトをPythonで実行し
たいこのメイジがいます。アップロードする


各画像を下の画像のように作成します。境界線を追加するために境界線が必要な場合は、各画像をこのように円で囲みます。灰色の背景を追加します

回答

2 fmw42 Aug 24 2020 at 21:56

PythonOpenCVでこれを行う1つの方法があります。

  • 入力を読む
  • 最大寸法、オフセット、入力の中心を計算します
  • 最大寸法とパディングの白い画像を作成します
  • 入力画像を白い画像の中央に挿入します
  • 白い画像と同じ寸法の灰色の背景画像を作成します
  • 灰色の背景の中央に最大寸法に等しい直径の黒い円を描きます
  • 黒丸をぼかしてドロップシャドウを作成します
  • 黒の画像の中心に最大寸法に等しい直径の白い円を作成します
  • 白い背景の画像と背景のぼやけた黒い円をブレンドして結果を形成します
  • 結果を保存する

入力:

import cv2
import numpy as np

# load image and get maximum dimension
img = cv2.imread("radio_skull.jpg")
hh, ww = img.shape[:2]
maxwh = max(ww,hh)
offx = (maxwh - ww) // 2
offy = (maxwh - hh) // 2
cx = maxwh // 2
cy = maxwh // 2
pad = 10
pad2 = 2*pad

# create white image of size maxwh plus 10 pixels padding all around
white = np.full((maxwh+pad2, maxwh+pad2, 3), (255,255,255), dtype=np.uint8)

# put input img into center of white image
img_white = white.copy()
img_white[offy+pad:offy+pad+hh, offx+pad:offx+pad+ww] = img

# create light gray background image with 10 pixel padding all around
bckgrnd = np.full((maxwh+pad2,maxwh+pad2,3), (192,192,192), dtype=np.uint8)

# create black circle on background image for drop shadow
cv2.circle(bckgrnd, (cx+pad,cy+pad), cx, (0,0,0), -1)

# blur black circle
bckgrnd = cv2.GaussianBlur(bckgrnd, (25,25), 0)

# create white circle on black background as mask
mask = np.zeros_like(img_white)
cv2.circle(mask, (cx+pad,cy+pad), cx, (255,255,255), -1)

# use mask to blend img_white and bckgrnd
img_white_circle = cv2.bitwise_and(img_white, mask)
bckgrnd_circle = cv2.bitwise_and(bckgrnd, 255-mask)
result = cv2.add(img_white_circle, bckgrnd_circle)

# write result to disk
cv2.imwrite("radio_skull_img_white.jpg", img_white)
cv2.imwrite("radio_skull_background.jpg", bckgrnd)
cv2.imwrite("radio_skull_mask.jpg", mask)
cv2.imwrite("radio_skull_img_white_circle.jpg", img_white_circle)
cv2.imwrite("radio_skull_bckgrnd_circle.jpg", bckgrnd_circle)
cv2.imwrite("radio_skull_result.jpg", result)

# display it
cv2.imshow("img_white", img_white)
cv2.imshow("bckgrnd", bckgrnd)
cv2.imshow("mask", mask)
cv2.imshow("img_white_circle", img_white_circle)
cv2.imshow("bckgrnd_circle", bckgrnd_circle)
cv2.imshow("result", result)
cv2.waitKey(0)

白い背景に入力:

背景にぼやけた黒い円:

マスク:

白のマスクされた画像:

マスクされた黒い円:

結果: