Filter löschen
Filter löschen

Difference between 2 images?

3 Ansichten (letzte 30 Tage)
Itai Gutman
Itai Gutman am 1 Aug. 2023
Kommentiert: Image Analyst am 2 Aug. 2023
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 Kommentar
Mrutyunjaya Hiremath
Mrutyunjaya Hiremath am 1 Aug. 2023
Itai, despite receiving answers to your questions, you are not responding to them.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath am 1 Aug. 2023
% 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 Kommentare
Itai Gutman
Itai Gutman am 2 Aug. 2023
My idea was very similar to your script, thank you for the full script.
Image Analyst
Image Analyst am 2 Aug. 2023
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.

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 1 Aug. 2023
Bearbeitet: Matt J am 1 Aug. 2023
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.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by