How can I avoid artifacts when I use im2frame to convert black-and-white images to frames for a video?

2 views (last 30 days)
My function creates a series of 2D black-and-white uint8 images. These display perfectly as individual images, but when I try to make a video with them using im2frame, the lines in the frames are enclosed in salt-and-pepper squares that I assume are some king of artifiact:

Accepted Answer

Stuart Smith
Stuart Smith on 4 Feb 2022
I was able to get clean black-and-white line images with this code:
function y = linedrwg( inpict )
img = rgb2gray( inpict ) ;
binimg = not( imbinarize( edgemap( img ) ) ) ;
binimg = bwmorph( binimg , 'clean' ) ;
[ r , c ] = size( binimg ) ;
y = zeros( r , c , 3 ) ;
y( : , : , 1 : 3 ) = binimg ;
end

More Answers (1)

Benjamin Thompson
Benjamin Thompson on 1 Feb 2022
If you are using VideoWriter to write out the video file with frames you get from im2frame, use an uncompressed or lossless compression profile. The default 'Motion JPEG AVI' profile and any MPEG-4 profile will have a quality parameter that typically allows for improved compression at the expense of artifacts or blurring of edges.
'Archival'
Motion JPEG 2000 file with lossless compression
'Motion JPEG AVI'
AVI file using Motion JPEG encoding
'Motion JPEG 2000'
Motion JPEG 2000 file
'MPEG-4'
MPEG-4 file with H.264 encoding (systems with Windows 7 or later, or macOS 10.7 and later)
'Uncompressed AVI'
Uncompressed AVI file with RGB24 video
'Indexed AVI'
Uncompressed AVI file with indexed video
'Grayscale AVI'
Uncompressed AVI file with grayscale video
  1 Comment
Stuart Smith
Stuart Smith on 2 Feb 2022
I tried all of the profiles listed above. 'Archival' and 'Motion JPEG 2000' produce the error 'unsupported data type double for the chosen file type.' The other profiles produce video with the same artifiacts as in my original post.
Should the images I'm using to make frames be indexed or truecolor, or doesn't it matter?
Thanks for your help.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by