SciTech-Mathmatics-ImageProcessing-Remove the Background Color or Image from an image using Python+NumPy+Pandas
https://www.geeksforgeeks.org/how-to-remove-the-background-from-an-image-using-python/
Remove Background Image
# Install required packages first.
# pip install Pillow
# pip install rembg
from rembg import remove
from PIL import Image
input_path = './RemoveBackGroundOriginal.png'
output_path = './Remove BackGroundOutput.png'
input = Image.open(input_path)
remove(input).save(output_path)
Remove Background Color
当背景颜色与图片主体部分的颜色有一定的区分度时,
就可以用这种办法把整片整片的背景颜色都替换成我们需要的颜色值。
我们直接就能用数值分析算法, 统计概率, 并以NumPy和Pandas实现数值统计。
# Usage: ipython --pdb removeColor.py 18
#! /usr/bin/env python
import itertools as it
import collections as ct
import cv2 as cv
import numpy as np
import pandas as pd
def calculate_top_color(image):
a = image.copy()
cnt = ct.Counter(a)
topTen = cnt.most_common(10)
topTenColors=[k for k,v in topTen]
topColor=np.array(topTenColors).mean()
return topColor
def replace_color(image, top=None, threshold=5, color=255):
color_threshold = threshold
a = image.copy()
s = pd.Series(image.flatten())
rows, cols = image.shape
topColor0 = top or calculate_top_color(s)
topColor = top or int(s.describe()['50%'])
colorMin = topColor - color_threshold
colorMax = topColor + color_threshold
print(s.describe(), '\n', "TopColor: %s, %s\n" % (topColor, topColor0))
for r in range(rows):
for c in range(cols):
val = a.item(r, c)
if colorMin <= val < colorMax:
a.itemset((r, c), color)
return a
def remove_top_color(img, top=None, threshold=18, color=255):
b, g, r =img[:, :, 0], img[:, :, 1], img[:, :, 2]
print("\nProcessing Color Channel: BLUE")
b = replace_color(b, top, threshold, color)
print("\nProcessing Color Channel: GREEN")
g = replace_color(g, top, threshold, color)
print("\nProcessing Color Channel: RED")
r = replace_color(r, top, threshold, color)
img0 = cv.merge((b, g, r))
return img0
if __name__ == "__main__":
import os,sys
threshold = int(sys.argv[1]) if len(sys.argv) > 1 else 18
# Load the image first
img_path = './Signature.jpg'
img0 = cv.imread(img_path)
assert img0 is not None, "file could not be read, check with os.path.exists('%s')" % img_path
cv.imwrite('Original.jpg', img0)
img1 = remove_top_color(img0, threshold=threshold)
cv.imwrite('Output.jpg', img1)
SciTech-Mathmatics-ImageProcessing-Remove the Background Color or Image from an image using Python+NumPy+Pandas的更多相关文章
- unity Changing Game View background color
Change the background color in the camera 参考:http://forum.unity3d.com/threads/changing-game-view-bac ...
- android:background="@color/white" [create file color.xml at res/values/]
<resources><color name="white">#FFFFFF</color><!--白色 --><col ...
- How to change the header background color of a QTableView
You can set the style sheet on the QTableView ui->tableView->setStyleSheet("QHeaderView:: ...
- Ananconda常用指令
Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项,可用于解决开发过程中遇到python版本需要切换的问题. conda有一点好处是,如 ...
- javascript改变背景/字体颜色(Through the javascript to change the background and font color)
鼠标移动到.移出DIV时修改DIV的颜色: 1.Change the font and Div background color--function <div style="width ...
- 【转】c#、wpf 字符串,color,brush之间的转换
转自:http://www.cnblogs.com/wj-love/archive/2012/09/14/2685281.html 1,将#3C3C3C 赋给background this.selec ...
- CSS3详解:background
CSS3对于background做了一些修改,最明显的一个就是采用设置多背景,不但添加了4个新属性,并且还对目前的属性进行了调整增强. 1.多个背景图片 在css3里面,你可以再一个标签元素里应用多个 ...
- C#中Brush、Color、String相互转换WPF/Silverlight
//部分方法只适用于WPF,在SL中不能用 using System.Windows.Media; 1.String转换成Color Color color = (Color)ColorConvert ...
- windows cmd color setup
设置颜色的话,一般可定会有foreground和background color设置:(其实color /?直接看一下就好了) Color Background Foreground Black 0 ...
- [Android疑难杂症]动态改变Background后Padding无效的问题
前言 在Layout中指定好background和padding以后,程序里面动态修改background之后padding就失效了,貌似是一个BUG,这里找到了一篇英文文章,简单翻译分享一下. 声明 ...
随机推荐
- Maven版本号管理规范:为何父POM是统一依赖版本的最佳实践?
结论先行 在Maven多模块项目中,依赖的版本号应集中定义在父POM的<dependencyManagement>中,子模块通过继承父POM来引用版本号,通常无需在子POM中重复声明.这能 ...
- 凯亚IOT平台在线测试MQTT接入设备
一.概述 凯亚 (Kayak)开通了MQTT端口425,以便给感兴趣的同僚进行测试,下面将在此篇文章讲解如何平台接入设备进行MQTT通信 凯亚 (Kayak) 是什么? 凯亚(Kayak)是基于.NE ...
- 代码随想录第二十七天 | Leecode 455. 分发饼干、 376. 摆动序列、 53. 最大子数组和
Leecode 455. 分发饼干 题目描述 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干. 对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的 ...
- 【记录】IDA|Ollydbg|两种软件中查看指令在原二进制文件中的位置,及查看原二进制文件位置对应的反汇编指令的方式
文章目录 在IDA中查看指令地址 在Ollydbg中查看指令地址 在Ollydbg中查看地址对应的指令 在IDA中查看指令地址 在Ollydbg中查看指令地址 ollydbg在对应指令处,右键-查看- ...
- 用 Proxy 进一步提高 npm 安装速度
@charset "UTF-8"; .markdown-body { line-height: 1.75; font-weight: 400; font-size: 15px; o ...
- 使用php的openssl_encrypt和python的pycrypt进行跨语言的对称加密和解密问题
最近有一个业务需求,需要前端传递一个密码到后端,期间要对传递的密码通过进行对称加密,我们约定使用成熟的AES加密方法. 前端使用php,后端用python,但是发现前端兄弟加密后的字符串,在pytho ...
- (Pytorch第二天)CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
我在cmd里执行conda active,是没有问题的,但是在powershell里会报标题的错 按照各种提示以及查的资料,都说执行conda init 或者conda init cmd.exe等诸如 ...
- Mimikatz 常用命令
以肉去蚁蚁愈多,以鱼驱蝇蝇愈至. 导航 1 工具介绍 2 基本用法 2.1 执行方式 2.2 帮助命令 3 模块用法 3.1 Standard 模块 3.2 Privilege 模块 3.3 Toke ...
- 揭秘C#异步编程核心机制:从状态机到线程池的全面拆解
C#中的异步编程是一个强大且复杂的特性,它允许开发者编写非阻塞的代码,从而显著提升应用程序的响应性和吞吐量.本文将深入剖析异步编程的底层原理,从async和await关键字的工作机制,到状态机.任务调 ...
- 算法新手村 Day01
作者:故事我忘了¢个人微信公众号:程序猿的月光宝盒 目录 前言: 算法 算法的分类 几个排序算法 0.两值交换 1.选择排序 动图 思路 代码 2.冒泡排序 动图 思路 代码 3.插入排序 动图 思路 ...