Argv is, executable File Name, read original image, gray value of the output original image, first-order differentiation of the gray value of the output original image along the X axis, and second-order differentiation of the gray value of the output original image along the X axis.
# Include
# Include
# Include
# Include
# Include
# Pragma comment (Lib, "opencv_highgui243d.lib ")
# Pragma comment (Lib, "opencv_core243d.lib ")
# Pragma comment (Lib, "opencv_ml243d.lib ")
# Pragma comment (Lib, "opencv_imgproc243d.lib ")
Int main (INT argc, char ** argv ){
Int height, width, step, channels;
Uchar * graydata;
Uchar graydatatmp, prev1graydatatmp, prev2graydatatmp;
Int differentialgrayfirstorder, differentialgraysecondorder, prevdifferentialgrayfirstorder;
Int I, J;
File * fpgrayorgn, * fpgrayfirst, * fpgraysecond;
Iplimage * grayimg;
// Load image in single channel, aka. Transform the image to gray (but not save)
Grayimg = cvloadimage (argv [1], 0 );
// Exit from failing loading source Image
If (! Grayimg)
{
Printf ("cocould not load image file: % s", argv [1]);
Exit (1 );
}
// Get basic information of the image
Height = grayimg-> height;
Width = grayimg-> width;
Step = grayimg-> widthstep;
Channels = grayimg-> nchannels;
// Print Image on screen and show basic information of the image
Printf ("processing a % DX % d image with % d channels \ n", height, width, channels );
Cvnamedwindow ("minesweeperwindow", cv_window_autosize );
Cvshowimage ("minesweeperwindow", grayimg );
// Exit from file create Error
Fpgrayorgn = fopen (argv [2], "W + ");
If (fpgrayorgn = NULL)
{
Printf ("file % s create/Open error! ", Argv [2]);
Exit (2 );
}
Fpgrayfirst = fopen (argv [3], "W + ");
If (fpgrayfirst = NULL)
{
Printf ("file % s create/Open error! ", Argv [3]);
Exit (3 );
}
Fpgraysecond = fopen (argv [4], "W + ");
If (fpgraysecond = NULL)
{
Printf ("file % s create/Open error! ", Argv [4]);
Exit (4 );
}
// Move pointer to the start of File
Rewind (fpgrayorgn );
Rewind (fpgrayfirst );
Rewind (fpgraysecond );
// Get every value (in gray) and output to a TXT file
Graydata = (uchar *) grayimg-> imagedata;
For (I = 0; I {
For (j = 0; j <width; ++ J)
{
// Get value
Graydatatmp = graydata [I * Step + J];
Fprintf (fpgrayorgn, "% 4D", graydatatmp );
// Init
If (0 = J)
{
Prev1graydatatmp = 0;
Prev2graydatatmp = 0;
Prevdifferentialgrayfirstorder = 0;
}
// Calculate difference of first-order
Differentialgrayfirstorder = (INT) graydatatmp-(INT) prev1graydatatmp;
Fprintf (fpgrayfirst, "% 4D", differentialgrayfirstorder );
// Calculate difference of second-order
Differentialgraysecondorder = differentialgrayfirstorder-prevdifferentialgrayfirstorder;
Fprintf (fpgraysecond, "% 4D", differentialgraysecondorder );
// Re-assignment
Prevdifferentialgrayfirstorder = differentialgrayfirstorder;
Prev2graydatatmp = prev1graydatatmp;
Prev1graydatatmp = graydatatmp;
}
// Insert a newline
Fprintf (fpgrayorgn, "\ n ");
Fprintf (fpgrayfirst, "\ n ");
Fprintf (fpgraysecond, "\ n ");
}
Fclose (fpgrayorgn );
Fclose (fpgrayfirst );
Fclose (fpgraysecond );
// End print process
Cvwaitkey (0 );
Cvreleaseimage (& grayimg );
Cvdestroywindow ("mineswepperwindow ");
Return (0 );
}
【]
Http://www.infineon-ecosystem.org/focusnie/blog/13-07/295656_9bd39.html
Foundation of Image Processing-gray image differentiation based on opencv