操作数不能与形状(1406,1100,3)(653,639,3)一起广播

时间:2019-05-01 03:32:49

标签: python opencv

为什么会出现此错误? 我与您分享我正在使用的图片。 我在其他图像上尝试了相同的脚本,但不会发生错误。 想法是,它可以是用于各种图像的通用脚本。

目标是最终使图像获得白色背景。

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

#== Parameters ======================================================================= 
BLUR = 21 
CANNY_THRESH_1 = 10 
CANNY_THRESH_2 = 200 
MASK_DILATE_ITER = 10 
MASK_ERODE_ITER = 10 
MASK_COLOR = (0.0,0.0,1.0) # In BGR format 


#== Processing ======================================================================= 

#-- Read image ----------------------------------------------------------------------- 
img = cv2.imread('./remera.png') 

#-- Edge detection ------------------------------------------------------------------- 
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2) 
edges = cv2.dilate(edges, None) 
edges = cv2.erode(edges, None) 

#-- Find contours in edges, sort by area --------------------------------------------- 
contour_info = [] 
_, contours, _ = 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_contour = contour_info[0] 

# split image into channels 
c_red, c_green, c_blue = cv2.split(img) 

#-- Create empty mask, draw filled polygon on it corresponding to largest contour ---- 
# Mask is black, polygon is white 
mask = np.zeros(edges.shape) 
cv2.fillConvexPoly(mask, max_contour[0], (255)) 

#-- Smooth mask, then blur it -------------------------------------------------------- 
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 

#-- Blend masked img into MASK_COLOR background -------------------------------------- 
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 



---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-71-dd2e80b083d7> in <module>()
     53 img   = img.astype('float32')/255.0     # for easy blending
     54 
---> 55 masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR) # Blend
     56 masked = (masked * 255).astype('uint8')      # Convert back to 8-bit
     57 
[![enter image description here][1]][1]
ValueError: operands could not be broadcast together with shapes (1406,1100,3) (653,639,3) 

欢迎任何帮助。

0 个答案:

没有答案