突然兴起想玩一下抠图,试着用自带的Example\video来改,花了一个中午做了个小样:

分别是白色为底与黑色为底的效果,代码如下:

 import processing.video.*;
int numPixels;
int[] backgroundPixels;
Capture video;
PImage img; void setup() {
size(640, 480);
video = new Capture(this, width, height);
video.start();
numPixels = video.width * video.height;
backgroundPixels = new int[numPixels];
img = new PImage(video.width, video.height);//make a copy of video.
frameRate(30);
} void draw() {
if (video.available()) {
video.read();
}
img = video;//use img as a template
img.loadPixels();
for (int i = 0; i < numPixels; i++) {
color currColor = img.pixels[i];
color bkgdColor = backgroundPixels[i];
int currR = (currColor >> 16) & 0xFF;
int currG = (currColor >> 8) & 0xFF;
int currB = currColor & 0xFF;
int bkgdR = (bkgdColor >> 16) & 0xFF;
int bkgdG = (bkgdColor >> 8) & 0xFF;
int bkgdB = bkgdColor & 0xFF;
int diffR = abs(currR - bkgdR);
int diffG = abs(currG - bkgdG);
int diffB = abs(currB - bkgdB);
int td = 20;//set threshold to 20 pixcels; //check each pixel of the frame, set background pixels to white.
if (diffR >td||diffG>td||diffB>td) {
img.pixels[i] = color(currR, currG, currB);
} else {
img.pixels[i] = color(255, 255, 255);
}
}
img.updatePixels();
//use mouse to adjust the blur effect;
fastblur(img,(int)map(mouseX,0,width,1,20));
//display image.
image(img, 0, 0);
} void keyPressed() {
if (key == ' ') {
arraycopy(img.pixels, backgroundPixels);
}
}
 void fastblur(PImage img,int radius){

   if (radius<1){
return;
}
int w=img.width;
int h=img.height;
int wm=w-1;
int hm=h-1;
int wh=w*h;
int div=radius+radius+1;
int r[]=new int[wh];
int g[]=new int[wh];
int b[]=new int[wh];
int rsum,gsum,bsum,x,y,i,p,p1,p2,yp,yi,yw;
int vmin[] = new int[max(w,h)];
int vmax[] = new int[max(w,h)];
int[] pix=img.pixels;
int dv[]=new int[256*div];
for (i=0;i<256*div;i++){
dv[i]=(i/div);
} yw=yi=0; for (y=0;y<h;y++){
rsum=gsum=bsum=0;
for(i=-radius;i<=radius;i++){
p=pix[yi+min(wm,max(i,0))];
rsum+=(p & 0xff0000)>>16;
gsum+=(p & 0x00ff00)>>8;
bsum+= p & 0x0000ff;
}
for (x=0;x<w;x++){ r[yi]=dv[rsum];
g[yi]=dv[gsum];
b[yi]=dv[bsum]; if(y==0){
vmin[x]=min(x+radius+1,wm);
vmax[x]=max(x-radius,0);
}
p1=pix[yw+vmin[x]];
p2=pix[yw+vmax[x]]; rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16;
gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8;
bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff);
yi++;
}
yw+=w;
} for (x=0;x<w;x++){
rsum=gsum=bsum=0;
yp=-radius*w;
for(i=-radius;i<=radius;i++){
yi=max(0,yp)+x;
rsum+=r[yi];
gsum+=g[yi];
bsum+=b[yi];
yp+=w;
}
yi=x;
for (y=0;y<h;y++){
pix[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum];
if(x==0){
vmin[y]=min(y+radius+1,hm)*w;
vmax[y]=max(y-radius,0)*w;
}
p1=x+vmin[y];
p2=x+vmax[y]; rsum+=r[p1]-r[p2];
gsum+=g[p1]-g[p2];
bsum+=b[p1]-b[p2]; yi+=w;
}
}
}

代码主要分为2个部分:

1. main

获取摄像头帧video,使用PImage对象img作为缓存帧,通过空格保存对比帧background,使用阀值将对比帧与缓存帧之间相同的部分存为指定颜色(例如:白色、黑色),再显示不同的部分。

2. fastblur

简单高斯模糊,本来是想进行边缘柔化的,结果发现这种方法不行,看来只有另外找了。

