Я работаю в 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) ошибка: OpenCV (4.1.0) C: \ projects \ opencv-python \ opencv \ modules \ core \ src \ arithm.cpp: 663: ошибка: (-209: размеры входных аргументов неmatch) Операция не является ни «array op array» (где массивы имеют одинаковый размер и одинаковое количество каналов), ни «array op scalar», ни «scalar op array» в функции «cv :: arithm_op»
Что-то я не так делаю? Как я могу это исправить?
ОБНОВЛЕНИЕ
Я нашел ошибку. Форма ImageCV [index] имеет вид (224, 224, 3), а форма img и gb_img равна (448, 372, 3)