Example #1
0
        void vcd_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            try
            {
                if (!processando)
                {
                    processando = true;

                    bmp = (Bitmap)eventArgs.Frame.Clone();
                    if (ImgDtWebCam == null)
                        ImgDtWebCam = new ImageData(bmp);
                    else ImgDtWebCam.ReadBmp(bmp);

                    //Filter
                    if (!frmFilter.IsDisposed) filterWebCam = frmFilter.GetFilters();

                    CLFilter.ApplyFilter(ImgDtWebCam, filterWebCam, true, WorkDim2);
                    bmp = ImgDtWebCam.GetStoredBitmap(bmp);

                    if (!sw.IsRunning) sw.Start();

                    nFrames++;
                    fRate = nFrames / sw.Elapsed.TotalSeconds;

                    this.Invoke(delegRefreshPic);

                    processando = false;
                }
            }
            catch (Exception ex)
            {
                this.Text = ex.ToString();
            }
        }
Example #2
0
        /// <summary>Applies given filter to the image</summary>
        /// <param name="imgDt">Image to be filtered</param>
        /// <param name="Filter">Filter. [3*size*size]</param>
        public static void ApplyFilter(ImageData imgDt, float[] Filter, bool useOpenCL, bool useWorkDim2)
        {
            int FilterSize = (int)Math.Sqrt(Filter.Length/3);

            if (Filter.Length != 3 * FilterSize * FilterSize)
                throw new Exception("Invalid filter");

            if (!Initialized && useOpenCL) Init(FilterSize);

            //Writes filter to device
            if(useOpenCL) varFilter.WriteToDevice(Filter);

            if (FilteredVals == null || FilteredVals.Length != imgDt.Height * imgDt.Width * 3)
            {
                //Filtered values
                FilteredVals = new float[imgDt.Height * imgDt.Width * 3];
                varFiltered = new CLCalc.Program.Variable(FilteredVals);
            }

            //Width
            if (useOpenCL) varWidth.WriteToDevice(new int[] { imgDt.Width });

            //Executes filtering
            int mean = (FilterSize - 1) / 2;
            if (useOpenCL)
            {
                CLCalc.Program.Variable[] args = new CLCalc.Program.Variable[] { imgDt.varData, varFilter, varFiltered, varWidth };
                if (useWorkDim2)
                {
                    kernelApplyFilterWorkDim2.Execute(args, new int[] { imgDt.Width - FilterSize, imgDt.Height - FilterSize });
                }
                else
                {
                    kernelApplyFilter.Execute(args, new int[] { imgDt.Height - FilterSize });
                }
                //Reads data back
                varFiltered.ReadFromDeviceTo(FilteredVals);

            }
            else
            {
                ApplyFilter(imgDt.Data, Filter, FilteredVals, new int[] { imgDt.Width }, imgDt.Height - FilterSize);
            }

            //Writes to image data
            for (int y = mean; y < imgDt.Height - mean - 1; y++)
            {
                int wy = imgDt.Width * y;
                for (int x = mean; x < imgDt.Width - mean - 1; x++)
                {
                    int ind = 3 * (x + wy);
                    imgDt.Data[ind] = (byte)FilteredVals[ind];
                    imgDt.Data[ind + 1] = (byte)FilteredVals[ind + 1];
                    imgDt.Data[ind + 2] = (byte)FilteredVals[ind + 2];
                }
            }

            //Writes filtered values
            //In the future this rewriting can be avoided
            //because byte_addressable will be widely available
            if (useOpenCL) imgDt.varData.WriteToDevice(imgDt.Data);
        }
Example #3
0
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Pictures|*.jpg";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    bmp = new Bitmap(ofd.FileName);
                    imgDt = new ImageData(bmp);
                    pic.Image = bmp;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }