What is the ratio of area of the flame between the last frame and the first? What is wrong with my approach? I am getting an incorrect answer upon submission.

6 views (last 30 days)
Question 3 Create a mask for each frame of this video that isolates the nearly-white flame exiting the rocket. Do this by converting each frame to grayscale. Then label all pixels with intensities less than or equal to 245 as false, and all pixels with intensities greater than 245 as true.
What is the ratio of area of the flame between the last frame and the first? (Use the number of true pixels for area.)
% Load the video file 'shuttle.avi'
videoFile = 'shuttle.avi';
v = VideoReader(videoFile);
% Read the first frame
firstFrame = readFrame(v);
% Move to the last frame
while hasFrame(v)
lastFrame = readFrame(v);
end
% Convert both frames to grayscale
firstGray = rgb2gray(firstFrame);
lastGray = rgb2gray(lastFrame);
% Create masks where pixel intensities > 245 are true, the rest are false
firstMask = firstGray > 245;
lastMask = lastGray > 245;
% Calculate the flame area (number of 'true' pixels) for both frames
areaFirst = sum(firstMask(:));
areaLast = sum(lastMask(:));
% Calculate the ratio of the flame area between the last and the first frame
if areaFirst == 0
error('No flame detected in the first frame.');
else
ratio = areaLast / areaFirst;
end
% Display the results
fprintf('Area of the flame in the first frame: %d pixels\n', areaFirst);
Area of the flame in the first frame: 520 pixels
fprintf('Area of the flame in the last frame: %d pixels\n', areaLast);
Area of the flame in the last frame: 5497 pixels
fprintf('Ratio of flame area (last frame / first frame): %.2f\n', ratio);
Ratio of flame area (last frame / first frame): 10.57
% Optional: Display the original frames and the masks for visual verification
figure;
subplot(2,2,1), imshow(firstFrame), title('First Frame');
subplot(2,2,2), imshow(lastFrame), title('Last Frame');
subplot(2,2,3), imshow(firstMask), title('First Frame Mask');
subplot(2,2,4), imshow(lastMask), title('Last Frame Mask');

Accepted Answer

Image Analyst
Image Analyst on 15 Sep 2024
Try different thresholds to see which one gives you the correct answer. Maybe you were not using the same threshold as the instructor.
Try using bwareafilt to take just the largest blob only.
  2 Comments
Amit
Amit on 16 Sep 2024
Hi!! Thank you for response. I solved the question after brainstorming a lot and it was as you said, the threshold value was causing the error. I found out the correct value after experimenting. Thanks a lot for the help!!
Image Analyst
Image Analyst on 16 Sep 2024
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
For full details on how to earn reputation points see: https://www.mathworks.com/matlabcentral/answers/help?s_tid=al_priv#reputation

Sign in to comment.

More Answers (1)

Ayush
Ayush on 14 Sep 2024
Hi Amit,
Although I do not have access to your file but you can try these debugging steps to check for any issues:
  • Threshold: The 245 threshold might not fit all frames due to varying lighting and video quality. Consider adjusting it dynamically or using a histogram-based method.
  • Edge Cases: Ensure the first frame has flame pixels. If areaFirst is zero, your ratio calculation will fail. Double-check the first frame for flame presence.
  • Video Quality: Noise or compression artifacts can affect processing. Preprocess frames to reduce noise if needed.
  • Frame Selection: Confirm you're selecting the correct first and last frames by ensuring readFrame iterates properly.
I hope this helps!
  1 Comment
Image Analyst
Image Analyst on 15 Sep 2024
If you have the Image Processing Toolbox, you will have the video. It ships with the toolbox.
>> folder = fileparts(which('shuttle.avi'))
folder =
'C:\Program Files\MATLAB\R2024b\toolbox\matlab\demos'

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!