Message Boards Message Boards

Convert a batch of RGB images to Grayscale?

GROUPS:

I have a few RGB images in a folder which i would like to convert to grayscale. I have been able to convert them individually but i am unable to convert all the images in the folder at one go. I have tried to implement ColorConvert and ColorSpace but was unable to convert the complete set of images in one go. I need to convert approximately 2000 images so it is not possible to convert them individually. I request some help to achieve this task.

Thanks

POSTED BY: Pushkar D
Answer
2 months ago

Would something like this work for you?

pathToFiles = "/yourPath/..";
SetDirectory[pathToFiles];
newPath = FileNameJoin[{pathToFiles, "newPath"}];
If[!DirectoryQ[newPath],
    CreateDirectory[newPath];
];
files = FileNames["*.jpg", pathToFiles];
Export[FileNameJoin[{newPath, FileNameTake[#, -1]}], ColorConvert[Import[#], "Grayscale"]] & /@ files

referenced from here

POSTED BY: Mikayel Egibyan
Answer
2 months ago

had tried that already before posting the question, I am getting the following error when i do that

ColorConvert::ccvinput: C:\Users\Pushkar\Desktop\validate\test\car\00001.jpg should be a valid image, a color directive, a list of machine-sized real numbers of length up to 3, or a list of such objects.

I get the above error around 5 times and then i get the following error

General::stop: Further output of ColorConvert::ccvinput will be suppressed during this calculation.

after this error the notebook quits the kernel

POSTED BY: Pushkar D
Answer
2 months ago

Please retry.

POSTED BY: Mikayel Egibyan
Answer
2 months ago

Thanks, It worked. I tried something similar but it did not work when i tried.

I have another questions which i am trying to solve and would like your help if its not too much to ask.

I would like to take the same set of images, find out the HOG descriptors of the images and save the vectors in the form of a jpg for all the images.

   img=(*image*); 
 dims = ImageDimensions[img];
    dirs = ImageData[GradientOrientationFilter[img, 5]];
    magnitudes = ImageData[GradientFilter[img, 5]];
    orientations = 
        MapThread[#1 {-Sin[#2], Cos[#2]} &, {magnitudes, dirs}, 2];
    Show[img, 
      ListVectorPlot[
        MapIndexed[{{#2[[2]], dims[[2]] - #2[[1]]}, #1} &, 
          orientations, {2}], VectorColorFunction -> (Yellow &)]]

There are two problems with this, I would like to get an output of only the descriptors in the form of a jpg and also i would like to write a loop for 2000 images. I will start a new discussion regarding this but as you are actively solving all my questions, i wanted to ask you directly.

The source of my example is wolfram documentation , Please see the Applications section

http://reference.wolfram.com/language/ref/GradientOrientationFilter.html

I apologize for side tracking this discussion.

Thanks

POSTED BY: Pushkar D
Answer
2 months ago

You can make a function and use it:

pathToFiles = "/yourPath/..";
SetDirectory[pathToFiles];
newPath = FileNameJoin[{pathToFiles, "newPath"}];
If[!DirectoryQ[newPath],
    CreateDirectory[newPath];
];
files = FileNames["*.jpg", pathToFiles];
Export[FileNameJoin[{newPath, FileNameTake[#, -1]}], customFunc@Import[#]] & /@ files

Now, your customFunc

customFunc[img_Image] :=
    Block[{dims, dirs, magnitudes, orientations},
       dims = ImageDimensions[img];
       dirs = ImageData[GradientOrientationFilter[img, 5]];
       magnitudes = ImageData[GradientFilter[img, 5]];
       orientations = MapThread[#1 {-Sin[#2], Cos[#2]} &, {magnitudes, dirs}, 2];

        ListVectorPlot[
           MapIndexed[{{#2[[2]], dims[[2]] - #2[[1]]}, #1} &, orientations, {2}], 
           VectorColorFunction -> (Yellow &)
        ]
    ]
POSTED BY: Mikayel Egibyan
Answer
2 months ago

I have tried what you suggested but the code is just creating an empty folder and is exiting the kernel.

I forgot to mention to you that i do not want the image in the background, i just want the descriptors as jpg without the actual image in the background.

POSTED BY: Pushkar D
Answer
2 months ago

I have updated the reply not to include the original image.

Are you sure you are trying exactly what I posted?

POSTED BY: Mikayel Egibyan
Answer
2 months ago

I have tried the updated code and have done the exact same thing which you posted but i am still getting an empty folder without anything in it and the kernel is quitting within seconds of evaluating the notebook.

POSTED BY: Pushkar D
Answer
2 months ago

Can you please retry with the updated answer (there was no need for extra wrapper function) ?

You can first try with a smaller chunk to see how it works. For that just use files[[;;5]] instead of files at the last line of the code block (the line with Export)

POSTED BY: Mikayel Egibyan
Answer
2 months ago

Group Abstract Group Abstract