how to generate company logo image mask using OpenCV?

Question:

I am working on removing the company logo background image mask but I’m not getting the exact 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.

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
Asked By: Parth Soni

||

Answers:

Here is my code for remove company logo background using opencv it will be work for me. Here is my input image and output image of mask in below.

# 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:

input image

Output image mask:

output image mask

Answered By: Ghanshyam
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.