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 次查看(过去 30 天)
显示 更早的评论
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);
fprintf('Area of the flame in the last frame: %d pixels\n', areaLast);
fprintf('Ratio of flame area (last frame / first frame): %.2f\n', ratio);
% 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');
2 个评论
采纳的回答
Image Analyst
2024-9-15
Try different thresholds to see which one gives you the correct answer. Maybe you were not using the same threshold as the instructor.
2 个评论
Image Analyst
2024-9-16
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
更多回答(1 个)
Ayush
2024-9-14
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 个评论
Image Analyst
2024-9-15
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'
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!