Beispiel #1
0
        private void button3_Click(object sender, EventArgs e)
        {
            if (grabcut == null)
            {
                return;
            }

            grabcut.grabCutUtils.TrimapFromRect(d_bmp_mask, selection, width, height);

            grabcut.computeSegmentationFromTrimap();

            NPPImage_8uC1 alphamap = new NPPImage_8uC1(grabcut.AlphaMap.DevicePointer, grabcut.AlphaMap.Width, grabcut.AlphaMap.Height, grabcut.AlphaMap.Pitch);

            alphamap.CopyToHost(bmp_mask);


            pictureBox_Mask.Image = bmp_mask;

            int mode = 0;

            if (rb_Masked.Checked)
            {
                mode = 1;
            }
            if (rb_CutOut.Checked)
            {
                mode = 2;
            }

            grabcut.grabCutUtils.ApplyMatte(mode, d_bmp_res, d_bmp_src, grabcut.AlphaMap, width, height);
            npp_bmp_res.CopyToHost(bmp_res);

            pictureBox_Result.Image = bmp_res;

            lbl_Iterations.Text = grabcut.Iterations.ToString();
            lbl_runtime.Text    = grabcut.Runtime.ToString() + " [ms]";
        }
Beispiel #2
0
        private void Dilate3x3(object sender, EventArgs e, Bitmap map)
        {
            ManagedCuda.NPP.NPPImage_8uC4 source = new NPPImage_8uC4(map.Width, map.Height);
            source = CudaHelper(map);
            ManagedCuda.NPP.NPPImage_8uC4 dest = new NPPImage_8uC4(source.Size);

            Int32 iter = Iterations;

            for (int i = 0; i < iter; i++)
            {
                source.Dilate3x3(dest);
                source = dest;
            }
            Bitmap destMap = new Bitmap(map.Width, map.Height);

            dest.CopyToHost(destMap);
            outPictureBox2.Image = destMap;

            SetTextSize(this.label2, destMap);
        }
