删除轮廓周围的背景 [英] Removing Background Around Contour

查看:0
本文介绍了删除轮廓周围的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只学习了几个星期的Python和OpenCV编程,但StackOverflow已经帮了我很多次了。然而,我似乎想不出这个问题,所以决定问我的第一个问题。

  • 我正在尝试拍摄图像
  • 按面积查找最大等高线
  • 删除轮廓外部的背景
  • 有效地从中最大的对象&中删除背景 图片。

我正在努力完成最后一部分。我知道我需要以某种方式创建一个蒙版,然后将该蒙版放在原始图像上。

如何创建正确类型的蒙版?如何将蒙版放置在原始图像的顶部?

这是我的代码:

import cv2
import numpy as np

# Load image
image = cv2.imread('Resources/X.png')

# Grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Find Canny edges
edged = cv2.Canny(gray, 30, 200)

# Finding Contours
contours, hierarchy = cv2.findContours(edged,
                                       cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

cv2.imshow('Canny Edges After Contouring', edged)
print("Number of Contours found = " + str(len(contours)))
cv2.waitKey(0)

# Largest contour
c = max(contours, key=cv2.contourArea)


# Not sure what to do from here. Attempt below:
mask = np.zeros(image.shape, np.uint8)                  # What is this actually doing? what does np.unit8 mean?
cv2.drawContours(mask, c, -1, (255, 255, 255), 1)       # I am drawing the correct outline/contour

cv2.imshow('Mask', mask)
cv2.waitKey(0)

如有任何帮助,我们将不胜感激。

谢谢 克里斯

编辑:

我设法做到了,但不确定我在做什么:-( 我怎样才能得到不同颜色的背景?我想我必须用另一种颜色填充空白面具? 我也不确定按位函数实际在做什么。

blank_mask = np.zeros(image.shape, dtype=np.uint8)
cv2.fillPoly(blank_mask, [c], (255,255,255))
blank_mask = cv2.cvtColor(blank_mask, cv2.COLOR_BGR2GRAY)
result = cv2.bitwise_and(original,original,mask=blank_mask)
cv2.imshow('Result', result)

推荐答案

以下是使用PYTHON/OpenCV更改图像背景的一种方法。

  • 阅读输入并获取其尺寸
  • 黑色阈值和反转以在黑色背景上获得白色
  • 从倒置的阈值图像中获取最大轮廓
  • 以白色填充黑色背景绘制最大轮廓作为蒙版
  • 创建反转蒙版
  • 创建新的彩色背景图像
  • 将蒙版应用于图像
  • 将反转蒙版应用于背景颜色图像
  • 添加两个镜像
  • 保存结果

import cv2
import numpy as np

# Read image
img = cv2.imread('shapes.png')
hh, ww = img.shape[:2]

# threshold on black
# Define lower and uppper limits of what we call "white-ish"
lower = np.array([0, 0, 0])
upper = np.array([0, 0, 0])

# Create mask to only select black
thresh = cv2.inRange(img, lower, upper)

# invert mask so shapes are white on black background
thresh_inv = 255 - thresh

# get the largest contour
contours = cv2.findContours(thresh_inv, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# draw white contour on black background as mask
mask = np.zeros((hh,ww), dtype=np.uint8)
cv2.drawContours(mask, [big_contour], 0, (255,255,255), cv2.FILLED)

# invert mask so shapes are white on black background
mask_inv = 255 - mask

# create new (blue) background
bckgnd = np.full_like(img, (255,0,0))

# apply mask to image
image_masked = cv2.bitwise_and(img, img, mask=mask)

# apply inverse mask to background
bckgnd_masked = cv2.bitwise_and(bckgnd, bckgnd, mask=mask_inv)

# add together
result = cv2.add(image_masked, bckgnd_masked)

# save results
cv2.imwrite('shapes_inverted_mask.jpg', mask_inv)
cv2.imwrite('shapes_masked.jpg', image_masked)
cv2.imwrite('shapes_bckgrnd_masked.jpg', bckgnd_masked )
cv2.imwrite('shapes_result.jpg', result)

cv2.imshow('mask', mask)
cv2.imshow('image_masked', image_masked)
cv2.imshow('bckgrnd_masked', bckgnd_masked)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

从最大轮廓遮罩图像:

屏蔽图像:

背景已屏蔽:

结果:

这篇关于删除轮廓周围的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