Beispiel #1
0
QImage GradientEnergy::conv(double* mat, bool sum){
    int width = grey.width();
    int height = grey.height();
    const unsigned int* src = (const unsigned int*)grey.bits();
    unsigned int* dst = (unsigned int*)malloc(sizeof(unsigned int)*width*height);
    for(int y=0;y<height;y++)
    {
        for(int x=0;x<width;x++)
        {
            int color=0;
            //Convolute repair 180 degree
            for(int cy=-1;cy<=1;cy++)
            {
                for(int cx=-1;cx<=1;cx++)
                {
                    int c=0;
                    if((cx+x>=0 && cy+y>=0) && (cx+x<width && cy+y<height))
                    {
                        c = round(qRed(src[(x+cx)+(y+cy)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                    }
                    else if(cx+x<0)
                    {
                        if(cy+y<0)
                        {
                            c = round(qRed(src[(0)+(0)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                        else if(cy+y>=height)
                        {
                            c = round(qRed(src[(0)+(height-1)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                        else
                        {
                            c = round(qRed(src[(0)+(y+cy)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                    }
                    else if(cx+x>=width)
                    {
                        if(cy+y<0)
                        {
                            c = round(qRed(src[(width-1)+(0)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                        else if(cy+y>=height)
                        {
                            c = round(qRed(src[(width-1)+(height-1)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                        else
                        {
                            c = round(qRed(src[(width-1)+(y+cy)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }

                    }
                    else if(cy+y<0)
                    {
                        if(cx+x<0)
                        {
                            c = round(qRed(src[(0)+(0)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                        else if(cx+x>=width)
                        {
                            c = round(qRed(src[(width-1)+(0)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                        else
                        {
                            c = round(qRed(src[(cx+x)+(0)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                    }
                    else if(cy+y>height)
                    {
                        if(cx+x<0)
                        {
                            c = round(qRed(src[(0)+(height-1)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                        else if(cx+x>=width)
                        {
                            c = round(qRed(src[(width-1)+(height-1)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                        else
                        {
                            c = round(qRed(src[(width)+(height-1)*image.width()])*mat[(2-(cx+1))+((2-(cy+1))*3)]);
                        }
                    }
                    color+=c;
                }
            }
            if (sum)
                color+=128;
            if(color>255)
            {
                color=255;
            }
            else if(color<0)
            {
                color = 0;
            }
            dst[x+y*width]=qRgb(color,color,color);
        }
    }

    QImage conv_img((unsigned char*)dst,width,height,QImage::Format_ARGB32);
    return conv_img;
}
Beispiel #2
0
static void test_conv_image()
{
    printf("test_conv_image...");

    unsigned int img_width = 10;
    unsigned int img_height = 10;
    unsigned int bitsperpixel = 0;
    int no_of_layers = 3;
    int max_features = 20;
    int reduction_factor = 6;
    int pooling_factor = 2;
    float error_threshold[] = {0.0, 0.0, 0.0};
    unsigned int random_seed = 648326;
    unsigned char * img, * img2;
    deeplearn_conv conv;
    float BPerror = -1;
    char plot_filename[256];
    char plot_title[256];

    /* load image from file */
    assert(deeplearn_read_png_file((char*)"Lenna.png",
                                   &img_width, &img_height,
                                   &bitsperpixel, &img)==0);

    img2 = (unsigned char*)malloc(128*128*3*sizeof(unsigned char));
    assert(img2);
    deeplearn_downsample(img, img_width, img_height,
                         img2, 128, 128);
    free(img);
    img = img2;
    img_width = 128;
    img_height = 128;

    assert(conv_init(no_of_layers,
                     img_width, img_height,
                     bitsperpixel/8, max_features,
                     reduction_factor, pooling_factor,
                     &conv, error_threshold,
                     &random_seed) == 0);

    int conv0_size =
        conv.layer[0].units_across *
        conv.layer[0].units_down * max_features;

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < conv0_size; j++) {
            conv.layer[0].convolution[j] = -9999;
        }

        assert(conv_img(img, &conv) == 0);

        /* check that some convolution happened */
        for (int j = 0; j < conv0_size; j++) {
            assert(conv.layer[0].convolution[j] != -9999);
        }

        /* error should be >= 0 */
        assert(conv.BPerror >= 0);
        /* error should be reducing */
        if (i > 0) {
            assert(conv.BPerror < BPerror);
        }
        BPerror = conv.BPerror;
    }

    /* move to hte next layer */
    conv.BPerror = -1;
    conv.current_layer++;

    int conv1_size =
        conv.layer[1].units_across *
        conv.layer[1].units_down * max_features;

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < conv1_size; j++) {
            conv.layer[1].convolution[j] = -9999;
        }

        assert(conv_img(img, &conv) == 0);

        /* check that some convolution happened */
        for (int j = 0; j < conv1_size; j++) {
            assert(conv.layer[1].convolution[j] != -9999);
        }

        /* error should be >= 0 */
        if (!(conv.BPerror >= 0)) {
            printf("\nBPerror: %f\n",conv.BPerror);
        }
        assert(conv.BPerror >= 0);
        /* error should be reducing */
        if (i > 0) {
            assert(conv.BPerror < BPerror);
        }
        BPerror = conv.BPerror;
    }

    sprintf(plot_filename,"/tmp/%s","libdeep_conv.png");
    sprintf(plot_title,"%s","Convolution Training Error");

    assert(conv_plot_history(&conv, plot_filename,
                             plot_title,
                             1024, 640) == 0);
    conv_free(&conv);
    free(img);

    printf("Ok\n");
}