Saturday, May 18, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 22443  / 3 Years ago, wed, june 16, 2021, 10:38:43

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:



black and white photo of a face


More From » convert

 Answers
0

The following Python code works:



import cv2
import numpy as np
img = cv2.imread('10524.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img2 = np.zeros_like(img)
img2[:,:,0] = gray
img2[:,:,1] = gray
img2[:,:,2] = gray
cv2.imwrite('10524.jpg', img2)

[#6785] Thursday, June 17, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ingwhin

Total Points: 332
Total Questions: 112
Total Answers: 115

Location: Burkina Faso
Member since Tue, Apr 26, 2022
2 Years ago
;