Subtraction manually

asked 2014-03-14 21:31:34 -0600

Guru978 gravatar image

updated 2014-03-14 21:32:27 -0600

Hi Folks, I'm trying to implement a function which is subtracting 2 images. (the same does subtract() of opencv)...but since OpenCv often needs 8bit-single Channel images I have to normalize my images before subtracting because the images are CV_16UC1. As I want to save time I want to have the images subtracted before normalizing.

so i did it this way: (back-front=mask)

Mat moving=Mat::zeros(240,320,CV_8UC1);
for(int i=0;i<240;i++){
    unsigned short* rowF=front.ptr<unsigned short>(i);
    unsigned short* rowB=back.ptr<unsigned short>(i);
    uchar* rowM=mask.ptr<uchar>(i);
    uchar fg=255;
    uchar bg=0;
for(int j=0;j<320;j++){
   unsigned short m=rowB[j]-rowF[j];
   if(m>=300){ rowM[j]=fg;
      }else rowM[j]=bg;

  }
}

it runs, but: not with the same result. When I try:

cout<<"120,160"<<":"<<back.at<unsigned short>(120,160)<<endl;

the value of back is displayed. but when I move in front of the camera, the back-value changes to the front value. And the back-image is a still image which is never changed after initialization.

I don't have any ideas what I've done wrong, but you?. Thx for Your help!

edit retag flag offensive close merge delete