Opencv Image Frame Difference Method (Image Subtraction) Code
/* It indicates that this method can be debugged in VC, but not in codeblocks. The problem lies in the height variable. If the value of height is smaller
Yes, but some images are not processed, and the processing speed is slow using the cvset () and cvget () functions */
# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
# Include "cv. H"
# Include "highgui. H"
Iplimage * img0 = NULL;
Iplimage * img1 = NULL;
Iplimage * img2 = NULL;
Void ontrackerslid (int pos)
{
Int I, J;
Cvscalar S0, S1, S2;
For (I = 0; I height; I ++)
For (j = 0; j width; j ++)
{
S0 = cvget2d (img0, I, j); // get the (I, j) pixel value
S1 = cvget2d (img1, I, j );
S2 = cvget2d (img2, I, j );
// For (k = 0; k <channel; k ++)
If (FABS (s1.val [0]-s0.val [0])> POS)
{
S2.val [0] = 0;
Cvset2d (img2, I, j, S2); // set the (I, j) pixel value
}
Else
{
S2.val [0] = 255;
Cvset2d (img2, I, j, S2 );
}
}
Cvshowimage ("result", img2 );
}
Int main (INT argc, char ** argv)
{Int thresh = 80;
// Load the image
Img0 = cvloadimage (argv [1], 0); // you cannot use iplimage again because it is already a global variable.
Img1 = cvloadimage (argv [2], 0 );
Img2 = cvcreateimage (cvgetsize (img0), 8, 1 );
// Int channel = IMG-> nchannels;
// Printf ("the image is % d x % d wiht % d channels", height, width, Channel );
Cvnamedwindow ("imge0", cv_window_autosize); // create a window
Cvnamedwindow ("imge1", cv_window_autosize );
Cvnamedwindow ("result", cv_window_autosize );
Cvshowimage ("imge0", img0); // display the image
Cvshowimage ("imge1", img1 );
Cvcreatetrackbar ("threshold", "result", & Thresh, 255, ontrackerslid );
Ontrackerslid (thresh );
Cvwaitkey (0); // wait for the key
Cvdestroywindow ("imge0"); // destroy the window
Cvdestroywindow ("imge1 ");
Cvdestroywindow ("result ");
Cvreleaseimage (& img0); // release the image
Cvreleaseimage (& img1 );
Cvreleaseimage (& img2 );
Return-1;
}
Method 2:
/* # Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
# Include "cv. H"
# Include "highgui. H"
Iplimage * img0 = NULL;
Iplimage * img1 = NULL;
Iplimage * img2 = NULL;
Uchar * data0 = NULL;
Uchar * data1 = NULL;
Uchar * data2 = NULL;
Void ontrackerslid (int pos)
{
Int I, J;
Int Height = img0-> height;
Int width = img0-> width;
Int step = img0-> widthstep/sizeof (uchar );
Data0 = (uchar *) img0-> imagedata;
Data1 = (uchar *) img1-> imagedata;
Data2 = (uchar *) img2-> imagedata;
For (I = 0; I {
If (ABS (data1 [I * Step + J + 0]-data0 [I * Step + J + 0])> POS)
Data2 [I * Step + J + 0] = 255;
Else
Data2 [I * Step + J + 0] = 0;
}
Cvshowimage ("result", img2 );
}
Note: For this part, you can use the function provided by CV to achieve image subtraction and threshold processing:
Add an iplimage object img3 and use cvcreateimage to initialize it. replace this part with the following code:
Cvabsdiff (img1, img0, img2 ); // The Image Subtraction function is the same as if (data1 [I * Step + J + 0]-data0 [I * Step + J + 0])> POS, no absolute value
Cvthreshold (img2, img3, POs, 255, cv_thresh_binary); // threshold value processing
Cvshowimage ("result", img3); // display the processed image
Int main (INT argc, char ** argv)
{Int thresh = 80;
// Load the image
Img0 = cvloadimage (argv [1], 0 );
Img1 = cvloadimage (argv [2], 0 );
Img2 = cvcreateimage (cvgetsize (img0), 8, 1 );
Cvsmooth (img0, img0, cv_gaussian, 3, 0,); // Gaussian filtered Smoothing image
// Int channel = IMG-> nchannels;
// Printf ("the image is % d x % d wiht % d channels", height, width, Channel );
Cvnamedwindow ("imge0", cv_window_autosize); // create a window
Cvnamedwindow ("imge1", cv_window_autosize );
Cvnamedwindow ("result", cv_window_autosize );
Cvshowimage ("imge0", img0); // display the image
Cvshowimage ("imge1", img1 );
Cvcreatetrackbar ("threshold", "result", & Thresh, 255, ontrackerslid );
Ontrackerslid (thresh );
Cvwaitkey (0); // wait for the key
Cvdestroywindow ("imge0"); // destroy the window
Cvdestroywindow ("imge1 ");
Cvdestroywindow ("result ");
Cvreleaseimage (& img0); // release the image
Cvreleaseimage (& img1 );
Cvreleaseimage (& img2 );
Return-1;
}*/
Method 3:
/* Use matrix subtraction for image frame difference. The Code is as follows, which is similar to using the function provided by CV */
# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
# Include "cv. H"
# Include "highgui. H"
Iplimage * img0 = NULL;
Iplimage * img1 = NULL;
Cvmat * img2 = NULL;
Cvmat * img3 = NULL;
Void ontrackerslid (int pos)
{
Cvsub (img0, img1, img2, 0); // Image Subtraction
Cvthreshold (img2, img3, POs, 255, cv_thresh_binary); // threshold value processing
Cvshowimage ("result", img3); // display the processed image
}
Int main (INT argc, char ** argv)
{Int thresh = 80;
// Load the image
Img0 = cvloadimage (argv [1], 0 );
Img1 = cvloadimage (argv [2], 0 );
Img2 = cvcreatemat (img0-> height, img0-> width, cv_8uc1); // matrix pointer initialization, similar to cvcreareimage
Img3 = cvcreatemat (img0-> height, img0-> width, cv_8uc1 );
Cvsmooth (img0, img0, cv_gaussian, 3, 0,); // Gaussian filtered Smoothing image
Cvnamedwindow ("imge0", cv_window_autosize); // create a window
Cvnamedwindow ("imge1", cv_window_autosize );
Cvnamedwindow ("result", cv_window_autosize );
Cvshowimage ("imge0", img0); // display the image
Cvshowimage ("imge1", img1 );
Cvcreatetrackbar ("threshold", "result", & Thresh, 255, ontrackerslid );
Ontrackerslid (thresh );
Cvwaitkey (0); // wait for the key
Cvdestroywindow ("imge0"); // destroy the window
Cvdestroywindow ("imge1 ");
Cvdestroywindow ("result ");
Cvreleaseimage (& img0); // release the image
Cvreleaseimage (& img1 );
Cvreleasemat (& img2 );
Cvreleasemat (& img3 );
Return-1;
}
// The effect is different because cvsub only subtract pixels and does not take the absolute value of the difference */