Пример #1
0
IplImage* IPLCreateImage(CImage img)
{
    // Compute the required parameters
    CShape sh = img.Shape();
    int nChannels = sh.nBands;      // Number of channels in the image.
    int alphaChannel =              // Alpha channel number (0 if there is no alpha channel in the image).
        (nChannels == 4) ? img.alphaChannel+1 : 0;
    int depth =                     // Bit depth of pixels. Can be one of:
        (img.PixType() == typeid(uchar)) ? IPL_DEPTH_8U :
        (img.PixType() == typeid(char))  ? IPL_DEPTH_8S :
        (img.PixType() == typeid(unsigned short)) ? IPL_DEPTH_16U :
        (img.PixType() == typeid(short)) ? IPL_DEPTH_16S :
        (img.PixType() == typeid(int  )) ? IPL_DEPTH_32S :
        (img.PixType() == typeid(float)) ? IPL_DEPTH_32F : 0;
    char* colorModel =              // A four-character string describing the color model.
        (nChannels == 1) ? "Gray" : "RGBA";
    char* channelSeq =              // The sequence of color channels.
        (nChannels == 1) ? "GRAY" : "BGRA";
    int dataOrder = IPL_DATA_ORDER_PIXEL;
    int origin = IPL_ORIGIN_TL;     // The origin of the image.
    int align =                     // Alignment of image data.
        ((((int) img.PixelAddress(0, 0, 0)) | ((int) img.PixelAddress(0, 1, 0))) & 7) ?
        IPL_ALIGN_DWORD : IPL_ALIGN_QWORD;
    int width = sh.width;           // Width of the image in pixels.
    int height = sh.height;         // Height of the image in pixels.
    IplROI* roi = 0;                // Pointer to an ROI (region of interest) structure.
    IplImage* maskROI = 0;          // Pointer to the header of another image that specifies the mask ROI.
    void* imageID = 0;              // The image ID (field reserved for the application).
    IplTileInfo* tileInfo = 0;      // The pointer to the IplTileInfo structure

    // Create the header
    IplImage* ptr = iplCreateImageHeader(
        nChannels,
        alphaChannel, depth,  colorModel,
        channelSeq, dataOrder, origin, align,
        width, height, roi, maskROI,
        imageID, tileInfo);
    if (ptr == 0)
        throw CError("IPLCreateImage: could not create the header");

    // Fill in the image data pointers
    char* imgData   = ((char *) img.PixelAddress(0, 0, 0));
    int nBytes      = ((char *) img.PixelAddress(0, 1, 0)) - imgData;
	ptr->imageSize = nBytes * sh.height;    // useful size in bytes
	ptr->imageData = imgData;               // pointer to aligned image
	ptr->widthStep = nBytes;                // size of aligned line in bytes
	ptr->imageDataOrigin = imgData;         // ptr to full, nonaligned image

    // Set the border mode
    int mode = 
        (img.borderMode == eBorderZero)     ? IPL_BORDER_CONSTANT :
        (img.borderMode == eBorderReplicate)? IPL_BORDER_REPLICATE :
        (img.borderMode == eBorderReflect)  ? IPL_BORDER_REFLECT :
        (img.borderMode == eBorderCyclic)   ? IPL_BORDER_WRAP : 0;
    iplSetBorderMode(ptr, mode, IPL_SIDE_ALL, 0);

    return ptr;
}
Пример #2
0
static int fcaScharr( void )
{
    int KerX[15];
    int KerY[15];
    CvSize KerLens;
    int dx,dy;
    AtsRandState state;
    AtsRandState sizegen;
    double Error = 0;

    IplImage* src8u;
    IplImage* src8s;
    IplImage* src32f;
    IplImage* src16;
    IplImage* dst16;
    IplImage* test16;
    IplImage* dst32f;
    IplImage* test32f;
    IplImage* buf;
    IplConvKernel* KernelX;
    IplConvKernel* KernelY;
    IplConvKernelFP* KX;
    IplConvKernelFP* KY;

    /* Initialization global parameters */
    if( !read_param )
    {
        read_param = 1;
        /* Determining which test are needed to run */
        trsCaseRead( &data_types,"/u/s/f/a", "a",
                     "u - unsigned char, s - signed char, f - float, a - all" );
        /* Reading test-parameters */
        trsiRead( &lMinImageSize, "16", "Image height" );
        trsiRead( &lMaxImageSize, "256", "Image width" );
        trsiRead( &lTestNum, "20", "Test count" );
    }
    if( data_types != 3 && data_types != 0 ) return TRS_UNDEF;
    atsRandInit(&sizegen,lMinImageSize,lMaxImageSize,0);
    /* Creating images for testing */
    for(int i = 0; i < lTestNum; i++)
    {
        lImageWidth = atsRand32s(&sizegen);
        lImageHeight = atsRand32s(&sizegen);
        src8u= cvCreateImage(cvSize(lImageWidth, lImageHeight), IPL_DEPTH_8U, 1);
        src8s= cvCreateImage(cvSize(lImageWidth, lImageHeight), IPL_DEPTH_8S, 1);
        src32f = cvCreateImage(cvSize(lImageWidth, lImageHeight), IPL_DEPTH_32F, 1);
        src16 = cvCreateImage(cvSize(lImageWidth, lImageHeight), IPL_DEPTH_16S, 1);
        dst16 = cvCreateImage(cvSize(lImageWidth, lImageHeight), IPL_DEPTH_16S, 1);
        test16 = cvCreateImage(cvSize(lImageWidth, lImageHeight), IPL_DEPTH_16S, 1);
        buf = cvCreateImage(cvSize(lImageWidth, lImageHeight), IPL_DEPTH_32F, 1);
        dst32f = cvCreateImage(cvSize(lImageWidth, lImageHeight), IPL_DEPTH_32F, 1);
        test32f = cvCreateImage(cvSize(lImageWidth, lImageHeight), IPL_DEPTH_32F, 1);
        atsRandInit(&state,0,64,32);
        atsFillRandomImageEx(src8u, &state );
        atsFillRandomImageEx(src8s, &state );
        atsFillRandomImageEx(src32f, &state );


        /* Calculating the kernels */
        for(dx = 0; dx<=1; dx++)
        {
            dy=1-dx;
            atsCalcKernel(cv8u,dx,dy,CV_SCHARR,(char*)KerX,(char*)KerY,&KerLens,CV_ORIGIN_TL);
            KernelX = iplCreateConvKernel(KerLens.width,1,KerLens.width/2,0,KerX,0);
            KernelY = iplCreateConvKernel(1,KerLens.height,0,KerLens.height/2,KerY,0);

            /* Calculating the convolution */
            atsConvert(src8u,src16);
            iplSetBorderMode(src16,
                             IPL_BORDER_REPLICATE,
                             IPL_SIDE_TOP|IPL_SIDE_BOTTOM|IPL_SIDE_RIGHT|IPL_SIDE_LEFT,
                             0);
            iplConvolveSep2D(src16,dst16,KernelX,KernelY);
            cvSobel(src8u,test16,dx,dy,CV_SCHARR);
            Error += iplNorm(dst16,test16,IPL_C);

            atsConvert(src8s,src16);
            iplSetBorderMode(src16,
                             IPL_BORDER_REPLICATE,
                             IPL_SIDE_TOP|IPL_SIDE_BOTTOM|IPL_SIDE_RIGHT|IPL_SIDE_LEFT,
                             0);
            iplSetBorderMode(src16,
                             IPL_BORDER_REPLICATE,
                             IPL_SIDE_TOP|IPL_SIDE_BOTTOM|IPL_SIDE_RIGHT|IPL_SIDE_LEFT,
                             0);
            iplConvolveSep2D(src16,dst16,KernelX,KernelY);
            cvSobel(src8s,test16,dx,dy,CV_SCHARR);
            Error += iplNorm(dst16,test16,IPL_C);

            atsCalcKernel(IPL_DEPTH_32F,dx,dy,CV_SCHARR,(char*)KerX,(char*)KerY,&KerLens,CV_ORIGIN_TL);
            KX = iplCreateConvKernelFP(KerLens.width,1,KerLens.width/2,0,(float*)KerX);
            KY = iplCreateConvKernelFP(1,KerLens.height,0,KerLens.height/2,(float*)KerY);
            iplSetBorderMode(src32f,
                             IPL_BORDER_REPLICATE,
                             IPL_SIDE_TOP|IPL_SIDE_BOTTOM|IPL_SIDE_RIGHT|IPL_SIDE_LEFT,
                             0);


            /* Calculating the convolution */
            iplConvolveSep2DFP(src32f,dst32f,KX,KY);
            cvSobel(src32f,test32f,dx,dy,CV_SCHARR);
            /*for(i = 0; i<lImageHeight; i++)
            	for(int j = 0; j<lImageWidth; j++)
            		{
            			float a = ((float*)(dst32f->imageData))[i*dst32f->widthStep/4+j];
            			float b = ((float*)(test32f->imageData))[i*test32f->widthStep/4+j];
            		}
            	*/
            Error += iplNorm(test32f,dst32f,IPL_C);
            iplDeleteConvKernel(KernelX);
            iplDeleteConvKernel(KernelY);
            iplDeleteConvKernelFP(KX);
            iplDeleteConvKernelFP(KY);
        }

        /* Free Memory */

        cvReleaseImage( &src8u );
        cvReleaseImage( &src8s );
        cvReleaseImage( &src32f );
        cvReleaseImage( &src16 );
        cvReleaseImage( &dst16 );
        cvReleaseImage( &test16 );
        cvReleaseImage( &dst32f );
        cvReleaseImage( &test32f );
        cvReleaseImage( &buf);
    }

    if(Error/lTestNum>=EPSILON)return TRS_FAIL;
    return TRS_OK;
} /* fcaSobel8uC1R */