canvas像素点操作之视频绿幕抠图


当前第2页 返回上一页

PS中魔法棒工具可以把图片中一定容差下的相近像素都选中、清空,轻松做到一键“抠图”,前提是主体一定要与背景有大的差异,即像素值差值越大,抠图效果越好。

Canvas同样可以做到,并且可以处理视频帧,其中的原理是一样的 ―― 将每个视频帧中绿幕的像素块透明度置0即可。像这样 ――

demo

部分代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

import videoUrl from './component/video.ogv';

import imgUrl from './component/sample.jpg';

 

const TOLERANCE = 5;

export default {

    data () {

        return {

            videoUrl: videoUrl,

            imgUrl: imgUrl

        }

    },

 

    methods: {

        draw () {

            if (this.video.paused || this.video.ended) {

                return;

            }

            this.ctx.drawImage(this.video, 0, 0, this.width, this.height);

            this.ctx.putImageData(this.cutOut(), 0, 0);

        },

 

        cutOut () {

            let frameData = this.ctx.getImageData(0, 0, this.width, this.height),

                len = frameData.data.length / 4;

 

            for (let i = 0; i < len; i++) {

                let r = frameData.data[i * 4 + 0],

                    g = frameData.data[i * 4 + 1],

                    b = frameData.data[i * 4 + 2];

                if (r - 100 >= TOLERANCE

                 && g - 100 >= TOLERANCE

                 && b - 43 <= TOLERANCE) {

                    frameData.data[i * 4 + 3] = 0;

                }

            }

            return frameData;

        }

    },

 

    mounted () {

        this.video = this.$refs['video'];

        this.canvas = this.$refs['canvas'];

        this.ctx = this.canvas.getContext('2d');

        this.timer = null;

 

        this.video.addEventListener('play', () => {

            this.width = this.video.videoWidth;

            this.height = this.video.videoHeight;

 

            this.timer && clearInterval(this.timer);

            this.timer = setInterval(() => {

                this.draw();

            }, 50);

        }, false);

    }

}

参考资料

Manipulating video using canvas

Pixel manipulation with canvas

Canvas and images and pixels

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问Html5视频教程!

相关推荐:

php公益培训视频教程

HTML5图文教程

HTML5在线手册

以上就是canvas像素点操作之视频绿幕抠图的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

canvas渐变色:canvas如何实现渐变色的效果?

如何解决canvas绘图时遇到的跨域问题

用h5的canvas做出弹幕效果

html5中关于canvas画图之画圆形的实例介绍

canvas绘制饼图的方法介绍(代码)

canvas与svg的区别有什么?canvas和svg的区别比较

canvas中使用clip()函数裁剪方法

canvas画直角坐标系

canvas如何绘制时钟?canvas画环形时钟的实现过程

使用html5 canvas api中的clip()方法裁剪区域图像代码实例

更多相关阅读请进入《canvas》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...