简体   繁体   English

如何使用 OpenCV 生成公司徽标图像掩码?

[英]how to generate company logo image mask using OpenCV?

I am working on removing the company logo background image mask but I'm not getting the exact output.我正在努力删除公司徽标背景图像蒙版,但我没有得到确切的 output。 I try many ways to remove the company logo background but every model is trained in human categories just like model name mode net, u2net, etc. I plan to train the custom model so I need an image and mask I'm generating an image mask but not getting exact output here is my code.我尝试了很多方法来删除公司徽标背景,但是每个 model 都接受过人类类别的训练,就像 model 名称模式网、u2net 等一样。我计划训练自定义 Z20F35E630DAF44DBFA4C3F68F5399D8但没有得到准确的 output 这是我的代码。

import numpy as np 

# Reading image
image = cv2.imread('image_path')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

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

# creating canvas
canvas = np.zeros(image.shape, np.uint8)
canvas.fill(255)

#creating image background mask
mask = np.ones(image.shape, dtype=np.uint8) * 255

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

cnts = contours[0]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)

    ROI = image[y:y+h, x:x+w]

#finding controus
contours_mask, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for contour in range(len(contours_mask)):
   cv2.fillConvexPoly(mask, contours_mask[contour], (0,0,255))

# fill color in background image
cv2.floodFill(mask, None, seedPoint=(0, 0), newVal=128, loDiff=1, upDiff=1)

r=cv2.selectROI('ROI', edged,False,False)
imROI = edged[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]

cv2.imshow('Canny Edges After Contouring', ROI)
cv2.waitKey(0)

print("Number of Contours found = " + str(len(contours)))

# Draw all contours
# -1 signifies drawing all contours
cv2.drawContours(image, contours, -1, (0, 255, 0), 3)

cv2.imshow('Contours', image)
cv2.imshow('background mask', mask)
cv2.imshow('canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()```

**>I am getting that kind of output in background mask image output.**
[enter image description here][1]


[enter image description here][2]


  [1]: https://i.stack.imgur.com/6B5Mx.png

Here is my code for remove company logo background using opencv it will be work for me.这是我使用 opencv 删除公司徽标背景的代码,它对我有用。 Here is my input image and output image of mask in below.这是我的输入图像和下面的掩码 output 图像。

# import library
import cv2
import numpy as np

# Reading image
img = cv2.imread("sample_image/logo_1.png", cv2.IMREAD_GRAYSCALE)

# Cannny edge detection
img_canny = cv2.Canny(img, 50, 50)

# Applies a morphological filter to image
img_dilate = cv2.dilate(img_canny, None, iterations=1)

# Perform erosion on the image
img_erode = cv2.erode(img_dilate, None, iterations=1)

# creating new image of given shape 
mask = np.full(img.shape, 0, "uint8")

# Finding Contours
contours, hierarchies = cv2.findContours(img_erode, cv2.RETR_TREE, 
cv2.CHAIN_APPROX_NONE)

# draw contours
for cnt in contours:
  cv2.drawContours(mask, [cnt], -1, 255, -1)

# write image
cv2.imwrite("output_of_mask.png", mask)

# show image
cv2.imshow("result", mask)
cv2.waitKey(0)

Input image:输入图像:

输入图像

Output image mask: Output 图像掩码:

输出图像掩码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM