【发布时间】:2021-01-21 20:16:28
【问题描述】:
我目前正在使用此代码:
def bundle_contour(image):
src_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
crop_img = cv2.resize(src_gray, (196, 196))
v = np.median(crop_img)
lower = int(max(0, (1.0 - 0.33) * v))
upper = int(min(255, (1.0 + 0.33) * v))
filter = cv2.bilateralFilter(src_gray, 9, 75, 75)
filter = filter.astype(np.uint8)
edged = cv2.Canny(filter, lower, upper)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
thresh = cv2.dilate(edged, kernel, iterations=2)
# Find contours in threshold image, then grab the largest one
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
# Find the extreme points
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
# crop new image out of the original image using the four extreme points (left, right, top, bottom)
new_image = image[extTop[1]:extBot[1], extLeft[0]:extRight[0]]
return new_image
def load_data(filename):
image = cv2.imread(filename)
# crop the bundle and ignore the unnecessary rest part of the image
cnt_image = bundle_contour(image)
# resize image
resized_image = cv2.resize(cnt_image, dsize=(196, 196), interpolation=cv2.INTER_CUBIC)
# normalize values
norm_image = resized_image / 255.
reshaped_img = norm_image.reshape(196,196,1)
return reshaped_img
def compose_dataset(df):
data = []
labels = []
for img_path, label in df.values:
data.append(load_data(img_path))
labels.append(label)
return np.array(data), np.array(labels)
X_train, y_train = compose_dataset(train_df)
print('Train data shape: {}, Labels shape: {}'.format(X_train.shape, y_train.shape))
我收到此错误:
ValueError: 无法将大小为 115248 的数组重新整形为 (196,196,1)。
如果我将代码更改为norm_image.reshape(196,196,-1),我的输出是:
Train data shape: (134, 196, 196, 3), Labels shape: (134,)
如何将形状设为 (196,196,1) 以输入到我的 CNN??
【问题讨论】:
-
您的 CNN 是否使用灰度图像?然后,只需将您的图像转换为灰度。您当前输入的尺寸似乎为
(196, 196, 3)(自196 * 196 * 3 = 115248)。这里的错误信息非常准确。