Turn on thread page Beta
    • Thread Starter
    Offline

    10
    ReputationRep:
    Hi

    does anyone know if its possible to convert 500 images to grayscale at once rather than one at a time ?

    at the moment im converting the image one by one using

    imshow(I(:,:,1))

    where I is the picture

    rep to the first person that can help me
    Offline

    0
    ReputationRep:
    i hate matlab...its the worst program/module iv ever had to do
    • Thread Starter
    Offline

    10
    ReputationRep:
    (Original post by fidel69)
    i hate matlab...its the worst program/module iv ever had to do
    i wish it was as easy as a module :mad: at least that way a could ask sumone else

    its part of ma final year project and that means am all alone :banghead:
    Offline

    2
    ReputationRep:
    I don't know much about Matlab, but I seriously doubt that you can do it at once in any programming/functional language. I'm quite sure that opening files, modifying them in some way and then saving back to hard disk is a linear process. You could open various files at once, but it'd still save them in a linear fashion (save one first, then next one, then next one, etc).
    Offline

    15
    ReputationRep:
    what do you mean by conversion; imshow is just showing it? do you mean you want to see them all at the same time?

    as I is a three-dimensional matrix, you can just load all your 500 images into four-dimensional matrix. or convert them (search "RGB to gray" in Help (probaby the command is rgb2gray)), and load them into a three-dimensional matrix.

    sorry I'm not really specific but it isn't really clear to me what you want to do!

    oh btw, I think matlab supports multitif files, so you could probably save your 500 images as one file.



    A faster way to do it, not using matlab, would be to load your images into ImageJ ( "import imageseries" ), convert the stack from RGB to gray level, and save them either as a multitif or as 500 gray images ( "export imageseries" ). that would probably take less than two minutes.

    (Of course if you do this often then you might as well use matlab.)
    Offline

    0
    ReputationRep:
    I presume you're looking for some sort of for loop construct?
    • Thread Starter
    Offline

    10
    ReputationRep:
    (Original post by llys)
    what do you mean by conversion; imshow is just showing it? do you mean you want to see them all at the same time?

    as I is a three-dimensional matrix, you can just load all your 500 images into four-dimensional matrix. or convert them (search "RGB to gray" in Help), and load them into a three-dimensional matrix.

    sorry I'm not really specific but it isn't really clear to me what you want to do!

    yes and no :o:

    basiclly i have 500 images that i took from two cameras and there in colour all i need to do is make them black and white using matlab

    i dont need to see them on the screen at the same time

    also im not too sure on matlab how would u load all my images into a 4d matrix?

    imshow(I(:,:,1)) the bit in bold is what code im using to turn the images in to black and white
    • Thread Starter
    Offline

    10
    ReputationRep:
    (Original post by laser)
    I presume you're looking for some sort of for loop construct?
    yeah

    imshow(I(:,:,1)) is the code am using for one

    how would i do it so it would load the 500 images at once
    Offline

    15
    ReputationRep:
    Search in Matlab Help, there is something about how you can load movies into matlab. A movie is basically a 4-dimensional matrix, so from the explanation there you should be able to extrapolate for your situation. Then load them into a 4D. What are the file names? If they are similar, just have different number extensions or something this is definitely possible, but I'd have to get back to you tomorrow because I don't have matlab on this netbook.

    Once you have them all, you can convert them all with something like

    for n=1:500
    G(:,:,n)=rgb2gray(I(:,:,:,n));
    end

    then save G, or use it for further analysis etc.. Do you have imageJ ?
    Offline

    15
    ReputationRep:
    (Original post by azzi)
    imshow(I(:,:,1)) the bit in bold is what code im using to turn the images in to black and white
    this shows you a gray image because it shows you only the red channel of the image, but if you need all channel information this is not really what you want, I guess.
    • Thread Starter
    Offline

    10
    ReputationRep:
    (Original post by llys)
    Search in Matlab Help, there is something about how you can load movies into matlab. A movie is basically a 4-dimensional matrix, so from the explanation there you should be able to extrapolate for your situation. Then load them into a 4D. What are the file names? If they are similar, just have different number extensions or something this is definitely possible, but I'd have to get back to you tomorrow because I don't have matlab on this netbook.

    Once you have them all, you can convert them all with something like

    for n=1:500
    G(:,:,n)=rgb2gray(I(:,:,:,n));
    end

    then save G, or use it for further analysis etc.. Do you have imageJ ?

    image j ?

    what i have at the moment is 500 pics with the variable name (file name)
    cam2_loop1.bmp

    not sure how i would do the rest
    Offline

    15
    ReputationRep:
    (Original post by azzi)
    image j ?

    what i have at the moment is 500 pics with the variable name (file name)
    cam2_loop1.bmp

    not sure how i would do the rest
    OK I will try this tomorrow with matlab and let you know.

    ImageJ is an image processing program that is free, very easy to use and faster than matlab for routine things like this. Do you want to work with the converted images further in matlab once you've converted them?

    If not, I would suggest you download ImageJ. I edited my first post in this thread (when you weren't looking), so please check above; it explains how you can do this very fast with ImageJ.
    • Thread Starter
    Offline

    10
    ReputationRep:
    (Original post by llys)
    OK I will try this tomorrow with matlab and let you know.

    ImageJ is an image processing program that is free, very easy to use and faster than matlab for routine things like this. Do you want to work with the converted images further in matlab once you've converted them?

    If not, I would suggest you download ImageJ. I edited my first post in this thread (when you weren't looking), so please check above; it explains how you can do this very fast with ImageJ.

    Cheers mate your a legend

    i downloaded image j and have used it to convert it

    but i have to use matlab for it
    Offline

    15
    ReputationRep:
    (Original post by azzi)
    Cheers mate your a legend

    i downloaded image j and have used it to convert it

    but i have to use matlab for it

    OK, I'm sure there is an easier or faster way to do it, but this should work:


    %save this .m file in a directory with your image series
    %create an array of filenames that make up the image sequence
    dirOutput = dir('cam2_loop*.bmp'); %I'm assuming your images are called cam2_loop1, cam2_loop2, etc.
    fileNames = {dirOutput.name}';
    numFrames = numel(fileNames);

    %load first file to preallocate space for the 500
    I = imread(fileNames{1});
    %preallocate the array
    sequence = zeros([size(I) numFrames],class(I));
    sequence(:,:,:,1) = I;

    % Create image sequence array
    for p = 2:numFrames
    sequence(:,:,:,p) = imread(fileNames{p});
    end

    %sequence now contains all your (RGB) images
    %convert to grayscale images
    for n=1:numFrames
    G(:,:,n)=rgb2gray(sequence(:,:,: ,n));
    %G now contains converted, grayscale images

    %for .gif animation (just for fun)
    anim(:,:,1,n)=G(:,:,n);

    %save to originalName_gray_*.bmp, where * goes from 001 to numFrames
    %numbering of the new files
    N=48+n;
    num=[floor((N-48)/100)+48, floor((N-floor((N-48)/100)-48)/10)+48, ((N-floor((N-48)/100)-floor((N-floor((N-48)/100)-48)/10)-48))+48];
    %to get the filename you want, get the numbers with uint8('yourName'),
    %and then put num in the place where the file number should be,
    %right now the new names are cam2_loop_gray_001 to 500 etc.
    newName=char([99 97 109 50 95 108 111 111 112 95 103 114 97 121 95 num 46 98 109 112]);
    imwrite(G(:,:,n),newName);
    end

    %or save a gif animation
    imwrite(anim,'animation.gif')
    %(you can also write .avi or .mov files; check matlab help



    NOTE: this worked when I tried it, but run a test first on only 3 of your images, not all 500! ...
    (btw the first part of this you could find in Matlab Help, "working with image sequences".)

    Oh and if this is for a project, please make sure you understand what the code does (execute it one by one, then you can figure out what each part does).
    • Thread Starter
    Offline

    10
    ReputationRep:
    (Original post by llys)
    OK, I'm sure there is an easier or faster way to do it, but this should work:


    %save this .m file in a directory with your image series
    %create an array of filenames that make up the image sequence
    dirOutput = dir('cam2_loop*.bmp'); %I'm assuming your images are called cam2_loop1, cam2_loop2, etc.
    fileNames = {dirOutput.name}';
    numFrames = numel(fileNames);

    %load first file to preallocate space for the 500
    I = imread(fileNames{1});
    %preallocate the array
    sequence = zeros([size(I) numFrames],class(I));
    sequence(:,:,:,1) = I;

    % Create image sequence array
    for p = 2:numFrames
    sequence(:,:,:,p) = imread(fileNames{p});
    end

    %sequence now contains all your (RGB) images
    %convert to grayscale images
    for n=1:numFrames
    G(:,:,n)=rgb2gray(sequence(:,:,: ,n));
    %G now contains converted, grayscale images

    %for .gif animation (just for fun)
    anim(:,:,1,n)=G(:,:,n);

    %save to originalName_gray_*.bmp, where * goes from 001 to numFrames
    %numbering of the new files
    N=48+n;
    num=[floor((N-48)/100)+48, floor((N-floor((N-48)/100)-48)/10)+48, ((N-floor((N-48)/100)-floor((N-floor((N-48)/100)-48)/10)-48))+48];
    %to get the filename you want, get the numbers with uint8('yourName'),
    %and then put num in the place where the file number should be,
    %right now the new names are cam2_loop_gray_001 to 500 etc.
    newName=char([99 97 109 50 95 108 111 111 112 95 103 114 97 121 95 num 46 98 109 112]);
    imwrite(G(:,:,n),newName);
    end

    %or save a gif animation
    imwrite(anim,'animation.gif')
    %(you can also write .avi or .mov files; check matlab help



    NOTE: this worked when I tried it, but run a test first on only 3 of your images, not all 500! ...
    (btw the first part of this you could find in Matlab Help, "working with image sequences".)

    Oh and if this is for a project, please make sure you understand what the code does (execute it one by one, then you can figure out what each part does).

    cheers for that i tried it but im getting the following error

    Error in ==> Untitled at 13
    sequence(:,:,:,p) = imread(fileNames{p});

    any ideas?
    Offline

    15
    ReputationRep:
    (Original post by azzi)
    cheers for that i tried it but im getting the following error

    Error in ==> Untitled at 13
    sequence(:,:,:,p) = imread(fileNames{p});

    any ideas?
    Does it say anything else? Like "Index exceeds matrix dimensions"? That normally gives you a clue to what is wrong.


    Anyway, after you get this error, type

    fileNames

    in the command window. This should show you the filenames in this array - if you have only one filename, then that would cause such an error (because the loop to read filenames starts with p=2).
    If this is the problem and there is only one filename in fileNames, make sure that there are more than one image file in the current directory (at least two). If there already are more than two, check if this is true:
    dirOutput = dir('cam2_loop*.bmp'); %I'm assuming your images are called cam2_loop1, cam2_loop2, etc.
    This ONLY finds .bmp files that are called cam2_loop plus something. It does not find any others.
    If your images are called completely different names, change to
    dirOutput = dir('*.bmp');
    That will find all the .bmp files in the current directory, no matter what they are called.
    If your images are called different names you might want to change the name you save them under after conversion.



    Another problem could be if all your images have a different size (widthxheight). In that case you have to change the script.
    sequence(:,:,:,p) = imread(fileNames{p});
    assumes all images are the same size (because of the :,: ). And before that,
    sequence = zeros([size(I) numFrames],class(I))
    gave sequence the dimensions of the first image.




    Finally, if you type

    whos

    in the command window, it should also show you that "sequence" is a 4-D uint8 array. this was not possible in some older versions of matlab, but it doesn't seem to be your problem because you managed to load the first image into it OK.
    • Thread Starter
    Offline

    10
    ReputationRep:
    (Original post by llys)
    Does it say anything else? Like "Index exceeds matrix dimensions"? That normally gives you a clue to what is wrong.


    Anyway, after you get this error, type

    fileNames

    in the command window. This should show you the filenames in this array - if you have only one filename, then that would cause such an error (because the loop to read filenames starts with p=2).
    If this is the problem and there is only one filename in fileNames, make sure that there are more than one image file in the current directory (at least two). If there already are more than two, check if this is true:
    dirOutput = dir('cam2_loop*.bmp'); %I'm assuming your images are called cam2_loop1, cam2_loop2, etc.
    This ONLY finds .bmp files that are called cam2_loop plus something. It does not find any others.
    If your images are called completely different names, change to
    dirOutput = dir('*.bmp');
    That will find all the .bmp files in the current directory, no matter what they are called.
    If your images are called different names you might want to change the name you save them under after conversion.



    Another problem could be if all your images have a different size (widthxheight). In that case you have to change the script.
    sequence(:,:,:,p) = imread(fileNames{p});
    assumes all images are the same size (because of the :,: ). And before that,
    sequence = zeros([size(I) numFrames],class(I))
    gave sequence the dimensions of the first image.




    Finally, if you type

    whos

    in the command window, it should also show you that "sequence" is a 4-D uint8 array. this was not possible in some older versions of matlab, but it doesn't seem to be your problem because you managed to load the first image into it OK.

    me agian :o:

    im still getting a error

    error in ===> matlab at 35
    imwrite(G(:,:,n),newName);


    although a cant understand why because its changing the first 9 images to grayscale but not the others

    i tried the
    FileNames

    and it shows all the images

    i even tried

    whos
    and it is 4-D as you said it would
    Offline

    15
    ReputationRep:
    (Original post by azzi)
    me agian :o:

    im still getting a error

    error in ===> matlab at 35
    imwrite(G(:,:,n),newName);

    Hmmm. What kind of error is it? (It should say something more than error in ===> matlab at 35)

    Do you have more than 999 images?

    Could you type

    n
    num
    newName
    size(sequence)

    to see what that gives you?
    Offline

    15
    ReputationRep:
    Replace

    the respective section in the loop by

    %save to originalName_gray_*.bmp, where * goes from 001 to numFrames
    %numbering of the new files
    N=48+n;
    num=[floor((N-48)/100)+48, floor((N-(floor((N)/100))*100-48)/10)+48, N-floor((N-48)/100)*100-floor((N-(floor((N)/100))*100-48)/10)*10];
    %to get the filename you want, get the numbers with uint8('yourName'),
    %and then put num in the place where the file number should be,
    %right now the new names are cam2_loop_gray_001 to 500 etc.
    newName=char([99 97 109 50 95 108 111 111 112 95 103 114 97 121 95 num 46 98 109 112]);
    imwrite(G(:,:,n),newName);

    and check if it works now? (You'll have to delete the files that are gray before you do this.)
    • Thread Starter
    Offline

    10
    ReputationRep:
    (Original post by llys)
    OK I think I know what it is. (Probably newName is not proper if num is a vector as it would be if n>10)

    COuld you try typing in the command window:
    num=[49 49 49];
    newName=char([99 97 109 50 95 108 111 111 112 95 103 114 97 121 95 num 46 98 109 112])


    just to see if that gives you an error / what it says?
    cheers mate

    am not getting an error typing the above in

    newName=cam2_loop_gray111.bmp


    by the way what is it ive just typed ?
 
 
 
Reply
Submit reply
Turn on thread page Beta
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: January 20, 2010
Poll
Which accompaniment is best?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.