Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the conversion between grayscale image and color image using python opencv

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use python opencv to achieve the conversion between grayscale images and color images. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Conversion between opencv grayscale image and color image

If the camera already has a 3-dimensional infrared image, it can be used without processing:

Import cv2cap = cv2.VideoCapture (0) ret, image_np = cap.read ()

Convert directly to a single-channel grayscale image to see if it can be used:

# if it is not followed by 0, it is the default color # the first method image = cv2.imread ('* * / timg4.jpg',0) # the second method # dst=cv2.cvtColor (src,cv2.COLOR_BGR2GRAY) detecotr = TOD () detecotr.detect (image) # or the video conversion mode is as follows: ret, image_np= cap.read () image_np=cv2.cvtColor (image_np,cv2.COLOR_BGR2GRAY)

The data obtained by the above two methods are not exactly the same, and the specific reasons have not been found on the Internet. Through the comparison of the read ndArray data, it is found that there are different data.

Observe the image that is converted directly to 1 channel:

Directly converted to a 1-channel grayscale image fed into the neural network is not good, because there is only 1-channel, must go through the following processing

Image_np=cv2.cvtColor (image_np,cv2.COLOR_GRAY2BGR)

The above code can get a 3-channel grayscale image, that is, copy the 1-channel grayscale image three times, and then you can feed it into the network:

Infrared image and grayscale image are not concepts obtained under the same classification standard.

The infrared image is the image formed by the infrared imaging equipment collecting the radiation of the target in the infrared band. this image can be either grayscale image or color image. By the same token, visible light images can be grayscale images or color images.

Grayscale images are relative to color images. The grayscale image has no color, and when the gray value changes from 0 to 255, the image changes from black to white.

Then convert the above 3-channel gray values into pseudo-color images, and observe the results:

Ret, image_np= cap.read () image_np=cv2.cvtColor (image_np,cv2.COLOR_BGR2GRAY) # image_np=cv2.cvtColor (image_np,cv2.COLOR_GRAY2BGR) image_np= cv2.applyColorMap (image_np, cv2.COLORMAP_JET)

There are different pseudo-color images for pseudo-color images, and each function is as follows:

# COLORMAP_AUTUMN = 0Magi # COLORMAP_BONE = 1Magi # COLORMAP_JET = 2Magi # COLORMAP_WINTER = 3Magi # COLORMAP_RAINBOW = 4Magi # COLORMAP_OCEAN = 5Magi # COLORMAP_SUMMER = 6Magi # COLORMAP_SPRING = 8Magi # COLORMAP_HSV = 9Meimei # COLORMAP_PINK = 10Magi # COLORMAP_HOT = 11 Note:

When using the model framework, if you are using tensorflow object detection API, you need to convert the grayscale image of 1 channel into 3 channels (the only channel is copied three times) for training and detection

Why not do it directly with the grayscale image of the user channel? does this improve performance?

No, because this does not make sense to improve performance and speed, it only affects the first layer of the convolutional neural network, and there is no effect on the amount of computation and the number of parameters in the subsequent layer. If you really consider performance and speed, it is more reliable to change the model directly.

Attached: python converts grayscale image to RGB color image from PIL import Imageimport ospath = r 'path for picture storage' newpath = r 'path for image storage' def RGBtoGray (path): files = os.listdir (path) for file in files: imgpath = path + /'+ file # print (imgpath) # im = Image.open (imgpath). Convert ('RGB') # resize Convert the image pixels to the pixel size you need img = im.resize Dirpath = newpath file_name, file_extend = os.path.splitext (f) dst = os.path.join (os.path.abspath (dirpath), file_name + '.jpg') img.save (dst) if _ _ name__ = = "_ _ main__": RGBtoGray (path) on "how to use python opencv to convert grayscale images into color images" ends here Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report