记录一下这个小坑

MATLAB中指定路径读取图片和写图片有区别:【imread】读取文件的路径名和文件名需要通过一个【strcat】函数连接成一个字符串,而【imwrite】需要将两者分开。

下面分别展示一个例子。一个是把MNIST数据集中的图片写入到指定文件夹。

savePath = 'C:\Users\vector_Lu\Desktop\研一\模式识别\ML\Logistic\ex3\';
for i=1:10
    tempNum=rem(i,10);%求余数
    tempNumPath=strcat(savePath,num2str(tempNum));
    mkdir(tempNumPath);%important
    for j=1:10
        img=get_img(X(500*tempNum+j,:));
        img_Path=strcat(tempNumPath,'\');
        serial_num=num2str(j);
        img_name=strcat(num2str(tempNum),'-',num2str(serial_num),'.bmp');
        Path=strcat(img_Path,img_name);
        imwrite(img,Path);
    end
end

运行后的结果如下:

还有一个是从样本库中读取数字图片:

Path='C:\Users\vector_Lu\Desktop\研一\模式识别\作业\num\';
for i=0:9
    tempNum=i;%求余数
    tempNumPath=strcat(Path,num2str(tempNum));
    mkdir(tempNumPath);%important
    for j=1:10
        img_Path=strcat(tempNumPath,'\');
        img_name=strcat(num2str(tempNum),'-',num2str(j),'.bmp');
        img=imread(strcat(img_Path,img_name));
        imshow(img);
    end
end

再说一点:

【1】mkdir(tempNumPath);如果有错误提示没有权限,则这行代码很重要。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