コード例 #1
0
        public GrabCut(CudaPitchedDeviceVariable <uchar4> image, CudaPitchedDeviceVariable <byte> trimap, int width, int height)
        {
            d_trimap = trimap;
            //The first one will also init the CUDA context!
            grabCutUtils = new GrabCutUtils();
            grabCutGMM   = new GrabCutGMM();

            size.width  = width;
            size.height = height;
            graphcut8   = new GraphCut8(size);

            gmms          = 2 * COLOR_CLUSTER;
            edge_strength = EDGE_STRENGTH;

            blocks    = ((width + 31) / 32) * ((height + 31) / 32);
            gmm_pitch = 11 * sizeof(float);

            //d_image =  new CudaPitchedDeviceVariable<uchar4>(size.width, size.height);
            //d_image.CopyToDevice(image);
            d_image = image;

            // Doublebuffered alpha
            d_alpha    = new CudaPitchedDeviceVariable <byte> [2];
            d_alpha[0] = new CudaPitchedDeviceVariable <byte>(size.width, size.height, 4);
            d_alpha[1] = new CudaPitchedDeviceVariable <byte>(size.width, size.height, 4);

            // Graph
            d_terminals        = new CudaPitchedDeviceVariable <int>(size.width, size.height);
            d_top              = new CudaPitchedDeviceVariable <int>(size.width, size.height);
            d_topleft          = new CudaPitchedDeviceVariable <int>(size.width, size.height);
            d_topright         = new CudaPitchedDeviceVariable <int>(size.width, size.height);
            d_bottom           = new CudaPitchedDeviceVariable <int>(size.width, size.height);
            d_bottomleft       = new CudaPitchedDeviceVariable <int>(size.width, size.height);
            d_bottomright      = new CudaPitchedDeviceVariable <int>(size.width, size.height);
            d_left_transposed  = new CudaPitchedDeviceVariable <int>(size.height, size.width);
            d_right_transposed = new CudaPitchedDeviceVariable <int>(size.height, size.width);


            //int scratch_gc_size = 0;
            //nppiGraphcut8GetSize(size, &scratch_gc_size);

            int scratch_gmm_size = (int)(blocks * gmm_pitch * gmms + blocks * 4);

            d_scratch_mem = new CudaDeviceVariable <byte>(scratch_gmm_size);
            //CUDA_SAFE_CALL( cudaMalloc(&d_scratch_mem, MAX(scratch_gmm_size, scratch_gc_size)) );

            //NPP_CHECK_NPP(nppiGraphcutInitAlloc(size, &pState, d_scratch_mem) );
            d_gmm = new CudaDeviceVariable <float>(gmm_pitch * gmms);
            //CUDA_SAFE_CALL( cudaMalloc(&d_gmm, gmm_pitch * gmms) );

            // Estimate color models on lower res input image first
            createSmallImage(Math.Max(width / 4, height / 4));
        }
コード例 #2
0
        private void createSmallImage(int max_dim)
        {
            int[] temp_width  = new int[2];
            int[] temp_height = new int[2];

            CudaPitchedDeviceVariable <uchar4>[] d_temp = new CudaPitchedDeviceVariable <uchar4> [2];

            temp_width[0]  = (int)Math.Ceiling(size.width * 0.5f);
            temp_height[0] = (int)Math.Ceiling(size.height * 0.5f);

            temp_width[1]  = (int)Math.Ceiling(temp_width[0] * 0.5f);
            temp_height[1] = (int)Math.Ceiling(temp_height[0] * 0.5f);

            d_temp[0] = new CudaPitchedDeviceVariable <uchar4>(temp_width[0], temp_height[0]);
            d_temp[1] = new CudaPitchedDeviceVariable <uchar4>(temp_width[1], temp_height[1]);

            // Alloc also the small trimaps
            d_small_trimap    = new CudaPitchedDeviceVariable <byte> [2];
            d_small_trimap[0] = new CudaPitchedDeviceVariable <byte>(temp_width[0], temp_height[0], 4);
            d_small_trimap[1] = new CudaPitchedDeviceVariable <byte>(temp_width[1], temp_height[1], 4);

            grabCutGMM.Downscale(d_temp[0], temp_width[0], temp_height[0], d_image, size.width, size.height);
            int current = 0;

            while (temp_width[current] > max_dim || temp_height[current] > max_dim)
            {
                grabCutGMM.Downscale(d_temp[1 - current], temp_width[1 - current], temp_height[1 - current], d_temp[current], temp_width[current], temp_height[current]);
                current ^= 1;
                temp_width[1 - current]  = (int)Math.Ceiling(temp_width[current] * 0.5f);
                temp_height[1 - current] = (int)Math.Ceiling(temp_height[current] * 0.5f);
            }

            d_small_image     = d_temp[current];
            small_size.width  = temp_width[current];
            small_size.height = temp_height[current];

            graphcut8Small = new GraphCut8(small_size);
            d_temp[1 - current].Dispose();
        }