Explore the impact of grayscale images on target detection test results-RGB to grayscale images, grayscale images expanded into three channels

1. Question: Directly input the converted grayscale image into the neural network and find

FileNotFoundError: [Errno 2] No such file or directory: 'home/JPEGImages/Image_24.jpg'

Second, analysis: gray-scale image has only one channel, while RGB has three channels

3. Solution: Copy the grayscale image of one channel twice to get the image required by the three channels

import cv2 
import os
from PIL import Image
import numpy as np
file_dir = 'home / JPEGImages2 /' # 'input folder /'
out_dir = 'home / JPEGImages /' # 'output folder /'
a = os.listdir (file_dir)
# img = Image.open ("home / img / Image_01.jpg") #Single image opening method

for i in a:
print (i)
I = Image.open (file_dir + i)
L = I.convert ('L' )
a = np.array (L) # converted to numpy array
image = np.expand_dims (a, axis = 2)
image = np.concatenate ((image, image, image), axis = -1) # axis = -1 Is the last channel
# image.save (out_dir + i) #Save picture (save image as image)
cv2.imwrite (out_dir + i, image) # Save picture (save array as image)
# print (image)

Reference link:

https://blog.csdn.net/zgcr654321/article/details/88015327 (Several ways to save the array as an image, he also has a reference to stackoverflow)

https://stackoom.com/question/3WAWn/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E5%B8%A6%E6%9C%89%E7 % 81% B0% E5% BA% A6% E5% 9B% BE% E5% 83% 8F% E7% 9A% 84% E9% A2% 84% E5% 85% 88% E8% AE% AD% E7% BB % 83% E7% 9A% 84% E7% A5% 9E% E7% BB% 8F% E7% BD% 91% E7% BB% 9C (After reading it, I did not choose to change the original network, but chose the current method)

Guess you like

Origin www.cnblogs.com/wywshtc/p/12699028.html