Problem using the imcrop function for batch image cropping!

5 vues (au cours des 30 derniers jours)
Zander
Zander le 23 Mar 2014
Modifié(e) : fewf le 28 Mar 2014
Hi all,
I wrote a little batch image cropping function based on one my friend wrote but I found it kept getting the same error (after I sorted out the other problems). It seems there is some problem with the imcrop function but I don't know what as I think it is being used in the right way. The aim of the function is to crop all .tif files in a specific folder by the same amount (they all have quite different file names). The images are all the same size to start with. Anyone have any thoughts? Any help would be most appreciated.
The function is:
function fun_im_crop_enlarged_test(basePath)
tifFiles = dir([basePath, '*.tif']);
cropPos = [20, 20, 40, 40];
for k = 1:length(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);

Réponse acceptée

Image Analyst
Image Analyst le 23 Mar 2014
Change to insert a whos and tell us what you see in the command window just before it bombs
data=imread(path1);
whos data
whos cropPos
cropIm = imcrop(data, cropPos);
You code is not very robust . For the first example there is no checking of the size of the image to make sure it has at least 40 rows and columns. Also, you're not using fullfile(), not checking to see if tifFiles is null before entering the loop, and not using try/catch. My code is always longer because of all the validation checks I put in there, but I know my code is bulletproof and can be rolled out to users across the globe without worry.
  1 commentaire
Zander
Zander le 23 Mar 2014
I am a fairly new Matlab user (or perhaps I should say BAD one!) so you're right about that!
I have kept it simple for the above reasons but also because I know that all the images in the folder will be bigger (I just adjust the crop positions for each batch of data as all the images are the same size). The crop positions are just arbitrary for the moment until the program works.
I don't know what fullfile() is so I will look into that. Thank you.
I've included the whos part and it gave this:
Name Size Bytes Class Attributes
data 900x1200x4 4320000 uint8
Name Size Bytes Class Attributes
cropPos 1x4 32 double
Looking at it... the image is AxBx4... should be AxBx3 right? Perhaps its because these images were modified in a different program first? (normally all my processing is done in Matlab but these needed some alterations of the axes).

Connectez-vous pour commenter.

Plus de réponses (1)

fewf
fewf le 28 Mar 2014
Modifié(e) : fewf le 28 Mar 2014
i'd like to share with you some of image cropping codes i am testing.it helps me crop tiff image or other image formats with a few clicks. please have a test and see whether it helps you or not. let me know if it is working. private void button1_Click(object sender, EventArgs e) { string fileName = "c:/Sample.png";
REImage reImage = REFile.OpenImageFile(fileName);
ImageProcessing.ApplyCrop(reImage, 80, 80, 500, 500);
REFile.SaveImageFile(reImage, "c:/reimage.png", new PNGEncoder());
}

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by