【问题标题】:Loading own dataset like MNIST images加载自己的数据集,如 MNIST 图像
【发布时间】:2018-08-06 08:30:32
【问题描述】:

我正在尝试加载我自己的手写图像并在我的 MNIST 模型中对其进行测试。但是,我需要先对图像进行预处理。我有 100 张图像,每 10 个数字中的 10 个保存在文件夹图像上。我收到错误,我不知道这段代码是否有效。有什么帮助吗?

import cv2
from os import listdir
from os.path import isfile, join
import numpy as np
from PIL import Image
from scipy import ndimage
import math
def loadImages(path):

    imagesList = listdir(path)
    loadedImages = []
    for image in imagesList:
        gray = cv2.imread("image", cv2.CV_LOAD_IMAGE_GRAYSCALE)
        gray = cv2.resize(255-gray, (28, 28))
        (thresh, gray) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
        while np.sum(gray[0]) == 0:
            gray = gray[1:]
        while np.sum(gray[:,0]) == 0:
            gray = np.delete(gray,0,1)
        while np.sum(gray[-1]) == 0:
            gray = gray[:-1]
        while np.sum(gray[:,-1]) == 0:
            gray = np.delete(gray,-1,1)
        rows,cols = gray.shape
        if rows > cols:
            factor = 20.0/rows
            rows = 20
            cols = int(round(cols*factor))
            gray = cv2.resize(gray, (cols,rows))
        else:
            factor = 20.0/cols
            cols = 20
            rows = int(round(rows*factor))
            gray = cv2.resize(gray, (cols, rows))
        colsPadding = (int(math.ceil((28-cols)/2.0)),int(math.floor((28-cols)/2.0)))
        rowsPadding = (int(math.ceil((28-rows)/2.0)),int(math.floor((28-rows)/2.0)))
        gray = np.lib.pad(gray,(rowsPadding,colsPadding),'constant')
        shiftx,shifty = getBestShift(gray)
        shifted = shift(gray,shiftx,shifty)
        gray = shifted
        loadedImages.append(gray)
    return loadedImages

def getBestShift(img):
    cy,cx = ndimage.measurements.center_of_mass(img)
    rows,cols = img.shape
    shiftx = np.round(cols/2.0-cx).astype(int)
    shifty = np.round(rows/2.0-cy).astype(int)
    return shiftx,shifty
path = 'images/'

def shift(img,sx,sy):
    rows,cols = img.shape
    M = np.float32([[1,0,sx],[0,1,sy]])
    shifted = cv2.warpAffine(img,M,(cols,rows))
    return shifted
imgs = loadImages(path)
rgb=np.array(imgs)

错误:AttributeError:模块'cv2.cv2'没有属性'CV_LOAD_IMAGE_GRAYSCALE'

【问题讨论】:

标签: python opencv mnist


【解决方案1】:

根据这个链接

Read image grayscale opencv 3.0.0-dev

所以,你需要在 python 2.4 中运行这个脚本(我猜)或者根据你当前的 python 版本更改标志名称

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2016-11-13
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多