How to improve OpenCV and python VideoWriter resolution?
Question:
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()
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.
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 )
open cv screen recorded video is low quality when you play these files using unsupported codec player .
i was getting low quality video while i was playing on windows media player and after lot of analysis when i tried on VLC media Player .boom video quality is superb.
so just change media player to try if video is still poor quality.
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()
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.
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')
orcv2.VideoWriter_fourcc(*'MJPG)
forMJPG
.
""" # >>> 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 )
open cv screen recorded video is low quality when you play these files using unsupported codec player .
i was getting low quality video while i was playing on windows media player and after lot of analysis when i tried on VLC media Player .boom video quality is superb.
so just change media player to try if video is still poor quality.