zoukankan      html  css  js  c++  java
  • 摄像头视频二值化(基于图像)

    一个很简单的问题,就是要将上篇文章摄像头得到的灰度视频进行二值化。

    起初,采取了简单的for循环,手动设定阈值,发现时延严重,代码如下:

    vid = videoinput('winvideo',1);
    preview(vid);
    set(vid,'ReturnedColorSpace','grayscale');
    while 1
    pause(1);%1s
    figure(2);
    %imshow(image);
    [m,n] = size(image);
    for x = 1:m
    	for y = 1:n
    		if image(x,y) > 130 %手动设定阈值
    		image(x,y) = 0;
    		else image(x,y) = 255;
    		end
    	end
    end
    figure(3);
    imshow(image);
    end

    为了减小时延,采用了matlab内置函数im2bw进行二值化,并用graythresh函数自动获取阈值,设定pause延时为最小25帧(0.04秒),效果大大改善,实时性也很好,代码如下:

    vid = videoinput('winvideo',1);
    preview(vid);
    set(vid,'ReturnedColorSpace','grayscale');
    while 1
    pause(0.04);%25帧
    image = getsnapshot(vid);
    thresh = graythresh(image);
    image = im2bw(image,thresh);
    imshow(image);
    end


  • 相关阅读:
    Go基础
    格式化输入输出
    常量
    Go语言基础之变量
    跨平台编译
    Hello World
    使用go module导入本地包
    Go语言之依赖管理
    Go包管理
    Go项目结构
  • 原文地址:https://www.cnblogs.com/iamlsj/p/3870031.html
Copyright © 2011-2022 走看看