コード例 #1
0
ファイル: Form1.cs プロジェクト: furusdara/cuda
        private ManagedCuda.NPP.NPPImage_8uC4 CudaHelper(Bitmap map)
        {
            NppiSize size = new NppiSize(map.Width, map.Height);

            ManagedCuda.NPP.NPPImage_8uC4 source = new NPPImage_8uC4(size);

            source.CopyToDevice(map);

            return(source);
        }
コード例 #2
0
        private void AllocateImagesNPP(Bitmap size)
        {
            int w = size.Width;
            int h = size.Height;

            if (inputImage8uC3 == null)
            {
                inputImage8uC1 = new NPPImage_8uC1(w, h);
                inputImage8uC3 = new NPPImage_8uC3(w, h);
                inputImage8uC4 = new NPPImage_8uC4(w, h);
                imageBayer     = new NPPImage_32fC1(w, h);
                inputImage32f  = new NPPImage_32fC3(w, h);
                noisyImage8u   = new NPPImage_8uC3(w, h);
                noiseImage32f  = new NPPImage_32fC3(w, h);
                resultImage8u  = new NPPImage_8uC3(w, h);
                resultImage32f = new NPPImage_32fC3(w, h);
                return;
            }

            if (inputImage8uC3.Width >= w && inputImage8uC3.Height >= h)
            {
                inputImage8uC1.SetRoi(0, 0, w, h);
                inputImage8uC3.SetRoi(0, 0, w, h);
                inputImage8uC4.SetRoi(0, 0, w, h);
                imageBayer.SetRoi(0, 0, w, h);
                inputImage32f.SetRoi(0, 0, w, h);
                noisyImage8u.SetRoi(0, 0, w, h);
                noiseImage32f.SetRoi(0, 0, w, h);
                resultImage8u.SetRoi(0, 0, w, h);
                resultImage32f.SetRoi(0, 0, w, h);
            }
            else
            {
                inputImage8uC1.Dispose();
                inputImage8uC3.Dispose();
                inputImage8uC4.Dispose();
                imageBayer.Dispose();
                inputImage32f.Dispose();
                noisyImage8u.Dispose();
                noiseImage32f.Dispose();
                resultImage8u.Dispose();
                resultImage32f.Dispose();

                inputImage8uC1 = new NPPImage_8uC1(w, h);
                inputImage8uC3 = new NPPImage_8uC3(w, h);
                inputImage8uC4 = new NPPImage_8uC4(w, h);
                imageBayer     = new NPPImage_32fC1(w, h);
                inputImage32f  = new NPPImage_32fC3(w, h);
                noisyImage8u   = new NPPImage_8uC3(w, h);
                noiseImage32f  = new NPPImage_32fC3(w, h);
                resultImage8u  = new NPPImage_8uC3(w, h);
                resultImage32f = new NPPImage_32fC3(w, h);
            }
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: furusdara/cuda
        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);
        }
コード例 #4
0
        private void btn_open_Click(object sender, EventArgs e)
        {
            if (!_nppOK)
            {
                return;
            }

            CleanUp();

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Images|*.jpg;*.bmp;*.png;*.tif";
            if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            Bitmap src = new Bitmap(ofd.FileName);

            switch (src.PixelFormat)
            {
            case PixelFormat.Format24bppRgb:
                _colorChannels = 3;
                break;

            case PixelFormat.Format32bppArgb:
                _colorChannels = 4;
                break;

            case PixelFormat.Format32bppRgb:
                _colorChannels = 4;
                break;

            case PixelFormat.Format8bppIndexed:
                _colorChannels = 1;
                break;

            default:
                _colorChannels = 0;
                txt_info.AppendText(ofd.FileName + " has an unsupported pixel format.\n");
                break;
            }

            try
            {
                switch (_colorChannels)
                {
                case 1:
                    //Allocate memory on device for one channel images...
                    src_c1  = new NPPImage_8uC1(src.Width, src.Height);
                    dest_c1 = new NPPImage_8uC1(src.Width, src.Height);
                    src_c1.CopyToDevice(src);
                    txt_info.AppendText("Info: Loaded image '" + ofd.FileName + "' succesfully (Size: " + src.Width.ToString() + " x " + src.Height.ToString() + ", color channels: " + _colorChannels.ToString() + ")\n");
                    break;

                case 3:
                    //As of version 5, NPP has new histogram and LUT functions for three channel images, no more need to convert first to 4 channels.
                    //Allocate memory on device for four channel images...
                    src_c3  = new NPPImage_8uC3(src.Width, src.Height);
                    dest_c3 = new NPPImage_8uC3(src.Width, src.Height);

                    //Fill 3 channel image in device memory
                    src_c3.CopyToDevice(src);

                    txt_info.AppendText("Info: Loaded image '" + ofd.FileName + "' succesfully (Size: " + src.Width.ToString() + " x " + src.Height.ToString() + ", color channels: " + _colorChannels.ToString() + ")\n");
                    break;

                case 4:
                    //Allocate memory on device for four channel images...
                    src_c4  = new NPPImage_8uC4(src.Width, src.Height);
                    dest_c4 = new NPPImage_8uC4(src.Width, src.Height);
                    src_c4.CopyToDevice(src);
                    txt_info.AppendText("Info: Loaded image '" + ofd.FileName + "' succesfully (Size: " + src.Width.ToString() + " x " + src.Height.ToString() + ", color channels: " + _colorChannels.ToString() + ")\n");
                    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;
                }
            }
            //Show original image
            pictureBox_src.Image = src;
        }
