photo/image-to-sketch algorithm

前端 未结 5 1206
生来不讨喜
生来不讨喜 2020-12-12 20:06

Does anyone have an idea, link, library, source code, ... on how to convert photo\'s and images (bitmaps) to sketchy-like pictures? I can\'t find any good sources on how to

5条回答
  •  萌比男神i
    2020-12-12 20:18

    And adding color.

    *s = Read-File-Into-Image("/path/to/image")
    *g = Convert-To-Gray-Scale(s)
    *i = Invert-Colors(g)
    *b = Apply-Gaussian-Blur(i)
    *result = Color-Dodge-Blend-Merge(b,g)   
    *s2 = Apply-Gaussian-Blur(s) //I use radius 3
    *cartoon = Apply-Color(s2, result)
    

    I little modification to ColorDodgeBlend to eliminate all colors.

    public Bitmap ColorDodgeBlend(Bitmap source, Bitmap layer) 
    ....
    //before buffOut.put(pixel);
    
    float[] hsv = new float[3];
            Color.colorToHSV(pixel, hsv);
            hsv[1] = 0.0f;
            float top = VALUE_TOP; //Between 0.0f .. 1.0f I use 0.87f
            if (hsv[2] <= top) {
                hsv[2] = 0.0f;
            } else {
                hsv[2] = 1.0f;
            }
            pixel = Color.HSVToColor(hsv);
    

    An the applying color method:

    //hue, saturarion, value intervals size are for reduce colors on Bitmap
    //saturation, value percents are for increment or decrement [0..100..)
    public Bitmap getCartoonizedBitmap(Bitmap realBitmap, Bitmap dodgeBlendBitmap, int hueIntervalSize, int saturationIntervalSize, int valueIntervalSize, int saturationPercent, int valuePercent) {
        // Bitmap bitmap = Bitmap.createBitmap(scaledBitmap);
        // //fastblur(scaledBitmap, 4);
        Bitmap base = fastblur(realBitmap, 3).copy(Config.ARGB_8888, true);
        Bitmap dodge = dodgeBlendBitmap.copy(Config.ARGB_8888, false);
        try {
            int realColor;
            int color;
            float top = VALUE_TOP; //Between 0.0f .. 1.0f I use 0.87f
            IntBuffer templatePixels = IntBuffer.allocate(dodge.getWidth()
                    * dodge.getHeight());
            IntBuffer scaledPixels = IntBuffer.allocate(base.getWidth()
                    * base.getHeight());
            IntBuffer buffOut = IntBuffer.allocate(base.getWidth()
                    * base.getHeight());
    
            base.copyPixelsToBuffer(scaledPixels);
            dodge.copyPixelsToBuffer(templatePixels);
    
            templatePixels.rewind();
            scaledPixels.rewind();
            buffOut.rewind();
    
            while (buffOut.position() < buffOut.limit()) {
                color = (templatePixels.get());
                realColor = scaledPixels.get();
    
                float[] realHSV = new float[3];
                Color.colorToHSV(realColor, realHSV);
    
                realHSV[0] = getRoundedValue(realHSV[0], hueIntervalSize);
    
                realHSV[2] = (getRoundedValue(realHSV[2] * 100,
                        valueIntervalSize) / 100) * (valuePercent / 100);
                realHSV[2] = realHSV[2]<1.0?realHSV[2]:1.0f;
    
                realHSV[1] = realHSV[1] * (saturationPercent / 100);
                realHSV[1] = realHSV[1]<1.0?realHSV[1]:1.0f;
    
                float[] HSV = new float[3];
                Color.colorToHSV(color, HSV);
    
                boolean putBlackPixel = HSV[2] <= top;
    
                realColor = Color.HSVToColor(realHSV);
    
                if (putBlackPixel) {
                    buffOut.put(color);
                } else {
                    buffOut.put(realColor);
                }
            }// END WHILE
            dodge.recycle();
            buffOut.rewind();
            base.copyPixelsFromBuffer(buffOut); 
    
        } catch (Exception e) {
            // TODO: handle exception
        }
    
        return base;
    }
    
    public static float getRoundedValue(float value, int intervalSize) {
            float result = Math.round(value);
            int mod = ((int) result) % intervalSize;
            result += mod < (intervalSize / 2) ? -mod : intervalSize - mod;
            return result;
    
        }
    

    This is not improved. Its better if Apply-Color and Color-Dodge-Blend-Merge merges.

    Thanks to XverhelstX for his Question-Answer

提交回复
热议问题