Processing玩抠图的更多相关文章

  1. Processing 中玩增强现实 Argument Reality

    其实2009年Processing就能做AR了,只是我不知道而已~ 需要以下几个东西: 1.JMyron 2.GSVideo 3.nyar4psg 4.Picking 5.OBJLoader 或者大伙 ...

  2. 玩转spring boot——properties配置

    前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...

  3. powershell玩转xml之20问

    powershell玩转xml之20问 powershell 传教士 原创文章 2014-01-30,2015-10-27改 允许转载,但必须保留名字和出处,否则追究法律责任 问:xml文件编码情况如 ...

  4. 《Data-Intensive Text Processing with mapReduce》读书笔记之二:mapreduce编程、框架及运行

    搜狐视频的屌丝男士第二季大结局了,惊现波多野老师,怀揣着无比鸡冻的心情啊,可惜随着剧情的推进发展,并没有出现期待中的屌丝奇遇,大鹏还是没敢冲破尺度的界线.想百度些种子吧,又不想让电脑留下污点证据,要知 ...

  5. openstack手动玩转

    <一,preface Important Project Network> openstack or all most cloud env Network desgine  is so m ...

  6. 工欲善其事,必先利其器 软件工具开发关键词 protractor自动化测试工具 RegexBuddy正则 CodeSmith,LightSwitch:代码生成 CheatEngine:玩游戏修改内存值必备神器 ApkIDE:Android反编译工具 Reflector:反编译dll动态链接库

    工欲善其事,必先利其器 本文版权归翟士丹(Stan Zhai)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利. 原文地址:http ...

  7. [转]一步一步玩控件:自定义TabControl——从山寨Safari开始

    作者:野比 (conmajia@gmail.com) 时间:May, 2012 封面图片为野比原创,请勿未经允许私自引用 #1-1 嗯,各位,又是我,生物钟颠倒的家伙. 今天我要山寨的是大名鼎鼎的Ap ...

  8. MaskRCNN路标:TensorFlow版本用于抠图

    MaskRCNN用于检测路标,作为更详细的目标检测,用以得到更精准的额路标位置,路标的几何中心点,用于构建更为精准的拓扑地图,减少构图误差. 抠图工具已经完成,把框抠出来,用0值表示背景. pytho ...

  9. 玩转 SpringBoot 2 之整合 JWT 下篇

    前言 在<玩转 SpringBoot 2 之整合 JWT 上篇> 中介绍了关于 JWT 相关概念和JWT 基本使用的操作方式.本文为 SpringBoot 整合 JWT 的下篇,通过解决 ...

随机推荐

  1. metasploit、msfvenom生成木马入侵电脑及手机

    简介 msfvenom msfvenom a Metasploit standalone payload generator,Also a replacement for msfpayload and ...

  2. JS权威指南读书笔记(四)

    第十章 正则表达式 1 正则表达式直接量定义为包含在一对斜杠(/)之间的字符     a /s$/ == new RegExp("s$") 2 直接量字符:所有字母和数字都是按照字 ...

  3. react之高阶组件(二)

    高阶组件的使用 接上文———— 一.像函数一样直接调用 import React, { Component } from 'react' import A from './A' class C ext ...

  4. CSS ID选择器&通配选择器

    ID选择器 ID(IDentity)是编号的意思,一般指定标签在HTML文档中的唯一编号.ID选择器和标签选择器.类选择器的作用范围不同. ID选择器仅仅定义一个对下对象的样式,而标签选择器和类选择器 ...

  5. MySql时区修改

    1.查看当前时间 > select curtime(); #或select now()也可以+-----------+| curtime() |+-----------+| 15:18:10 | ...

  6. 常用的js加密

    https://github.com/hellobajie/AES-of-JavaScript    此为js的 AES加密方式,两个加密文档,可当做扣js必备

  7. 【hbase】hbase的shell操作笔记

    HBase Shell $ ./bin/hbase shell # 进入交互界面 DDL操作: create:创建表(默认命名空间为default) # create '表名','列族1','列族2' ...

  8. 关于在centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'的解决办法

    3.7版本需要一个新的包libffi-devel,安装此包之后再次进行编译安装即可. #yum install libffi-devel -y#make install若在安装前移除了/usr/bin ...

  9. [nginx] nginx源码分析--监控检测模块

    描述 HTTP监控检查,是nginx官网推荐使用的第三方模块 https://www.nginx.com/resources/wiki/modules/healthcheck/ https://git ...

  10. 最新my.cnf可用于5.6&5.7

    [MySQL]prompt = [\\u@\\h][\\d]>\\_  #自定义登录提示信息 [mysqld]#基本设置#user = mysql #用户名sql_mode =“STRICT_T ...