Example #1
2
        public static Bitmap Binarize(Bitmap bm, double threshhold)
        {
            //Binarize
            int size = bm.Width * bm.Height;
            int[] pixels = new int[size];
            bm.GetPixels( pixels, 0, bm.Width, 0, 0, bm.Width, bm.Height );

            // Calculate overall lightness of image
            int c;
            for (int i = 0; i < size; i++)
            {
                c = pixels[i];
                double whiteDist = Math.Sqrt(Math.Pow(0xff - ((c&0x00FF0000 )>>16),2) + Math.Pow(0xff - ((c & 0x0000FF00 )>>8), 2) + Math.Pow(0xff - (c&0x000000FF), 2));
                double blackDist = Math.Sqrt(Math.Pow(0x00 - ((c&0x00FF0000 )>>16),2) + Math.Pow(0x00 - ((c & 0x0000FF00 )>>8), 2) + Math.Pow(0x00 - (c&0x000000FF), 2));
                double distance = blackDist + whiteDist;

                if (whiteDist / distance > threshhold / 30.0) {
                    pixels [i] = Color.Black;
                } else {
                    pixels [i] = Color.White;
                }
            }

            Bitmap newBitmap = bm.Copy (bm.GetConfig (), true);
            newBitmap.SetPixels (pixels, 0, bm.Width, 0, 0, bm.Width, bm.Height);

            return newBitmap;
        }
        //Create the remove red button
        private void NoRed(object sender, System.EventArgs e)
        {
            TextView  tv_saved  = FindViewById <TextView>(Resource.Id.respondText);
            ImageView imageView = FindViewById <ImageView>(Resource.Id.takenPictureImageView);
            int       height    = Resources.DisplayMetrics.HeightPixels;
            int       width     = imageView.Height;

            Android.Graphics.Bitmap bitmap     = _file.Path.LoadAndResizeBitmap(width, height);
            Android.Graphics.Bitmap copyBitmap = bitmap.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);
            for (int i = 0; i < copyBitmap.Width; i++)
            {
                for (int j = 0; j < copyBitmap.Height; j++)
                {
                    int p = copyBitmap.GetPixel(i, j);
                    //00000000 00000000 00000000 00000000
                    //long mask = (long)0xFF00FFFF;
                    //p = p & (int)mask;
                    Android.Graphics.Color c = new Android.Graphics.Color(p);
                    c.R = 0;
                    copyBitmap.SetPixel(i, j, c);
                }
            }
            if (bitmap != null)
            {
                imageView.SetImageBitmap(copyBitmap);
                imageView.Visibility = Android.Views.ViewStates.Visible;
            }

            System.GC.Collect();
            tv_saved.SetTextKeepState("Red Removed!");
        }
