I have a grayscale image that has only 1 channel. I am using a code that expects all images to have three channels. How can I convert my image to a 3 channel image using some Linux commands?
Here is the error I get and I do not want to change the code, rather I want to change the image:
Traceback (most recent call last):
File "get_img_vec.py", line 22, in <module>
nd_arr = img2vec.get_vec(img)
File "../img_to_vec.py", line 43, in get_vec
h_x = self.model(image)
File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/models/resnet.py", line 139, in forward
File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 301, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 1, 224, 224] to have 3 channels, but got 1 channels instead
The image:
$ identify 10524.jpg
0524.jpg JPEG 1050x550 1050x550+0+0 8-bit PseudoClass 256c 80.7KB 0.000u 0:00.000
This is the image:
The following Python code works:
Install ImageMagick if needed:
(You can also install the latest release from source on Ubuntu 18.04 following this guide)
To separate image channels run:
(following this guide)
or to combine RGB image channels run:
(following this guide)
This will work for JPG too. Also you can try combining copies of the same gray-scale images with 3 different channels.