From: Chris Wood on 17 Feb 2010 19:09 Hello, I'm working on some image processing capabilities and having a bit of an issue as I try to convert everything into functions. Basically I want to make my .m files able to work as a function and accept any input image. Let's say this is my function call; function [improcess] = processing(InputImage) The handling of .jpeg and .gif is different, so I want to have a way to extract the filetype of InputImage and create "if INPUTNAME contains .gif...do this" and "if INPUTNAME contains .jpeg...do that" etc. Can I pull that filetype somehow? Thanks!
From: Nathan on 17 Feb 2010 19:17 On Feb 17, 4:09 pm, "Chris Wood" <cwballe...(a)gmail.com> wrote: > Hello, I'm working on some image processing capabilities and having a bit of an issue as I try to convert everything into functions. > > Basically I want to make my .m files able to work as a function and accept any input image. > > Let's say this is my function call; > function [improcess] = processing(InputImage) > > The handling of .jpeg and .gif is different, so I want to have a way to extract the filetype of InputImage and create "if INPUTNAME contains .gif...do this" and "if INPUTNAME contains .jpeg...do that" etc. Can I pull that filetype somehow? > > Thanks! If you pass in the filename, rather than the image itself, you can just test if the extension is .jpg or .gif. You can do a switch statement along the lines of: function [improcess] = processing(filename) dot = regexp(filename,'\.') switch(filename(dot+1:end)) case {'jpg','jpeg'} disp('jpg file') case {'gif'} disp('gif file') otherwise disp('error') end -Nathan
From: Walter Roberson on 17 Feb 2010 19:14 Chris Wood wrote: > Hello, I'm working on some image processing capabilities and having a > bit of an issue as I try to convert everything into functions. > > Basically I want to make my .m files able to work as a function and > accept any input image. > Let's say this is my function call; > function [improcess] = processing(InputImage) > > The handling of .jpeg and .gif is different, so I want to have a way to > extract the filetype of InputImage and create "if INPUTNAME contains > .gif...do this" and "if INPUTNAME contains .jpeg...do that" etc. Can I > pull that filetype somehow? knownexts = {'gif', 'jpeg', 'jpg', 'tif'}; lastdotpos = find(INPUTNAME, '.', 1, 'last'); fileext = INPUTNAME(lastdotpos+1:end); [tf, idx] = ismember(fileext, knownexts ); if ~tf %not a recognized extension else %you can switch on the value of idx or use fileext as a string or ... end
From: Chris Wood on 17 Feb 2010 21:07 Perfect, thanks! It took me no time to get that implemented once you pointed that out - just had to create if statements for filetype and RGB vs grayscale. Appreciate the help! Chris Nathan <ngreco32(a)gmail.com> wrote in message <71c44085-b51c-4065-afb7-c0a7c4a82bc0(a)m27g2000prl.googlegroups.com>... > On Feb 17, 4:09 pm, "Chris Wood" <cwballe...(a)gmail.com> wrote: > > Hello, I'm working on some image processing capabilities and having a bit of an issue as I try to convert everything into functions. > > > > Basically I want to make my .m files able to work as a function and accept any input image. > > > > Let's say this is my function call; > > function [improcess] = processing(InputImage) > > > > The handling of .jpeg and .gif is different, so I want to have a way to extract the filetype of InputImage and create "if INPUTNAME contains .gif...do this" and "if INPUTNAME contains .jpeg...do that" etc. Can I pull that filetype somehow? > > > > Thanks! > > If you pass in the filename, rather than the image itself, you can > just test if the extension is .jpg or .gif. > You can do a switch statement along the lines of: > > function [improcess] = processing(filename) > > dot = regexp(filename,'\.') > switch(filename(dot+1:end)) > case {'jpg','jpeg'} > disp('jpg file') > case {'gif'} > disp('gif file') > otherwise > disp('error') > end > > -Nathan
From: ImageAnalyst on 17 Feb 2010 22:42
Like others suggested, you can use fileparts() to extract the extension and then switch on it. On rare occasions, the extension is wrong and the file actually contains a different type of image. In that case, you can often check the first two bytes of the file using fread(). For example, if it's a BMP the bytes will be BM, if it's tiff they'll be II or MM, and if it's JPEG they'll FFD8 (in hex), etc. Some programs will handle incorrect extensions, but some will just throw an exception saying that the file doesn't agree with the extension. |