Difference between 2 images?

2 次查看(过去 30 天)
Itai Gutman
Itai Gutman 2023-8-1
I have a video that record some lamps that any time another lamp is on. I want to find the specific frame in the video when the lamp is on (few times in the video) and which lamp (I guess by the pixels location). I think to do that by compare all the frames in the video to first frame (all the lamps are off) for example:Im2-Im1. How can i do it?
  1 个评论
Mrutyunjaya Hiremath
Itai, despite receiving answers to your questions, you are not responding to them.

请先登录,再进行评论。

回答(2 个)

Mrutyunjaya Hiremath
% Read the video file
videoFile = 'path/to/your/video.mp4';
videoObj = VideoReader(videoFile);
% Read the first frame where all lamps are off
firstFrame = read(videoObj, 1);
firstFrameGray = rgb2gray(firstFrame);
% Initialize variables to store detected lamp information
lamp1OnFrames = [];
lamp2OnFrames = [];
% Loop through each frame in the video
for frameNumber = 2:videoObj.NumFrames
% Read the current frame and convert to grayscale
currentFrame = read(videoObj, frameNumber);
currentFrameGray = rgb2gray(currentFrame);
% Perform frame differencing
frameDiff = abs(double(currentFrameGray) - double(firstFrameGray));
% Threshold the frame difference to create a binary image
thresholdValue = 30; % Adjust this threshold based on your video characteristics
binaryImage = frameDiff > thresholdValue;
% Use connected component analysis to find and label connected regions
cc = bwconncomp(binaryImage);
numRegions = cc.NumObjects;
% Analyze the labeled regions to identify which lamp is on
for regionNumber = 1:numRegions
regionPixels = cc.PixelIdxList{regionNumber};
[row, col] = ind2sub(size(binaryImage), regionPixels);
% Use simple spatial analysis to determine which lamp is on
lamp1Location = [lamp1_x, lamp1_y]; % Set the spatial location of lamp 1
lamp2Location = [lamp2_x, lamp2_y]; % Set the spatial location of lamp 2
% Calculate the centroid of the connected region
centroid = [mean(col), mean(row)];
% Calculate the distance between the centroid and lamp locations
distanceToLamp1 = norm(centroid - lamp1Location);
distanceToLamp2 = norm(centroid - lamp2Location);
% Determine which lamp is closer and consider it as on
if distanceToLamp1 < distanceToLamp2
lampNumber = 1;
else
lampNumber = 2;
end
% Store the frame number and lamp number when a lamp is on
if lampNumber == 1
lamp1OnFrames = [lamp1OnFrames, frameNumber];
else
lamp2OnFrames = [lamp2OnFrames, frameNumber];
end
end
end
% Display the results
disp('Frames where Lamp 1 is on:');
disp(lamp1OnFrames);
disp('Frames where Lamp 2 is on:');
disp(lamp2OnFrames);
  2 个评论
Itai Gutman
Itai Gutman 2023-8-2
My idea was very similar to your script, thank you for the full script.
Image Analyst
Image Analyst 2023-8-2
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.

请先登录,再进行评论。


Matt J
Matt J 2023-8-1
编辑:Matt J 2023-8-1
You haven't really given us a clear idea what is unknown or what the challenge is. For example, it is not clear whether you know the pixel locations of the lamps in advance.
Assuming you don't know this, however, I would convert each frame to grayscale and take the inter-frame maximum,
maxFrame=max(Frames,[],3);
This would presumably give you an image where all the lamps are on, which you could then compare that to the first image (all lamps off) which would allow you to determine the lamp pixel locations.

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by