Question

I have different images and I would like to crop them and keep only what is different between both. Here is the code I have so far.

video = VideoReader('frames.avi', 'Tag', 'my reader object');
frameFirst = read(video,1);
frameSecond = read(video, video.NumberOfFrames-1 );
imshowpair (frameSecond,frameFirst);
pause();
Was it helpful?

Solution

Well, it's tough to give you a good answer without much more detail. I think I understand what you're trying to do, and this might get you moving in the right direction. This code iterates through each pixel of the image (each pixel contains a 1x3 vector of RGB data ranging from 0 to 1), by row and column. If the difference in any of the elements of the 1x3 RGB vector exceeds some threshold (in this case, set to 0.1), we make that whole pixel black (set it to [0 0 0]). Else, we lust make it whatever the last frame was. To filter out all but those pixels that are identical, set the thresh value to 0. It goes like this:

thresh = 0.1
for ii = 1:size(frameFirst, 1)
    for jj = 1:size(frameFirst, 2)
        pixDiff = frameFirst{ii, jj} - frameSecond{ii, jj}
        if (pixDiff(1) > thresh || pixDiff(2) > thresh || pixDiff(3) > thresh)
            outputFrame = frameSecond{ii, jj};
        else
            outputFrame = [0 0 0];
        end
    end
end

I hope this does what you're looking for. Good luck!

Edit 1: Ok, I understand what you are looking for now. You need to have the indices of the bottom-right and top-left. If you already have those, just do this: frameOut = frameIn(xStart:xStop, yStart, yStop. If you need to find those points, that's harder. Let me know and I'll help you work it out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top