图像处理之基础---基于opencv的灰度图像微分

argv分别为,可执行文件名、读入的原始图像、输出原始图像的灰度值、输出原始图像灰度值沿x轴方向的一阶微分、输出原始图像灰度值沿x轴方向的二阶微分。

#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("Could 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 < height; ++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

posted @ 2014-10-22 02:31  midu  阅读(653)  评论(0编辑  收藏  举报