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 achieve Video quality Enhancement by Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to achieve video image quality enhancement in Python". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve video picture quality enhancement in Python" can help you solve your doubts.

Principle

I wonder if you used to play this when you were kids?

This is how the earliest animation was formed. I remember that there were such small books to sell when I was a child.

In fact, the principle of video is the same. A video is made up of many pictures, and a picture is a frame. So we need to enhance the quality of the video, which can be split into operations on each frame of the picture, which we described earlier.

Therefore, the method of video quality enhancement can be divided into three steps: split-> processing-> synthesis.

Implement step split

In our first article, we talked about how to capture the video stream of the camera, and how to read the video and play it. Either way, we operate through frames. So the so-called split here is to get every frame of the video stream that we captured or read.

Success, img1 = cap.read () # if the frame is read correctly, success is True if not success: break cv2.imshow ('img1', img1)

It's that simple, and we can get every frame of the video.

Deal with

After we get a frame of the video, we have to convert the frame into a format that we can handle. Earlier, when we introduced how to enhance the image quality, we used the relevant method of the ImageEnhance function, which is in the PIL image processing library, so we must read the image of each frame into a format that PIL can handle:

Image = Image.fromarray (np.uint8 (img1)) # converted to a format that PIL can handle

After reading the image, we can enhance the image quality, using the code we talked about in the last article:

# Image processing def img_enhance (image, brightness=1, color=1,contrast=1) Sharpness=1): # Luminance enhancement enh_bri = ImageEnhance.Brightness (image) if brightness: image = enh_bri.enhance (brightness) # chromaticity enhancement enh_col = ImageEnhance.Color (image) if color: image = enh_col.enhance (color) # contrast enhancement enh_con = ImageEnhance.Contrast (image) if contrast: image = enh_con.enhance (contrast ) # sharpness enhancement enh_sha = ImageEnhance.Sharpness (image) if sharpness: image = enh_sha.enhance (sharpness) return image synthesis

After image processing, we need to synthesize each frame of the image to get our final video:

Cap = cv2.VideoCapture ('your video directory / xxx.mp4') success, _ = cap.read () # Resolution-width width = int (cap.get (cv2.CAP_PROP_FRAME_WIDTH)) # Resolution-height height = int (cap.get (cv2.CAP_PROP_FRAME_HEIGHT)) # Total frames frame_counter = int (cap.get (cv2.CAP_PROP_FRAME_COUNT)) video_writer = cv2.VideoWriter (' output .mp4' Cv2.VideoWriter_fourcc ('masked,' printed, '44th,' V'), 15, (width, height), True) while success: success, img1 = cap.read () try: image = Image.fromarray (np.uint8 (img1)) # converted to the format img_enhanced = img_enhance (image, 2, 2, 2) that PIL can handle 3) video_writer.write (np.asarray (img_enhanced)) if cv2.waitKey (1) & 0xFF = = ord ('q'): break except: breakcap.release () video_writer.release () cv2.destroyAllWindows ()

What I read here is a video in mp4 format, so when composing and writing video files, we need to use the

Cv2.VideoWriter_fourcc ('masking,' playing, '4percent,' V') this format.

I did not modify the resolution of the image here, just get the resolution of the original video separately, and then input the original resolution as a parameter when writing to the video file.

If you need to modify the resolution of the video, you can use the following ways:

Cv2.resize (src, dsize [, dst [, fx [, fy [, interpolation])

Just use the resize method:

Resized = cv2.resize (img, (width, height), interpolation = cv2.INTER_AREA) effect

Let's first take a look at the pre-processing video:

After processing (I deal with it more casually here, and the parameters are written at random), the video looks like this:

After reading this, the article "how to enhance video picture quality with Python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.

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