UKC

Matlab help needed... again!

New Topic
This topic has been archived, and won't accept reply postings.
 crayefish 23 Mar 2014
Hi,

For those fellow geek climbers who know their Matlab...

I've written a small batch processing function but keep getting the same error and can't figure out why. It seems there is some problem with the imcrop function but I don't know what! Anyone have any thoughts?

The script is:

function fun_im_crop_enlarged_test(basePath)

tifFiles = dir([basePath, '*.tif']);
cropPos = [20, 20, 40, 40];

for k = 1ength(tifFiles)
path1 = [basePath, tifFiles(k).name];
data=imread(path1);
cropIm = imcrop(data, cropPos);
path2 = char([basePath,'_crop_', tifFiles(k).name]);
imwrite(cropIm, path2);
end

The error I am getting is:

??? Error using ==> imcrop>checkCData at 381
Invalid input image.

Error in ==> imcrop>parseInputs at 252
checkCData(a);

Error in ==> imcrop at 94
[x,y,a,cm,spatial_rect,h_image,placement_cancelled] = parseInputs(varargin{:});

Error in ==> fun_im_crop_enlarged_test at 24
cropIm = imcrop(data, cropPos);
 Hairy Pete 23 Mar 2014
In reply to crayefish: Are all of the tif files valid? Could there be some that have zero length and are just file names?
 Jack B 23 Mar 2014
In reply to crayefish:
There is nothing obviously wrong. I assume the forum software ate your slashes? otherwise you probably want a slash, in quotes, between basePath and tifFiles(k).name. Either that or you need to make sure basePath ends in a slash. If that was your problem you'd get File "foo" does not exist. though.

Otherwise, you want to try debugging your script. If you aren't used to using the debugger, it is well worth investing the time to learn it. It's great.

Type dbstop if error into the command window, then run your function again. This time when it errors the prompt will change from ">>" to "K>>", this tells you you're in debug mode. Mouse over variables in your function (especially data) and matlab will tell you what is in them. Probably data contains something other than it should. A valid AxB pixel image should look like an AxBx3 matrix. You can type any function you want into the command line, to help understand what's wrong (such as imshow(data)). To quit debug mode, type dbquit. To stop it going into debug mode when an error happens, use dbclear all.

P.S. what version of matlab do you use? it should say in the title bar. There are ways of doing this stuff by clicking buttons, if the commands are hard to remember, but they changed a few version back.
Post edited at 20:40
 itsThere 23 Mar 2014
In reply to crayefish:
Try it for one image first, no for loop. Not used matlab for a while but here is my comments. They may be of no help and I cant see the rest of your code anyway. Which wont help.

in this section
"path1 = [basePath, tifFiles(k).name]; "
k is a number and the file name will be a char or a sting(I dont know), bad things will happen anyway. Could be same error as below, why not write it all into one line.

but i think i may have misunderstood your code there.

Matlab has a problem with your input to imcrop(), comment out cropPos and stick it in imcrop(image, [20 20 40 40]); matlab may be seeing it as imcrop(image, 20, 20, 40, 40); hency the varargin in the error.

Why is path1 not a char and path2 is?

Oh and do what Jack B said
Post edited at 21:32
 Jack B 23 Mar 2014
In reply to itsThere:

The suggestion of running it without the loops is a good one. Another trick is to add display(path1) right after path1 is assigned, and leave the loop in. That will tell you which TIFF file it is trying to process when it errors.

> "path1 = [basePath, tifFiles(k).name]; "
> k is a number and the file name will be a char or a sting(I dont know), bad things will happen anyway. Could be same error as below, why not write it all into one line.

This is a bit of an oddity of MATLAB syntax, but perfectly correct. tifFiles is an array of structures returned by dir(). k is a number used to index into the array and pick out an individual struct, then .name takes the name field of that struct. So it returns a char array, as it should. (In MATLAB the terms "char array" and "string" are used interchangeably. There is no real string class as such, but it doesn't matter because pretty much anything you might want to do with a string you can do with an array anyway).

> but i think i may have misunderstood your code there.

> Matlab has a problem with your input to imcrop(), comment out cropPos and stick it in imcrop(image, [20 20 40 40]); matlab may be seeing it as imcrop(image, 20, 20, 40, 40); hency the varargin in the error.

MATLAB won't do this. An array is always treated as a single argument. You might be able to pass multiple args by abusing the cell class, but not an array of doubles.

> Why is path1 not a char and path2 is?

This I did not notice. The cast to char shouldn't be necessary, as MATLAB will automatically (and silently) cast something to char if it's concatenated with a char. And all of those arguments should already be chars. The cast shouldn't be harmful though.

Aside: in MATLAB, the following are all true:
'a'==char(97)
double('a')==97
'a'==97
Yes, that last one is retarded. Even more fun:
'abc'=='abc'
evaluates to
[true true true]
which is why you should always use strcmp().
OP crayefish 24 Mar 2014
In reply to Hairy Pete:

Thanks for all your help guys! Hairy Pete was on the right track... it turns out it was as stupid as problems with the tiff files. After some previous operations they had been converted to m x n x 4 matrices (RGBa). The code was fine (though as many have pointed out... a bit messy!).

Took me bloody ages to try and remove the alpha channel but eventually someone pointed out to use data = data(: , : , 1:3). Felt like a total idiot as I'd previously tried that only using one component (hence why I got some wierd gray scale images!)

Thanks again for everyone's ideas.
OP crayefish 24 Mar 2014
In reply to Jack B:

Yeah actually I've always been in the habit of putting shashes in the basepath rather than in the code. Probably slower for me, but its force of habit as it's how my friend wrote all the code I used when I was first processing my data.

New Topic
This topic has been archived, and won't accept reply postings.
Loading Notifications...