2017-07-28 64 views
0

我显然无法区分不同类型的向量和数组以及像string,cellstr,char等命令。我的代码在另一个网络上,但基本上,我得到一个错误,说我的imread语句中的参数必须是字符矢量。这个参数是一个1x30的文件名数组,因为我使用了iscell命令,并且它返回了1,所以我尝试了上面列出的命令的几个组合,并且一直在阅读我能够做的所有事情,但无法确定如何更改将1x30单元格数组转换为字符向量,以便imread语句可以工作。文件名是从文件夹(使用uigetfile)作为757-01.bmp,757-02.bmp ... 757-30.bmp读入的。我想我需要让他们'757-01.bmp','757-02.bmp'...'757-30.bmp',并可能成为一个30x1矢量副1x30?或者,这对代码将在下一次遇到的for循环无关紧要。谢谢你的任何援助...在MatLab中,如何从单元格数组创建一个字符向量?

[imagefiles,file_path]=uigetfile({'*.jpeg;*.jpg;*.bmp;*.tif;*.tiff;*.png;*.gif','Image Files (JPEG, BMP, TIFF, PNG and GIF)'},'Select Images','multiselect','on'); 
imagefiles = cellstr(imagefiles); 
imagefiles = sort(imagefiles); 
nfiles = length(imagefiles); 

r = inputdlg('At what pixel row do you want to start the cropping?  .','Row'); 
r = str2num(r{l}); 

c = inputdlg('At what pixel column do you want to start the cropping (Must be > 0)?  .','Column'); 
c = str2num(c{l}); 

h = inputdlg('How many pixel rows do you want to include in the crop?  .','Height'); 
h = str2num(h{l}); 

w = inputdlg('How many pixel columns do you want to include in the crop?  .','Width'); 
w = str2num(w{l}); 

factor = inputdlg('By what real number factor do you want to enlarge the cropped image?  .','Factor'); 
factor = str2num(factor{l}); 

outdimR = h * factor; 
outdimC = w * factor; 

for loop=l:nfiles 
    filename = imagefiles(loop); 
    [mybmp, map] = imread(filename); 
    myimage = ind2rgb(mybmp,map); 
    crop = myimage(r:r+h-1, c:c+w-1, :); 
    imwrite(crop, sprintf('crop757-%03i.bmp',loop)); 
end 
+1

你可以发表你的代码如何使用imread?这可能是你的错误所在。 – Flynn

+2

请发表[mcve]。 – beaker

+0

代码是裁剪图像: – bpfreefly

回答

0

关于你原来的问题:

在MATLAB中,我怎么可以创建一个单元阵列的字符向量?

可以使用电池访问操作数({}),以获得您的单元格中字符串作为@本 - 沃伊特指出,或者包裹char()在你的声明(我和Ben的建议去)。

关于你提到的后续问题:说文件“757-01.bmp”根本不存在imread

它的错误了。在imagefiles数组中,30个值中的第一个是757-01.bmp(不包括引号),但我不知道MatLab是否将引号放在文件名的周围意味着它在数组中查找带引号的值。

这听起来像你的文件是在另一个目录,而不是你正在运行你的代码。

使用fullfile创建完整的文件名以使用文件的绝对路径而不是相对路径(这不适用于此)。

假设您的文件位于路径~/bpfreefly/images/中。

然后你就可以改变你的代码是这样的:

imgPath = '~/bpfreefly/images/' 
for loop=l:nfiles 
    filename = fullfile(imgPath,imagefiles{loop}); 
    [mybmp, map] = imread(filename); 
    myimage = ind2rgb(mybmp,map); 
    crop = myimage(r:r+h-1, c:c+w-1, :); 
    imwrite(crop, sprintf('crop757-%03i.bmp',loop)); 
end 

顺便说一句,你可以从uigetfile的第二输出参数的路径名。

+0

谢谢大家! – bpfreefly

相关问题