Example #3
0
        public static Android.Graphics.Bitmap mergeBitmap(Android.Graphics.Bitmap backBitmap, Android.Graphics.Bitmap frontBitmap)
        {
            Android.Graphics.Bitmap bitmap = backBitmap.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);
            Canvas canvas    = new Canvas(bitmap);
            Rect   baseRect  = new Rect(0, 0, backBitmap.Width, backBitmap.Height);
            Rect   frontRect = new Rect(0, 0, frontBitmap.Width, frontBitmap.Height);

            canvas.DrawBitmap(frontBitmap, frontRect, baseRect, null);
            return(bitmap);
        }
        // Called automatically whenever an activity finishes
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            SetContentView(Resource.Layout.PicManip);
            bool reverted = true;


            Button remred    = FindViewById <Button>(Resource.Id.RemRed);
            Button remgreen  = FindViewById <Button>(Resource.Id.RemGreen);
            Button remblue   = FindViewById <Button>(Resource.Id.RemBlue);
            Button negred    = FindViewById <Button>(Resource.Id.NegRed);
            Button neggreen  = FindViewById <Button>(Resource.Id.NegGreen);
            Button negblue   = FindViewById <Button>(Resource.Id.NegBlue);
            Button greyscale = FindViewById <Button>(Resource.Id.Greyscale);
            Button high_cont = FindViewById <Button>(Resource.Id.HighContrast);
            Button add_noise = FindViewById <Button>(Resource.Id.AddNoise);
            Button revert    = FindViewById <Button>(Resource.Id.Revert);
            Button save      = FindViewById <Button>(Resource.Id.Save);

            //test to make sure the picture was found
            if ((resultCode == Result.Ok) && (data != null))
            {
                //Make image available in the gallery
                //Test to see if we came from the camera or gallery
                //If we came from galley no need to make pic available
                if (requestCode == 0)
                {
                    Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                    var    contentUri      = Android.Net.Uri.FromFile(_file);
                    mediaScanIntent.SetData(contentUri);
                    SendBroadcast(mediaScanIntent);
                }
            }

            // Display in ImageView. We will resize the bitmap to fit the display.
            // Loading the full sized image will consume too much memory
            // and cause the application to crash.
            ImageView imageView = FindViewById <ImageView>(Resource.Id.takenPictureImageView);
            int       height    = Resources.DisplayMetrics.HeightPixels;
            int       width     = imageView.Height;

            bitmap = (Android.Graphics.Bitmap)data.Extras.Get("data");
            bitmap = Android.Graphics.Bitmap.CreateScaledBitmap(bitmap, 1024, 768, true);

            //check to make sure the bitmap has data we can manipulate
            if (bitmap != null)
            {
                copyBitmap = bitmap.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);
                imageView.SetImageBitmap(copyBitmap);
            }

            else
            {
                //If bitmap is null takes the user back to the original screen
                StartMainLayout();
            }

            //Removes the red in the picture by setting the red pixel to 0
            remred.Click += delegate
            {
                for (int i = 0; i < copyBitmap.Width; i++)
                {
                    for (int j = 0; j < copyBitmap.Height; j++)
                    {
                        int p = copyBitmap.GetPixel(i, j);
                        Android.Graphics.Color c = new Android.Graphics.Color(p);

                        c.R = 0;
                        copyBitmap.SetPixel(i, j, c);
                    }
                }

                imageView.SetImageBitmap(copyBitmap);
                reverted = false;
            };

            //Removes the green in the picture by setting the green pixel to 0
            remgreen.Click += delegate
            {
                for (int i = 0; i < copyBitmap.Width; i++)
                {
                    for (int j = 0; j < copyBitmap.Height; j++)
                    {
                        int p = copyBitmap.GetPixel(i, j);
                        Android.Graphics.Color c = new Android.Graphics.Color(p);

                        c.G = 0;
                        copyBitmap.SetPixel(i, j, c);
                    }
                }

                imageView.SetImageBitmap(copyBitmap);
                reverted = false;
            };

            //Removes the blue in the picture by setting the blue pixel to 0
            remblue.Click += delegate
            {
                for (int i = 0; i < copyBitmap.Width; i++)
                {
                    for (int j = 0; j < copyBitmap.Height; j++)
                    {
                        int p = copyBitmap.GetPixel(i, j);
                        Android.Graphics.Color c = new Android.Graphics.Color(p);

                        c.B = 0;
                        copyBitmap.SetPixel(i, j, c);
                    }
                }

                imageView.SetImageBitmap(copyBitmap);
                reverted = false;
            };

            //Negates the red in the picture by subtracting 255 from the current red pixels value
            negred.Click += delegate
            {
                for (int i = 0; i < copyBitmap.Width; i++)
                {
                    for (int j = 0; j < copyBitmap.Height; j++)
                    {
                        int p = copyBitmap.GetPixel(i, j);
                        Android.Graphics.Color c = new Android.Graphics.Color(p);
                        int r1 = c.R;
                        r1  = 255 - r1;
                        c.R = Convert.ToByte(r1);
                        copyBitmap.SetPixel(i, j, c);
                    }
                }

                imageView.SetImageBitmap(copyBitmap);
                reverted = false;
            };

            //Negates the green in the picture by subtracting 255 from the current green pixels value
            neggreen.Click += delegate
            {
                for (int i = 0; i < copyBitmap.Width; i++)
                {
                    for (int j = 0; j < copyBitmap.Height; j++)
                    {
                        int p = copyBitmap.GetPixel(i, j);
                        Android.Graphics.Color c = new Android.Graphics.Color(p);
                        int g1 = c.G;
                        g1  = 255 - g1;
                        c.G = Convert.ToByte(g1);
                        copyBitmap.SetPixel(i, j, c);
                    }
                }

                imageView.SetImageBitmap(copyBitmap);
                reverted = false;
            };

            //Negates the blue in the picture by subtracting 255 from the current blue pixels value
            negblue.Click += delegate
            {
                for (int i = 0; i < copyBitmap.Width; i++)
                {
                    for (int j = 0; j < copyBitmap.Height; j++)
                    {
                        int p = copyBitmap.GetPixel(i, j);
                        Android.Graphics.Color c = new Android.Graphics.Color(p);
                        int b1 = c.B;
                        b1  = 255 - b1;
                        c.B = Convert.ToByte(b1);
                        copyBitmap.SetPixel(i, j, c);
                    }
                }

                imageView.SetImageBitmap(copyBitmap);
                reverted = false;
            };

            //Converts the picture to greyscale by averaging all the pixels values
            //Then setting the each pixel to the average
            greyscale.Click += delegate
            {
                for (int i = 0; i < copyBitmap.Width; i++)
                {
                    for (int j = 0; j < copyBitmap.Height; j++)
                    {
                        int average = 0;
                        int p       = copyBitmap.GetPixel(i, j);
                        Android.Graphics.Color c = new Android.Graphics.Color(p);
                        int r_temp = c.R;
                        int g_temp = c.G;
                        int b_temp = c.B;

                        average = (r_temp + g_temp + b_temp) / 3;

                        c.R = Convert.ToByte(average);
                        c.G = Convert.ToByte(average);
                        c.B = Convert.ToByte(average);
                        copyBitmap.SetPixel(i, j, c);
                    }
                }
                imageView.SetImageBitmap(copyBitmap);
                reverted = false;
            };

            //Converts the picture to high contrast
            //If the pixel > 127.5, then set to 255, else set to 0
            high_cont.Click += delegate
            {
                for (int i = 0; i < copyBitmap.Width; i++)
                {
                    for (int j = 0; j < copyBitmap.Height; j++)
                    {
                        int check_num            = 255 / 2;
                        int p                    = copyBitmap.GetPixel(i, j);
                        Android.Graphics.Color c = new Android.Graphics.Color(p);
                        int r_temp               = c.R;
                        int g_temp               = c.G;
                        int b_temp               = c.B;

                        if (r_temp > check_num)
                        {
                            r_temp = 255;
                        }

                        else
                        {
                            r_temp = 0;
                        }

                        if (g_temp > check_num)
                        {
                            g_temp = 255;
                        }

                        else
                        {
                            g_temp = 0;
                        }

                        if (b_temp > check_num)
                        {
                            b_temp = 255;
                        }

                        else
                        {
                            b_temp = 0;
                        }

                        c.R = Convert.ToByte(r_temp);
                        c.G = Convert.ToByte(g_temp);
                        c.B = Convert.ToByte(b_temp);
                        copyBitmap.SetPixel(i, j, c);
                    }
                }
                imageView.SetImageBitmap(copyBitmap);
                reverted = false;
            };

            //Adds random noise to the picture
            //Get a random value from -10 - 10, and add it to the pixels value
            //making sure the pixel value doesn't go over 255 or under 0.
            add_noise.Click += delegate
            {
                Random rnd = new Random();

                for (int i = 0; i < copyBitmap.Width; i++)
                {
                    for (int j = 0; j < copyBitmap.Height; j++)
                    {
                        int p = copyBitmap.GetPixel(i, j);
                        Android.Graphics.Color c = new Android.Graphics.Color(p);
                        int rand_val             = rnd.Next(-10, 11);
                        int r_temp = c.R;
                        int g_temp = c.G;
                        int b_temp = c.B;

                        r_temp += rand_val;
                        g_temp += rand_val;
                        b_temp += rand_val;

                        if (r_temp > 255)
                        {
                            r_temp = 255;
                        }
                        else if (r_temp < 0)
                        {
                            r_temp = 0;
                        }

                        if (g_temp > 255)
                        {
                            g_temp = 255;
                        }
                        else if (g_temp < 0)
                        {
                            g_temp = 0;
                        }

                        if (b_temp > 255)
                        {
                            b_temp = 255;
                        }
                        else if (b_temp < 0)
                        {
                            b_temp = 0;
                        }

                        c.R = Convert.ToByte(r_temp);
                        c.G = Convert.ToByte(g_temp);
                        c.B = Convert.ToByte(b_temp);
                        copyBitmap.SetPixel(i, j, c);
                    }
                }

                imageView.SetImageBitmap(copyBitmap);
                reverted = false;
            };

            //Button used to revert the image effects back to the orginal picture
            revert.Click += delegate
            {
                if (bitmap != null && !reverted)
                {
                    copyBitmap = bitmap.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);
                    imageView.SetImageBitmap(copyBitmap);
                    reverted = true;
                }

                else if (reverted)
                {
                    Toast.MakeText(this, "The picture is the original.", ToastLength.Short).Show();
                }
            };

            //Saves the image to the users gallery
            save.Click += delegate
            {
                var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
                var filePath   = System.IO.Path.Combine(sdCardPath, "ImageManip_{0}.png");
                var stream     = new FileStream(filePath, FileMode.Create);
                copyBitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
                stream.Close();
                Toast.MakeText(this, "The picture was saved to gallery.", ToastLength.Short).Show();

                StartMainLayout();
            };

            // Dispose of the Java side bitmap.
            System.GC.Collect();
        }
		// Source: http://incubator.quasimondo.com/processing/superfast_blur.php
		public static Bitmap ToLegacyBlurred(Bitmap source, Context context, int radius)
		{
			Bitmap img = source.Copy(source.GetConfig(), true);

			int w = img.Width;
			int h = img.Height;
			int wm = w-1;
			int hm = h-1;
			int wh = w*h;
			int div = radius+radius+1;
			int[] r = new int[wh];
			int[] g = new int[wh];
			int[] b = new int[wh];
			int rsum,gsum,bsum,x,y,i,p,p1,p2,yp,yi,yw;
			int[] vmin = new int[Math.Max(w,h)];
			int[] vmax = new int[Math.Max(w,h)];
			int[] pix= new int[w*h];

			img.GetPixels(pix, 0, w, 0,0,w, h);

			int[] dv = new int[256*div];
			for (i=0;i<256*div;i++){
				dv[i]=(i/div);
			}

			yw=yi=0;

			for (y=0;y<h;y++){
				rsum=gsum=bsum=0;
				for(i=-radius;i<=radius;i++){
					p=pix[yi+Math.Min(wm,Math.Max(i,0))];
					rsum+=(p & 0xff0000)>>16;
					gsum+=(p & 0x00ff00)>>8;
					bsum+= p & 0x0000ff;
				}
				for (x=0;x<w;x++){

					r[yi]=dv[rsum];
					g[yi]=dv[gsum];
					b[yi]=dv[bsum];

					if(y==0){
						vmin[x]=Math.Min(x+radius+1,wm);
						vmax[x]=Math.Max(x-radius,0);
					}
					p1=pix[yw+vmin[x]];
					p2=pix[yw+vmax[x]];

					rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16;
					gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8;
					bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff);
					yi++;
				}
				yw+=w;
			}

			for (x=0;x<w;x++){
				rsum=gsum=bsum=0;
				yp=-radius*w;
				for(i=-radius;i<=radius;i++){
					yi=Math.Max(0,yp)+x;
					rsum+=r[yi];
					gsum+=g[yi];
					bsum+=b[yi];
					yp+=w;
				}
				yi=x;
				for (y=0;y<h;y++){
					// Preserve alpha channel: ( 0xff000000 & pix[yi] )
					pix[yi] = (int)((0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum]);
					if(x==0){
						vmin[y]=Math.Min(y+radius+1,hm)*w;
						vmax[y]=Math.Max(y-radius,0)*w;
					}
					p1=x+vmin[y];
					p2=x+vmax[y];

					rsum+=r[p1]-r[p2];
					gsum+=g[p1]-g[p2];
					bsum+=b[p1]-b[p2];

					yi+=w;
				}
			}

			img.SetPixels(pix,0, w,0,0,w,h);
			return img;
		}
