Applying Threshold to Video Pixels
Show older comments
This code isn't working for me. I'm trying to limit video pixel values to 235, yet I am still getting pixels in the range 235 - 255 leaking through. Am I applying thresholding correctly?
I get a playable video with this code.
Vptr = VideoReader('Normal.mp4');
img = zeros([Vptr.height,Vptr.width,3]);
writer = VideoWriter('transcoded_xylophone.avi', 'Uncompressed AVI');
writer.FrameRate = reader.FrameRate;
open(writer);
%Read and write each frame.
while hasFrame(Vptr)
img = readFrame(Vptr);
% perform thresholding by logical indexing
img(img>235) = 235;
writeVideo(writer,img);
end
close(writer);
Answers (3)
Image Analyst
on 1 Feb 2018
It has nothing to do with double. The first call to set img with the zeros() function is totally ignored. The code works, though I did have to change reader.FrameRate to Vptr.FrameRate since the is no "reader" variable in your code. I ran it with the rhinos.avi demo video and it works fine, clipping the values to 235 as desired. See this code:
Vptr = VideoReader('Rhinos.avi');
writer = VideoWriter('delete_me.avi', 'Uncompressed AVI');
writer.FrameRate = Vptr.FrameRate;
open(writer);
% Read and write each frame.
frameCounter = 1;
while hasFrame(Vptr)
thisFrame = readFrame(Vptr); % This is a uint8 variable.
% perform thresholding by logical indexing
thisFrame(thisFrame>235) = 235;
fprintf('For frame #%d, the max value = %d\n', frameCounter, max(thisFrame(:)))
writeVideo(writer,thisFrame);
frameCounter = frameCounter + 1;
end
close(writer);
9 Comments
Chris Clementson
on 2 Feb 2018
Image Analyst
on 2 Feb 2018
I haven't tried that. Can you attach a small such video? If you simply call
imshow(thisFrame);
does it look normal? If it does, it's an RGB image. If the colors look bizarre, then it's a YUV image where Y is displaying as red, U displaying as green, and V displaying as blue.
Chris Clementson
on 2 Feb 2018
Edited: Image Analyst
on 2 Feb 2018
Image Analyst
on 2 Feb 2018
That does not answer either of my questions.
And we'd really need your 'Normal.mp4' video, not some normal RGB pictures.
Chris Clementson
on 2 Feb 2018
Chris Clementson
on 2 Feb 2018
Chris Clementson
on 2 Feb 2018
Image Analyst
on 2 Feb 2018
Not sure what luma is. Luminance? Lightness? Anyway, if you want to make sure that the weighted value of RGB doesn't exceed 235 then you need to convert to a color space where luminance is an axes. So you can use rgb2lab() and clip the L value to 235/255, or use rgb2hsv() and clip the v channel, or use rgb2ycbcr() and clip the y channel. Then use the companion function to convert the image back into RGB color space.
Chris Clementson
on 2 Feb 2018
Jose Marques
on 1 Feb 2018
0 votes
Try to transform the image in a double matrix. readFrame gives a uint8 output.
1 Comment
Jose Marques
on 2 Feb 2018
Bad answer! = )
Chris Clementson
on 1 Feb 2018
0 votes
1 Comment
Jose Marques
on 1 Feb 2018
That's right... Look: just to be sure, try to add this code on yours:
% perform thresholding by logical indexing
img(img>235) = 235;
max_value = max(max(max(img)))
pause;
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!