简体   繁体   中英

Remove background from Canvas image

When I upload any image to the canvas tag I wish I could remove the background around the logo. This is my code:

jQuery 1.12.4 and jQuery UI 1.9.2 used

<canvas id="canvas" class="resize" style="width:200px;height:200px;"></canvas>
<input type="file" id="file-input">

<button id="removeBKG" onclick="removeBKG();">
RIMUOVI SFONDO
</button>

<script>
$(function() {
            
            $('#file-input').change(function(e) {
                var file = e.target.files[0],
                    imageType = /image.*/;

                if (!file.type.match(imageType))
                    return;

                var reader = new FileReader();
                reader.onload = fileOnload;
                reader.readAsDataURL(file);
            });

            function fileOnload(e) {
                var $img = $('<img>', { src: e.target.result });
                $img.load(function() {
                    var canvas = $('#canvas')[0];
                    var context = canvas.getContext('2d');

                    canvas.width = this.naturalWidth;
                    canvas.height = this.naturalHeight;
                    context.drawImage(this, 0, 0);
                });
            }
            
            $(document).mouseup(function(e) 
            {
                var container = $(".ui-wrapper");

                // if the target of the click isn't the container nor a descendant of the container
                if (!container.is(e.target) && container.has(e.target).length === 0) 
                {
                    $("#canvas").css("border-style","hidden");
                    $(".ui-resizable-handle").attr("style","visibility: hidden");
                } else {
                        $("#canvas").css("border-style","solid");
                        $(".ui-resizable-handle").attr("style","visibility: visible");
                }
            });
            
            
                window.zindex = 30;

                $(".resize").resizable({handles: 'ne, se, sw, nw'});
                $(".resize").parent().draggable({
                    stack: "div"
                });
            
        });
    
    
    function removeBKG(){
    var canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d"),
    image = document.getElementById("canvas");

canvas.height = canvas.width = 135;
ctx.drawImage(image,0,0);
    
var imgd = ctx.getImageData(0, 0, 135, 135),
    pix = imgd.data,
    newColor = {r:0,g:0,b:0, a:0};

for (var i = 0, n = pix.length; i <n; i += 4) {
    var r = pix[i],
            g = pix[i+1],
            b = pix[i+2];

            pix[i] = newColor.r;
            pix[i+1] = newColor.g;
            pix[i+2] = newColor.b;
            pix[i+3] = newColor.a;
}

ctx.putImageData(imgd, 0, 0);
}
</script>

Expected result when I click on the function: The background of the image is removed and only the logo remains visible.

Current result: When I click on the function the entire image is removed

Where is the issue?

This is an little example of work that i would:

http://jsfiddle.net/loktar/BtbSM/

Another example:

Image loaded with background

在此处输入图像描述

When clicked, the image must be without background

在此处输入图像描述

With the given image, you need to keep the blue part and remove the gray part

  • the value of the red channel of the blue part is low (<3)
  • the value of the red channel of the gray part is ~100

You can safely remove all the pixel whose red channel is greater than 10 , so this part of the code becomes

for (var i = 0, n = pix.length; i <n; i += 4) {
    var r = pix[i];


    if(r > 10) { 
        // overwrite gray pixel with white pixel
        pix[i] = pix[i+1] = pix[i+2] = 255;
        // alpha channel
        pix[i+3] = 255;
    }
}

of course this script can't work with other images with different colours because the information about it is hardcoded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM