0

I have a sequence of frames of a moving camera. The frames have been stabilized. I want to calculate the frame difference between each two subsequent frames. I do it using

diff = abs(frame1 - frame2);

Frames are Mat objects. However, the two frames will have areas that are not overlapped (i.e. one of the two pixel values of the two frames will be equal to 0), which I don't want to include. If two pixel values are a (= 0) and b (!= 0), then the abs() will be |b|, but I would like to instead have the value 0 if one of the two pixels is 0.

EDIT: I would like to do it without looping over the pixels

  • Create `diff` by iterating over each pixel in `frame1` and `frame2` simultaneously, applying the rules as you mentioned them? You haven't given the types of `frame1` or `frame2` which are pretty useful for anyone trying to give you anything more than broad advice. – druckermanly Jan 05 '17 at 05:59
  • I would like to do it without looping over the pixels. And I added the type of frame (Mat). Thanks. – Abdulrahman Alhadhrami Jan 05 '17 at 06:02

1 Answers1

0

Okay so I figured it out. Basically, we threshold the two frames, A and B, to convert them to a binary image (threshold value = 0, THRESH_BINARY mode), then the two binary images are ANDed, and that result is ANDed with the differenced frame to get the final result.

cv::Mat frameDifference(cv::Mat A, cv::Mat B)
{
  cv::Mat diff = cv::abs(A - B),
          binaryA,
          binaryB,
          binaryAND;

  cv::threshold(A, binaryA, 0, 256, cv::ThresholdTypes::THRESH_BINARY);
  cv::threshold(B, binaryB, 0, 256, cv::ThresholdTypes::THRESH_BINARY);
  cv::bitwise_and(binaryA, binaryB, binaryAND);
  cv::bitwise_and(diff, binaryAND, diff);

  return diff;
}