简体   繁体   English

如何将图像背景更改为白色?

[英]How to change the image background to white?

I have set of images. 我有一组图像。 Images have a simple background. 图像背景简单。 I want to change that background to white using Marvin Framework and Java. 我想使用Marvin Framework和Java将该背景更改为白色。

As I am new to Marvin, it is making me trouble to change the background. 因为我是Marvin的新手,所以改变背景让我很麻烦。 I also tried opencv for java but its giving unsatisfied link error. 我也尝试过opencv for java但它给出了不满意的链接错误。

Image Example: 图片示例:

在此输入图像描述

To get a perfect result you'll need to find a way to remove shadows. 要获得完美的结果,您需要找到一种去除阴影的方法。 But I think it is a good start point for you. 但我认为这对你来说是一个很好的起点。

Algorithm: 算法:

  1. Convert the image to binary color model (pixels are true or false) given a gray scale threshold. 给定灰度阈值,将图像转换为二进制颜色模型(像素为真或假)。
  2. Perform a morphological dilation for closing openings in the shoes boundary. 进行形态膨胀以封闭鞋边界的开口。
  3. Fill the background with color rgb(255,0,255) 用颜色rgb填充背景(255,0,255)
  4. After filling the background with a new color in the binary image, set the same pixels to white in the original image. 在二进制图像中使用新颜色填充背景后,在原始图像中将相同像素设置为白色。

output: 输出:

在此输入图像描述

source code: 源代码:

import static marvin.MarvinPluginCollection.*;

public class RemoveBackground {

    public RemoveBackground(){
        MarvinImage image = MarvinImageIO.loadImage("./res/shoes.jpg");
        MarvinImage bin = MarvinColorModelConverter.rgbToBinary(image, 116);
        morphologicalDilation(bin.clone(), bin, MarvinMath.getTrueMatrix(5, 5));
        MarvinImage mask = MarvinColorModelConverter.binaryToRgb(bin);
        boundaryFill(mask.clone(), mask, 5, 5, new Color(255,0,255));


        for(int y=0; y<mask.getHeight(); y++){
            for(int x=0; x<mask.getWidth(); x++){
                if(mask.getIntColor(x, y) == 0xFFFF00FF){
                    image.setIntColor(x, y, 255,255,255);
                }
            }
        }
        MarvinImageIO.saveImage(image, "./res/shoes_out.jpg");
    }

    public static void main(String[] args) {
        new RemoveBackground();
        System.exit(0);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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