Ask Your Question
0

Replacing the Background of an image?

asked 2019-02-26 04:46:38 -0600

Rounak gravatar image

updated 2019-02-26 08:34:33 -0600

supra56 gravatar image

As I wanted to Remove the background with some specific colors using python. And written some lines of codes but facing with a error. Not getting how it can be resolved?
kindly please check the code i have written:

import cv2
import numpy as np
from matplotlib import pyplot as plt

BLUR = 21
CANNY_THRESH_1 = 10
CANNY_THRESH_2 = 200
MASK_DILATE_ITER = 10
MASK_ERODE_ITER = 10
MASK_COLOR = (255.0, 255.0, 255.0)  # In BGR format
img = cv2.imread("1.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
edges = cv2.dilate(edges, None)
edges = cv2.erode(edges, None)
contour_info = []
contours: None = 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_contours = contour_info[0]
#mask = np.zeros(edges.shape)
#cv2.fillConvexPoly(mask, max_contour[0], (255))
mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
mask_stack = np.dstack([mask] * 3)  # Create 3-channel alpha mask
mask_stack = mask_stack.astype('float32') / 255.0  # Use float matrices,
img = img.astype('float32') / 255.0  # for easy blending
masked = (mask_stack * img) + ((1 - mask_stack) * MASK_COLOR)  # Blend
masked = (masked * 255).astype('uint8')  # Convert back to 8-bit
plt.imsave('img/1.jpg',masked)
c_red,c_green,c_blue=cv2.split(img)
img_a=cv2.merge((c_red,c_green,c_blue,mask.astype('float32')/255.0))
plt.imshow(img_a)
plt.show()
cv2.imwrite('img/1_1.jpg',img_a*255)
plt.imsave('img/1_2.jpg',img_a)
cv2.imshow('img', masked)  # Display
cv2.waitKey()

The error: File "C:/Users/USER/PycharmProjects/Project/image.py", line 22, in <module> cv2.isContourConvex(c), TypeError: Expected cv::UMat for argument 'contour'

Solving this problem will highly Appreciated.

edit retag flag offensive close merge delete

Comments

1

try

contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

instead of

contours: None = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

what does it mean contours: None ?

LBerger gravatar imageLBerger ( 2019-02-26 10:07:43 -0600 )edit

Ohh yes! it started working but gives me error at line 39. which says:File "C:/Users/USER/PycharmProjects/Project/image.py", line 29, in <module> mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER) NameError: name 'mask' is not defined

Hope you give a reply to the query. I am thankful to you.

Rounak gravatar imageRounak ( 2019-02-27 00:32:54 -0600 )edit
1

NameError: name 'mask' is not defined

mask is not defined in your program. At what line can I read i "mask = " ?

LBerger gravatar imageLBerger ( 2019-02-27 00:45:41 -0600 )edit

@Rounak. Good catch by @LBerger. Remove comment and see what happen. mask = np.zeros(edges.shape)

supra56 gravatar imagesupra56 ( 2019-02-27 03:53:11 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-02-26 07:51:07 -0600

supra56 gravatar image

The append is limited one argument not 3.

edit flag offensive delete link more

Comments

Many more thanks for your reply. If you could elaborate the limited argument or the missing one.

I will be Thankful to you. Thanks

Rounak gravatar imageRounak ( 2019-02-26 07:55:56 -0600 )edit

@Rounak. Can you post original image? Btw, I don't used matplotlib.

supra56 gravatar imagesupra56 ( 2019-02-26 08:36:17 -0600 )edit
1

https://banner2.kisspng.com/20180328/...

Hey! you can use this picture. Thanks .

Rounak gravatar imageRounak ( 2019-02-27 00:35:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-26 04:46:38 -0600

Seen: 1,628 times

Last updated: Feb 26 '19