0

I am playing with images changing their contrast but I don't know how to restore the contrast back of an image after modifying it first.

I understand with a value greater than 1 I increase the contrast and with a value between 0 and 1 I decrease it.

I tried with OpenCV and with a ColorMatrix in Android.

For example, using OpenCV, first I double the contrast of an image like this:

src.convertTo(dst, -1, 2, 0);

and then I decrease it by half:

src.convertTo(dst, -1, 0.5, 0);

but after decreasing it the resulting image is not the same as the original one I had before doubling the contrast.

With Android, I was using this colorMatrix to double the contrast:

ColorMatrix cm = new ColorMatrix();
        cm.set(new float[] {
                2, 0, 0, 0, 0,
                0, 2, 0, 0, 0,
                0, 0, 2, 0, 0,
                0, 0, 0, 1, 0});

and this to decrease it by half:

ColorMatrix cm = new ColorMatrix();
        cm.set(new float[] {
                0.5, 0, 0, 0, 0,
                0, 0.5, 0, 0, 0,
                0, 0, 0.5, 0, 0,
                0, 0, 0, 1, 0});

I tried with different values and research on the Internet but I can't figure out the equivalences between increasing and decreasing contrast.

Does anybody know how to do this?

mlg
  • 5,248
  • 1
  • 14
  • 17
  • I would say its not reversible without simply reverting to an image backup – Scott Stensland Jul 06 '17 at 11:48
  • that came to my mind too but then I checked apps like Instagram or Google Photos that have a bar to modify contrast and looks so smooth that made me think it is possible – mlg Jul 06 '17 at 11:53
  • 1
    possibly each invocation of a new setting is starting from a fresh copy of the original – Scott Stensland Jul 06 '17 at 13:10
  • 1
    [`convertTo`](http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat-convertto) saturates the result. In case of a matrix of 8bit unsigned values, that means that when you multiply by 2, all values greater of equal to 128 become 255. IMHO there's little hope of developing functional software when your not aware of the data types nor the behaviour of the functions you're using. – Dan Mašek Jul 06 '17 at 13:21
  • It's very well explained here: https://stackoverflow.com/questions/13939520/formula-for-adjusting-brightness-contrast-on-canvas – mlg Jul 06 '17 at 17:24

1 Answers1

1

Hi you may change contrast using below method

  /**
     * Set contrast and brightness in bitmap
     *
     * @param bmp        bitmap to change
     * @param contrast   contrast value from 1 to 10
     * @param brightness brightness value from 1 to 100
     * @return tuned bitmap
     */
    public static Bitmap ChangeBitmapContrastBrightness(Bitmap bmp, float contrast, float brightness) {
        ColorMatrix cm = new ColorMatrix(new float[]
                {
                        contrast, 0, 0, 0, brightness,
                        0, contrast, 0, 0, brightness,
                        0, 0, contrast, 0, brightness,
                        0, 0, 0, 1, 0
                });

        Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

        Canvas canvas = new Canvas(ret);

        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(bmp, 0, 0, paint);

        return ret;
    }
Niraj Sanghani
  • 1,493
  • 15
  • 23