程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[invincible Python] this is what you should do to enhance the video quality

編輯:Python

principle

I don't know if you played this when you were a child ?

That's how the earliest animation was formed , I remember when I was a child, there were such small books for sale .

In fact, the principle of video is the same , A video is made up of many pictures , A picture is a frame . So we need to enhance the image quality of the video , Can be split into pairs

Each frame of the picture to operate , We introduced this operation earlier .

therefore , Video quality can be enhanced in three steps : Split -> Handle -> synthesis .

Split

In the first article, we talked about how to capture the video stream of the camera , And how to read and play the video . Either way , We all operate through frames . the

The so-called splitting here is to obtain each frame of the video stream we captured or read .

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

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

Handle

After getting a frame of the video , We're going to convert this frame into a picture in a format we can handle . When we introduced how to enhance the image quality

Hou , It uses ImageEnhance Related methods of this function , This function is PIL In the image processing library , So we have to read the pictures of each frame

become PIL Format that can be processed :

image = Image.fromarray(np.uint8(img1)) # convert to PIL Format that can be processed 

After reading the image , We can enhance the image quality , Here we still use the code mentioned in our last article :

# The image processing 
python Exchange of learning Q Group :906715085####
def img_enhance(image, brightness=1, color=1,contrast=1,sharpness=1):
# Brightness enhancement 
enh_bri = ImageEnhance.Brightness(image)
if brightness:
image = enh_bri.enhance(brightness)
# Chroma 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

Image processing finished , We need to synthesize each frame of image , To get our final video :

cap = cv2.VideoCapture(' Your video directory /xxx.mp4')
success, _ = cap.read()
# The resolution of the - Width 
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
# The resolution of the - Height 
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# The total number of frames 
frame_counter = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
video_writer = cv2.VideoWriter(' Output .mp4', cv2.VideoWriter_fourcc('M', 'P', '4', 'V'), 15, (width, height), True)
while success:
success, img1 = cap.read()
try:
image = Image.fromarray(np.uint8(img1)) # convert to PIL Format that can be processed 
img_enhanced = img_enhance(image, 2, 2, 2, 3)
video_writer.write(np.asarray(img_enhanced))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except:
break
cap.release()
video_writer.release()
cv2.destroyAllWindows()

What I read here is mp4 Video format , So when composing and writing video files , We need to use

cv2.VideoWriter_fourcc('M', 'P', '4', 'V') This format .

I didn't change the resolution of the picture here , Just get the resolution of the original video separately , Then when you write a video file , Pass in the original resolution as a parameter .

If you need to change the resolution of the video , You can use the following methods :

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

Use resize The method is ok :

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

summary

I won't send out the effect here , Interested friends can try . Only this and nothing more , Our video image quality enhancement function is basically realized , No code

complex , That's all it adds up to . however , If you want to deal with it to your satisfaction , We still need to work hard to adjust the parameters , To optimize . Even for each frame it may be

The parameters passed in are different , This requires you to study it slowly . If you think it's ok , Please don't be stingy of your praise .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved