gpt4 book ai didi

python - OpenCV错误:该操作既不是 'array op array'也不是 'array op scalar',也不是函数 'scalar op array'中的 'cv::arithm_op'

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:02 24 4
gpt4 key购买 nike

我正在KERAS CNN中工作,以检测视网膜图像中的糖尿病性视网膜病变。但是,当我尝试使用此代码预处理图像..:

def estimate_radius(img):
mx = img[img.shape[0] // 2,:,:].sum(1)
rx = (mx > mx.mean() / 10).sum() / 2

my = img[:,img.shape[1] // 2,:].sum(1)
ry = (my > my.mean() / 10).sum() / 2

return (ry, rx)


def crop_img(img, h, w):
h_margin = (img.shape[0] - h) // 2 if img.shape[0] > h else 0
w_margin = (img.shape[1] - w) // 2 if img.shape[1] > w else 0

crop_img = img[h_margin:h + h_margin,w_margin:w + w_margin,:]

return crop_img

def subtract_gaussian_blur(img):
# http://docs.opencv.org/trunk/d0/d86/tutorial_py_image_arithmetics.html
# http://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html
gb_img = cv2.GaussianBlur(img, (0, 0), 5)

return cv2.addWeighted(img, 4, gb_img, -4, 128)

def remove_outer_circle(a, p, r):
b = np.zeros(a.shape, dtype=np.uint8)
cv2.circle(b, (a.shape[1] // 2, a.shape[0] // 2), int(r * p), (1, 1, 1), -1, 8, 0)

return a * b + 128 * (1 - b)

def place_in_square(img, r, h, w):
new_img = np.zeros((2 * r, 2 * r, 3), dtype=np.uint8)
new_img += 128
new_img[r - h // 2:r - h // 2 + img.shape[0], r - w // 2:r - w // 2 + img.shape[1]] = img

return new_img

def ReadImages(Path):
LabelList = list()
ImageCV = list()
classes = ["nonPdr", "pdr"]
scale = 224

# Get all subdirectories
FolderList = [f for f in os.listdir(Path) if not f.startswith('.')]

# Loop over each directory
for File in FolderList:
for index, Image in enumerate(os.listdir(os.path.join(Path, File))):
# Convert the path into a file
ImageCV.append(cv2.resize(cv2.imread(os.path.join(Path, File) + os.path.sep + Image), (224,224)))
#ImageCV[index]= np.array(ImageCV[index]) / 255.0
LabelList.append(classes.index(os.path.splitext(File)[0]))

ry, rx = estimate_radius(ImageCV[index])

resize_scale = scale / max(rx, ry)
w = min(int(rx * resize_scale * 2), scale * 2)
h = min(int(ry * resize_scale * 2), scale * 2)
img_resize = cv2.resize(ImageCV[index].copy(), (0, 0), fx=resize_scale, fy=resize_scale)

img_crop = crop_img(img_resize.copy(), h, w)

img_gbs = subtract_gaussian_blur(img_crop.copy())

img_remove_outer = remove_outer_circle(img_gbs.copy(), 0.9, scale)

ImageCV[index] = place_in_square(img_remove_outer.copy(), scale, h, w)

#cv2.imshow('image', ImageCV[index])
#cv2.waitKey(0)
#cv2.destroyAllWindows()

#ImageCV[index] = cv2.addWeighted(ImageCV[index],4, cv2.GaussianBlur(ImageCV[index],(0,0), 10), -4, 128)
#image_blurred = cv2.GaussianBlur(ImageCV[index], (0, 0), 100 / 30)
#ImageCV[index] = cv2.addWeighted(ImageCV[index], 4, image_blurred, -4, 128)

#ImageCV[index] = cv2.addWeighted (ImageCV[index],4,cv2.GaussianBlur(ImageCV[index] , (0,0) , 10) ,-4 ,128)
return ImageCV, LabelList

data, labels = ReadImages(TRAIN_DIR)

...它抛出此错误:

> return cv2.addWeighted(img, 4, gb_img, -4, 128) error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:663: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'



我做错什么了吗?我该如何解决?

更新

我发现了错误。 ImageCV [index]形状为(224、224、3),img和gb_img形状为(448、372、3)

最佳答案

我发现了错误。 ImageCV [index]形状为(224,224,3),img和gb_img形状为(448,372,3)。所以现在我必须使它们具有相同的形状。感谢您的帮助,GPhilo

关于python - OpenCV错误:该操作既不是 'array op array'也不是 'array op scalar',也不是函数 'scalar op array'中的 'cv::arithm_op',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58504964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com