Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Replacing the Background of an 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.

click to hide/show revision 2
retagged

updated 2019-02-26 07:12:22 -0600

berak gravatar image

Replacing the Background of an 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.

Replacing the Background of an 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: written:

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

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)

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, contour_info[0] #mask = np.zeros(edges.shape) #cv2.fillConvexPoly(mask, max_contour[0], (255))

(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()

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.