How to improve OpenCV and python VideoWriter resolution?

放肆的年华 提交于 2021-02-07 08:19:08

问题


I have a script which uses OpenCV and python and creates a video ( avi format ) from a set of png images.

The resolution of these images is good.

The problem is that the resolution of the resulting video is very low.

How can I improve the resolution?

Is the low resolution related to the images format?

CODE:

writer  = cv2.VideoWriter( "C:\Users\.../demo3_4.avi", -1, 1, ( width, height ) )
nFrames = 24

for i in range( 1, nFrames ):
    img   = cv2.imread( os.path.join( str( inf ), "colorraster%d.jpg"%i ) )
    writer.write( img )

cv2.destroyAllWindows()  
writer.release()

回答1:


According to the documentation, cv2.VideoWriter has fourcc parameter which specifies the codec, used to compress the frames. You are now specifying '-1' which means some default codec. I would suggest experimenting with different codecs from that list and see what gives the best result.

Update: To translate the codec into an int, the docs recommend this: CV_FOURCC('P','I','M','1') if you wanted to try codec PIM1.




回答2:


How to improve resolution?

Generate the output stream with a reasonable pixel-size frameSize and do not devastate the information quality ( you have stated above to have in the inputs ( in static pixmaps ) ) with a "cummulative product" of low FPS frames-per-second rate and too-lossy CODEC ( CV_FOURCC ).

SYNTAX:

>>> print cv2.VideoWriter.__doc__
VideoWriter( [ filename,
               fourcc,           # <--------- ref. below
               fps,              #            1 fps
               frameSize[,       #            73 * 59 px
               isColor  ]
               ]
              ) -> <VideoWriter object>

>>> print cv2.cv.FOURCC.__doc__
CV_FOURCC(c1, c2, c3, c4) -> int

>>> cv2.cv.FOURCC( *"XVID" )    1145656920
>>> cv2.cv.FOURCC( *"MJPG" )    1196444237
>>> cv2.cv.FOURCC( *"X264" )     875967064
>>> cv2.cv.FOURCC( *"DIB " )     541215044
>>> cv2.cv.FOURCC( *"WMV1" )     827739479
>>> cv2.cv.FOURCC( *"WMV2" )     844516695

Further readings:

FourCC is a 4-byte code used to specify the video codec. The list of available codes can be found in fourcc.org. It is platform dependent. Following codecs work fine: In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. ( XVID is more preferable. MJPG results in high size video. X264 gives very small size video ) In Windows: DIVX ( more to be tested and added )

FourCC code is passed as cv2.VideoWriter_fourcc('M','J','P','G') or cv2.VideoWriter_fourcc(*'MJPG) for MJPG.

"""                                                                 # >>> http://docs.opencv.org/master/dd/d43/tutorial_py_video_display.html#gsc.tab=0
fourcc  = cv2.cv.FOURCC(  *"DIB " )
video   = cv2.VideoWriter( 'ATC_LKPR_output.avi', fourcc, 30, size ) # fps = 30, size = ( 1024, 512 )


来源:https://stackoverflow.com/questions/32353246/how-to-improve-opencv-and-python-videowriter-resolution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!