Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve OpenCV and python VideoWriter resolution?

Tags:

python

opencv

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()
like image 370
Bárbara Duarte Avatar asked Sep 02 '15 12:09

Bárbara Duarte


People also ask

What is cv2 VideoWriter?

VideoWriter is the desired FPS of the output video file. We then have the width and height of output video. It's important that you set these values correctly, otherwise OpenCV will throw an error if you try to write a frame to file that has different dimensions than the ones supplied to cv2.


2 Answers

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.

like image 164
Ashalynd Avatar answered Oct 13 '22 07:10

Ashalynd


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 )
like image 25
user3666197 Avatar answered Oct 13 '22 06:10

user3666197