コード例 #5
0
        private void btn_openImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Images|*.bmp;*.jpg;*.jpeg;*.tiff;*.tif;*.png;*.gif";
            if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            bmp_src = new Bitmap(ofd.FileName);

            if (bmp_src.PixelFormat != PixelFormat.Format24bppRgb)
            {
                MessageBox.Show("Only 24-bit RGB images are supported!");
                bmp_src  = null;
                bmp_mask = null;
                bmp_res  = null;
                if (npp_bmp_src != null)
                {
                    npp_bmp_src.Dispose();
                }
                if (npp_bmp_res != null)
                {
                    npp_bmp_res.Dispose();
                }
                if (npp_bmp_mask != null)
                {
                    npp_bmp_mask.Dispose();
                }
                if (d_bmp_src != null)
                {
                    d_bmp_src.Dispose();
                }
                if (d_bmp_res != null)
                {
                    d_bmp_res.Dispose();
                }
                if (d_bmp_mask != null)
                {
                    d_bmp_mask.Dispose();
                }
                return;
            }
            width    = bmp_src.Width;
            height   = bmp_src.Height;
            marker   = new int[width * height];
            bmp_res  = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            bmp_mask = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
            SetPalette(bmp_mask);
            pictureBox_src.Image = bmp_src;

            selection.x      = (int)Math.Ceiling(width * 0.1);
            selection.y      = (int)Math.Ceiling(height * 0.1);
            selection.width  = width - 2 * selection.x;
            selection.height = height - 2 * selection.y;

            if (npp_bmp_src != null)
            {
                npp_bmp_src.Dispose();
            }
            if (npp_bmp_res != null)
            {
                npp_bmp_res.Dispose();
            }
            if (npp_bmp_mask != null)
            {
                npp_bmp_mask.Dispose();
            }
            if (d_bmp_src != null)
            {
                d_bmp_src.Dispose();
            }
            if (d_bmp_res != null)
            {
                d_bmp_res.Dispose();
            }
            if (d_bmp_mask != null)
            {
                d_bmp_mask.Dispose();
            }

            NPPImage_8uC3 npp_temp = new NPPImage_8uC3(width, height);
            CudaPitchedDeviceVariable <uchar3> d_bmp_temp = new CudaPitchedDeviceVariable <uchar3>(npp_temp.DevicePointer, width, height, npp_temp.Pitch);

            npp_temp.CopyToDevice(bmp_src);

            npp_bmp_src  = new NPPImage_8uC4(width, height);
            npp_bmp_res  = new NPPImage_8uC4(width, height);
            npp_bmp_mask = new NPPImage_8uC1(width, height);
            d_bmp_src    = new CudaPitchedDeviceVariable <uchar4>(npp_bmp_src.DevicePointer, width, height, npp_bmp_src.Pitch);
            d_bmp_res    = new CudaPitchedDeviceVariable <uchar4>(npp_bmp_res.DevicePointer, width, height, npp_bmp_res.Pitch);
            d_bmp_mask   = new CudaPitchedDeviceVariable <byte>(npp_bmp_mask.DevicePointer, width, height, npp_bmp_mask.Pitch);

            grabcut = new GrabCut(d_bmp_src, d_bmp_mask, width, height);
            grabcut.grabCutUtils.convertRGBToRGBA(d_bmp_src, d_bmp_temp, width, height);
            d_bmp_temp.Dispose();
            npp_temp.Dispose();
        }