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,这里找到了一篇英文文章,简单翻译分享一下. 声明 ...
随机推荐
- redis没有启动会报什么错???
以下是报错信息,应该是根据项目不同,具体的不太一样,但我们能清晰的看到一句 加红,加大,加粗 Caused by: redis.clients.jedis.exceptions.JedisConnec ...
- 遇到的问题之“web container destroy and kill the job.-Web容器销毁和终止作业”
一.问题 JobThread toStop, stopReason:web container destroy and kill the job. 2023-11-22 18:10:10 [com.x ...
- django实例(1)
Urls.py from django.contrib import adminfrom django.conf.urls import urlfrom cmdb import viewsurlpat ...
- TVM:使用张量表达式处理算子
在本教程中,把注意力转向 TVM 如何使用张量表达式(Tensor Expression,简称 TE)定义张量计算并应用循环优化.TE 以纯函数式语言描述张量计算(即每个表达式都没有副作用).从 TV ...
- 网络编程:TIME_WAITE
一.TIME_WAIT 1.TIME_WAIT发生的场景 故障:一次升级线上应用服务后,发现该服务的可用性时好时坏,一段时间可以对外提供服务,一段时间突然又不可以了.使用netstat命令发现主机有成 ...
- C++ decltype类型推导
1.decltype介绍 decltype(declare type,声明类型)为C++11 新增的关键字,和auto功能一样,用于在编译期间进行自动类型推导. auto和decltype关键字都可以 ...
- VMware Workstation 部署企业级 AD 域、DNS、DHCP 系统操作指南
一.主机环境规划 1. 虚拟机配置表 主机角色 操作系统 IP 地址 子网掩码 DNS 内存 硬盘 网络模式 域控制器 (DC) Windows Server 2022 192.168.1.10 25 ...
- 🚀 革命性AI提示词优化平台正式开源!
革命性AI提示词优化平台正式开源! AI时代最强大的Prompt工程师已经到来! 你是否还在为写不出高质量提示词而头疼?是否羡慕那些能够驾驭AI.让ChatGPT.Claude乖乖听话的"提 ...
- onnxruntime-gpu创建会话时报错:Could not load symbol cudnnGetLibConfig. Error code 127
最近在新的设备上做模型的demo,模型训练好之后,我习惯的拉取了我基于pyqt5和onnxruntime的demo演示代码,我按照我的cuda和cudnn环境,安装了1.19的onnxruntim ...
- 记一次 .NET 某SaaS版CRM系统 崩溃分析
一:背景 1. 讲故事 调试训练营里的一位学员前些天找到我,说他们跑在k8s中的程序崩掉了不知道怎么回事?日志也没有记录到,让我帮他看看,dump也抓到了,既然抓到了那就看看吧. 二:程序为什么会崩溃 ...