7

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()
user3666197
  • 1
  • 6
  • 50
  • 92
Bárbara Duarte
  • 529
  • 3
  • 9
  • 17
  • What are the dimensions of your images? Did you read the [class description in the manual](http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videowriter-videowriter)? What did you set the resolution of your VideoWriter (5th number at instantiation)? Could you show some code? – ljetibo Sep 02 '15 at 12:35
  • 2
    Can you provide some code? Otherwise it wont be easy to help you... – rdgfuentes Sep 02 '15 at 12:35
  • ljetibo, the images have 73*59 pixels. – Bárbara Duarte Sep 02 '15 at 12:53
  • Does cv2.VideoWriter have some options where you can set the desired resolution? – Ashalynd Sep 02 '15 at 12:53
  • Try changing fourcc parameter of cv2.VideoWriter, instead of providing the default. – Ashalynd Sep 02 '15 at 12:55

3 Answers3

4

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.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
  • I am searching in fourcc documentation but I don't understand the codes used. I have '-1', default code, but I didn't see this code in list http://www.fourcc.org/codecs.php. – Bárbara Duarte Sep 02 '15 at 13:10
  • It is not there because it's just "the default". – Ashalynd Sep 02 '15 at 14:00
  • I try what you said but python doesn't recognize CV_FOURCC('P','I','M','1') in my code. – Bárbara Duarte Sep 04 '15 at 09:47
  • well, for python it should be in slighlty different form I guess. Just found it mentioned in another answer (http://stackoverflow.com/questions/15584608/python-opencv2-cv2-cv-fourcc-not-working-with-videowriterb) : cv2.VideoWriter_fourcc('P','I','M','1') – Ashalynd Sep 04 '15 at 09:58
  • Ok. I get it!! But know I try several codecs but the video never works, juts works with '-1'. What is the best codec for windows media player? – Bárbara Duarte Sep 04 '15 at 12:33
  • Yes. I see in some comments to try with lower letters like this, cv2.VideoWriter_fourcc('p','i','m','1') and I try also but without success. – Bárbara Duarte Sep 04 '15 at 12:59
  • Could you check with some other tool whether you produced correct divx (assuming you used divx codec)? – Ashalynd Sep 04 '15 at 14:25
  • Another idea, may be the codecs on your machine have to be updated / additional codecs have to be installed. – Ashalynd Sep 04 '15 at 14:27
  • No success.... The problem is that I am developing a script that may be used by other users and this could be dependent of the other computers. Is there an easy way to handle a set of images and create a simple video using python? – Bárbara Duarte Sep 04 '15 at 14:30
  • Without success... I didn't find any codec that works in my script, only '-1' works and with a very very low resolution. – Bárbara Duarte Sep 04 '15 at 16:22
  • So, I am trying to update/install the codec DIVX in my pc. Can you tell where I can download DIVX for windows 8? – Bárbara Duarte Sep 16 '15 at 11:44
  • I don't use Windows, but googling gives this link: http://www.windows8codecs.com/ perhaps useful? – Ashalynd Sep 16 '15 at 12:00
  • I try but without results yet. Thank you. – Bárbara Duarte Sep 16 '15 at 13:47
4

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 )
user3666197
  • 1
  • 6
  • 50
  • 92
0

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.

Rupsx
  • 1
  • 1