Example #6
0
        /// <summary>
        /// Mark bitmap with given face information
        /// </summary>
        /// <param name="originalBitmap"></param>
        /// <param name="faces"></param>
        /// <returns></returns>
        private static Bitmap drawFaceRectanglesOnBitmap(Bitmap originalBitmap, Face[] faces)
        {
            Bitmap bitmap = originalBitmap.Copy(Bitmap.Config.Argb8888, true);
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            paint.Color = StrokeColor;
            paint.StrokeWidth = strokeWidth;
            paint.TextSize = textSize;
            if (faces != null)
            {
                foreach(Face face in faces)
                {
                    FaceRectangle faceRectangle = face.FaceRectangle;

                    paint.SetStyle(Paint.Style.Stroke);
                    canvas.DrawRect(
                            faceRectangle.Left,
                            faceRectangle.Top,
                            faceRectangle.Left + faceRectangle.Width,
                            faceRectangle.Top + faceRectangle.Height,
                            paint);

                    paint.SetStyle(Paint.Style.Fill);
                    canvas.DrawText(face.Attributes.Gender + ", " + 
                        face.Attributes.Age + " y/o", 
                        faceRectangle.Left, faceRectangle.Top - textSize, paint);
                }
            }
            return bitmap;
        }
        private static Bitmap ChangeColor(Bitmap bitmap, Color fromColor, Color targetColor, float tolerance = 20)
        {
            int width = bitmap.Width;
            int height = bitmap.Height;
            int[] pixels = new int[width * height];

            float[] redRange = new float[2]{
                (float)Math.Max(fromColor.R - (tolerance / 2), 0.0),
                (float)Math.Min(fromColor.R + (tolerance / 2), 255.0)};

            float[] greenRange = new float[2]{
                (float)Math.Max(fromColor.G - (tolerance / 2), 0.0),
                (float)Math.Min(fromColor.G + (tolerance / 2), 255.0)};

            float[] blueRange = new float[2]{
                (float)Math.Max(fromColor.B - (tolerance / 2), 0.0),
                (float)Math.Min(fromColor.B + (tolerance / 2), 255.0)};

            bitmap.GetPixels(pixels, 0, width, 0, 0, width, height);

            for (int i = 0; i < pixels.Length; i++)
            {

                if (pixels[i] == fromColor)
                {
                    pixels[i] = new Color(targetColor.R, targetColor.G, targetColor.B, targetColor.A - 1);
                }

                int red = Color.GetRedComponent(pixels[i]);
                int green = Color.GetGreenComponent(pixels[i]);
                int blue = Color.GetBlueComponent(pixels[i]);
                int alpha = Color.GetAlphaComponent(pixels[i]);

                if (((red >= redRange[0]) && (red <= redRange[1])) &&
                    ((green >= greenRange[0]) && (green <= greenRange[1])) &&
                    ((blue >= blueRange[0]) && (blue <= blueRange[1])) &&
                    ((alpha > 0 && alpha < 254)))
                {
                    pixels[i] = new Color(targetColor.R, targetColor.G, targetColor.B, targetColor.A - 1);
                }
            }

            if (bitmap.IsMutable)
            {
                bitmap.SetPixels(pixels, 0, width, 0, 0, width, height);
                return bitmap;
            }
            else
            {
                var mutableBitmap = bitmap.Copy(bitmap.GetConfig(), true);
                mutableBitmap.SetPixels(pixels, 0, width, 0, 0, width, height);
                return mutableBitmap;
            }
        }