Beispiel #3
0
        //Compute histogram and apply LUT to image
        private void btn_calc_Click(object sender, EventArgs e)
        {
            if (_colorChannels < 1 || !_nppOK)
            {
                return;
            }

            try
            {
                int binCount   = 255;
                int levelCount = binCount + 1;

                int[]  levels;
                int[]  bins;
                int[]  lut        = new int[levelCount];
                int    totalSum   = 0;
                float  mutiplier  = 0;
                int    runningSum = 0;
                Bitmap res;

                switch (_colorChannels)
                {
                case 1:
                    //The NPP library sets up a CUDA context, we can directly use it without access to it
                    CudaDeviceVariable <int> bins_d = new CudaDeviceVariable <int>(binCount);
                    levels = src_c1.EvenLevels(levelCount, 0, levelCount);
                    //Even levels in Cuda 5.5 seems to be broken: set it manually
                    for (int i = 0; i < levelCount; i++)
                    {
                        levels[i] = i;
                    }

                    //Compute histogram from source image
                    src_c1.HistogramEven(bins_d, 0, binCount + 1);
                    //Copy data from device to host:
                    bins = bins_d;

                    //draw histogram image
                    hist_rb_src.Image = GetHistogramImage(bins, 0);

                    //compute histogram equalization
                    for (int i = 0; i < binCount; i++)
                    {
                        totalSum += bins[i];
                    }
                    Debug.Assert(totalSum == src_c1.Width * src_c1.Height);

                    if (totalSum == 0)
                    {
                        totalSum = 1;
                    }

                    mutiplier = 1.0f / (float)totalSum * 255.0f;

                    for (int i = 0; i < binCount; i++)
                    {
                        lut[i]      = (int)(runningSum * mutiplier + 0.5f);
                        runningSum += bins[i];
                    }

                    lut[binCount] = 255;

                    //Aplly this lut to src image and get result in dest image
                    src_c1.LUT(dest_c1, lut, levels);

                    //Create new bitmap in host memory for result image
                    res = new Bitmap(src_c1.Width, src_c1.Height, PixelFormat.Format8bppIndexed);
                    SetPalette(res);

                    //Copy result from device to host
                    dest_c1.CopyToHost(res);

                    pictureBox_dest.Image = res;

                    //Compute new histogram and show it
                    dest_c1.HistogramEven(bins_d, 0, binCount);
                    hist_g_src.Image = GetHistogramImage(bins_d, 0);
                    //Free temp memory
                    bins_d.Dispose();
                    break;

                case 3:
                    //The NPP library sets up a CUDA context, we can directly use it without access to it
                    CudaDeviceVariable <int>[] bins_ds = new CudaDeviceVariable <int> [3];
                    bins_ds[0] = new CudaDeviceVariable <int>(binCount);
                    bins_ds[1] = new CudaDeviceVariable <int>(binCount);
                    bins_ds[2] = new CudaDeviceVariable <int>(binCount);
                    levels     = src_c3.EvenLevels(levelCount, 0, levelCount);
                    //Even levels in Cuda 5.5 seems to be broken: set it manually
                    for (int i = 0; i < levelCount; i++)
                    {
                        levels[i] = i;
                    }
                    int[] ll = new int[] { 0, 0, 0 };
                    int[] up = new int[] { binCount + 1, binCount + 1, binCount + 1 };

                    //Compute histogram from source image
                    src_c3.HistogramEven(bins_ds, ll, up);

                    int[][] bins3 = new int[3][];
                    int[][] luts  = new int[3][];
                    for (int c = 0; c < 3; c++)
                    {
                        //Copy data from device to host:
                        bins3[c] = bins_ds[c];
                        luts[c]  = new int[levelCount];
                    }

                    //draw histogram images
                    hist_rb_src.Image = GetHistogramImage(bins3[2], bins3[1], bins3[0], 1);
                    hist_g_src.Image  = GetHistogramImage(bins3[1], bins3[0], bins3[2], 2);
                    hist_b_src.Image  = GetHistogramImage(bins3[0], bins3[1], bins3[2], 3);

                    //compute histogram equalization
                    for (int c = 0; c < 3; c++)
                    {
                        totalSum   = 0;
                        runningSum = 0;
                        for (int i = 0; i < binCount; i++)
                        {
                            totalSum += bins3[c][i];
                        }
                        Debug.Assert(totalSum == src_c3.Width * src_c3.Height);

                        if (totalSum == 0)
                        {
                            totalSum = 1;
                        }

                        mutiplier = 1.0f / (float)totalSum * 255.0f;

                        for (int i = 0; i < binCount; i++)
                        {
                            luts[c][i]  = (int)(runningSum * mutiplier + 0.5f);
                            runningSum += bins3[c][i];
                        }
                        luts[c][binCount] = 255;
                    }
                    //Aplly this lut to src image and get result in dest image
                    src_c3.Lut(dest_c3, luts[0], levels, luts[1], levels, luts[2], levels);

                    res = new Bitmap(src_c3.Width, src_c3.Height, PixelFormat.Format24bppRgb);

                    //Copy result from device to host
                    dest_c3.CopyToHost(res);

                    pictureBox_dest.Image = res;

                    //Compute new histogram and show it
                    dest_c3.HistogramEven(bins_ds, ll, up);
                    bins3[0]           = bins_ds[0];
                    bins3[1]           = bins_ds[1];
                    bins3[2]           = bins_ds[2];
                    hist_rb_dest.Image = GetHistogramImage(bins3[2], bins3[1], bins3[0], 1);                           //r
                    hist_g_dest.Image  = GetHistogramImage(bins3[1], bins3[0], bins3[2], 2);                           //g
                    hist_b_dest.Image  = GetHistogramImage(bins3[0], bins3[1], bins3[2], 3);                           //b

                    //Free temp memory
                    bins_ds[0].Dispose();
                    bins_ds[1].Dispose();
                    bins_ds[2].Dispose();
                    break;

                case 4:
                    //The NPP library sets up a CUDA context, we can directly use it without access to it
                    CudaDeviceVariable <int>[] bins_ds4 = new CudaDeviceVariable <int> [4];
                    bins_ds4[0] = new CudaDeviceVariable <int>(binCount);
                    bins_ds4[1] = new CudaDeviceVariable <int>(binCount);
                    bins_ds4[2] = new CudaDeviceVariable <int>(binCount);
                    bins_ds4[3] = new CudaDeviceVariable <int>(binCount);
                    levels      = src_c4.EvenLevels(levelCount, 0, levelCount);
                    //Even levels in Cuda 5.5 seems to be broken: set it manually
                    for (int i = 0; i < levelCount; i++)
                    {
                        levels[i] = i;
                    }
                    int[] ll4 = new int[] { 0, 0, 0, 0 };
                    int[] up4 = new int[] { binCount + 1, binCount + 1, binCount + 1, binCount + 1 };

                    //Compute histogram from source image
                    src_c4.HistogramEven(bins_ds4, ll4, up4);

                    int[][] bins4 = new int[4][];
                    int[][] luts4 = new int[4][];
                    for (int c = 0; c < 4; c++)
                    {
                        //Copy data from device to host:
                        bins4[c] = bins_ds4[c];
                        luts4[c] = new int[levelCount];
                    }

                    //draw histogram images
                    hist_rb_src.Image = GetHistogramImage(bins4[2], bins4[1], bins4[0], 1);
                    hist_g_src.Image  = GetHistogramImage(bins4[1], bins4[0], bins4[2], 2);
                    hist_b_src.Image  = GetHistogramImage(bins4[0], bins4[1], bins4[2], 3);

                    //compute histogram equalization
                    for (int c = 0; c < 3; c++)
                    {
                        totalSum   = 0;
                        runningSum = 0;
                        for (int i = 0; i < binCount; i++)
                        {
                            totalSum += bins4[c][i];
                        }
                        Debug.Assert(totalSum == src_c4.Width * src_c4.Height);

                        if (totalSum == 0)
                        {
                            totalSum = 1;
                        }

                        mutiplier = 1.0f / (float)totalSum * 255.0f;

                        for (int i = 0; i < binCount; i++)
                        {
                            luts4[c][i] = (int)(runningSum * mutiplier + 0.5f);
                            runningSum += bins4[c][i];
                        }
                        luts4[c][binCount] = 255;
                    }

                    //Aplly this lut to src image and get result in dest image
                    src_c4.LutA(dest_c4, luts4[0], levels, luts4[1], levels, luts4[2], levels);

                    //Set alpha channel to 255
                    dest_c4.Set(255, 3);
                    res = new Bitmap(src_c4.Width, src_c4.Height, PixelFormat.Format32bppArgb);

                    //Copy result from device to host
                    dest_c4.CopyToHost(res);

                    pictureBox_dest.Image = res;

                    //Compute new histogram and show it
                    dest_c4.HistogramEven(bins_ds4, ll4, up4);
                    bins4[0]           = bins_ds4[0];
                    bins4[1]           = bins_ds4[1];
                    bins4[2]           = bins_ds4[2];
                    hist_rb_dest.Image = GetHistogramImage(bins4[2], bins4[1], bins4[0], 1);                           //r
                    hist_g_dest.Image  = GetHistogramImage(bins4[1], bins4[0], bins4[2], 2);                           //g
                    hist_b_dest.Image  = GetHistogramImage(bins4[0], bins4[1], bins4[2], 3);                           //b

                    //Free temp memory
                    bins_ds4[0].Dispose();
                    bins_ds4[1].Dispose();
                    bins_ds4[2].Dispose();
                    bins_ds4[3].Dispose();
                    break;
                }
            }
            catch (Exception ex)
            {
                if (ex is NPPException)
                {
                    txt_info.AppendText("NPPException: " + ex.Message + "\n");
                    CleanUp();
                }
                else if (ex is CudaException)
                {
                    txt_info.AppendText("CudaException: " + ex.Message + "\n");
                    CleanUp();
                }
                else
                {
                    throw;
                }
            }
        }