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

wslでubuntu その9

Last updated at Posted at 2020-01-25

概要

wslでpython-opencvやってみる。
輪郭を検出して、背景を消すをやって見た。

環境

windows10 64bit

インストール

sudo apt install Python3-opencv

元画像

結果

res14.png

サンプルコード

import numpy as np
import cv2

img = cv2.imread("cat0.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 5, 70)
edges = cv2.dilate(edges, None)
edges = cv2.erode(edges, None)
h, w = img.shape[ : 2]
back = np.ones((h, w, 3), np.uint8) * 255
contour_info = []
contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
for c in contours:
	contour_info.append((c, cv2.isContourConvex(c), cv2.contourArea(c), ))
contour_info = sorted(contour_info, key = lambda c: c[2], reverse = True)
max_contour = contour_info[0]
mask = np.zeros(edges.shape)
cv2.fillConvexPoly(mask, max_contour[0], (255))
kernel = np.array([[0.0, -1.0, 0.0], [-1.0, 5.0, -1.0], [0.0, -1.0, 0.0]])
mask = cv2.filter2D(mask, -1, kernel)
res = np.where(np.expand_dims(mask < 1, -1), back, img)
cv2.imwrite('res14.png', res)

以上。

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?