stabilized frame differencing using OpenCV abs() except for nonoverlapped areas

343 views Asked by At

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

1

There are 1 answers

0
Abdulrahman Alhadhrami On BEST ANSWER

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;
}