HSP Tips / Sample

Webカメラを使った動体検知


説明

 HSPCVを利用して,Webカメラの映像を取得,解析し動体検知を行う。
 ソースコードは未整理のためきたないですが・・・。プログラムでは,映像の一部のドットを抽出して,それらドットの減色を行い。それからその各ドットに変化があった場合にその変化の度合いが表示されます。




HSPスクリプト


#include "hspcv.as"
#module
#uselib "crtdll.dll"
#func time "time" sptr
#defcfunc timer local s1
time varptr(s1)
return s1
#global


#module


#deffunc colorcheck int _px, int _py, int _r, int _g, int _b, int _rect, local hit
repeat count
	if thcount(cnt) != 0{
		col_r(cnt) = col_tr(cnt)/thcount(cnt)
		col_g(cnt) = col_tg(cnt)/thcount(cnt)
		col_b(cnt) = col_tb(cnt)/thcount(cnt)
		
	
		if limit(col_r(cnt)-_rect, 0, 255)<=_r{
			
			if limit(col_r(cnt)+_rect, 0, 255)>=_r{
				if limit(col_g(cnt)-_rect, 0, 255)<=_g{
					if limit(col_g(cnt)+_rect, 0, 255)>=_g{
						if limit(col_b(cnt)-_rect, 0, 255)<=_b{
							if limit(col_b(cnt)+_rect, 0, 255)>=_b{
								col_tr(cnt) += _r
								col_tg(cnt) += _g
								col_tb(cnt) += _b
								thcount(cnt)++
								
								col_r(cnt) = col_tr(cnt)/thcount(cnt)
								col_g(cnt) = col_tg(cnt)/thcount(cnt)
								col_b(cnt) = col_tb(cnt)/thcount(cnt)
								pastel(_px, _py) = cnt
								hit = 1
								break
								
							}
						}
					}
				}		
			}
		}
	}
loop

if hit == 0 {
	count++
	col_tr(count) = _r
	col_tg(count) = _g
	col_tb(count) = _b
	thcount(count)++
}



return





#deffunc noizefilter int _flag, int _id, int _width, int _height, int _rect, int _box1, int _box2, int _type, local ycnt, local r, local g, local b, local s1
s1 = _box1 : if s1 == 0 : s1 = 1
dim pastel, _width, _height
gsel _id
repeat _height / s1
	ycnt = cnt * s1
	repeat _width / s1
		xcnt = cnt * s1
		pget xcnt, ycnt
		r = ginfo_r
		g = ginfo_g
		b = ginfo_b
		colorcheck xcnt, ycnt, r, g, b, _rect
	loop
loop
if _flag == 1{
	repeat _height / s1
			ycnt = cnt * s1
			repeat _width / s1
				xcnt = cnt * s1
				color col_r(pastel(xcnt, ycnt)), col_g(pastel(xcnt, ycnt)), col_b(pastel(xcnt, ycnt))
				if _type == 0{
					boxf xcnt-s1/2, ycnt-s1/2, xcnt+s1/2, ycnt+s1/2
				}else{
					circle xcnt-s1/2-_box2/2, ycnt-s1/2-_box2/2, xcnt+s1/2+_box2/2, ycnt+s1/2+_box2/2, 1
				}
			loop
	loop
}
return

#defcfunc hita int _box1, int _width, int _height, local s1, local s2
if (hita_width != _width)&(hita_height != _height){
	dim lr, _width, _height
	dim lg, _width, _height
	dim lb, _width, _height
	hita_width = _width
	hita_height = _height
}
s1 = _box1 : if s1 == 0 : s1 = 1

	repeat _height / s1
	
			ycnt = cnt * s1
			repeat _width / s1
				xcnt = cnt * s1
				if col_r(pastel(xcnt, ycnt)) != lr(xcnt, ycnt){
					if lg(xcnt, ycnt) != col_g(pastel(xcnt, ycnt)){
						if lb(xcnt, ycnt) != col_b(pastel(xcnt, ycnt)){
							s2++
						}
					}
				}
				lr(xcnt, ycnt) = col_r(pastel(xcnt, ycnt))
				lg(xcnt, ycnt) = col_g(pastel(xcnt, ycnt))
				lb(xcnt, ycnt) = col_b(pastel(xcnt, ycnt))
				
				
			loop
	loop
	return s2


#global



onexit *owari
buffer 1
cvcapture 0, 0
repeat 

gsel 1
cvgetcapture
cvgetimg
noizefilter 0, 1, 320, 240, 70, 4, 0, 0


gsel 0
pos 0,240 : gzoom 320,-240,1,0,0,320,240

title ""+ hita(50, 320, 240)


wait 1

loop


	
	
	

	gsel 0

	
	mes "フィルタリング後"
	pos 400, 0 : gcopy 1, 0, 0, mx, my
	mes ""+(timer()-ti)+"秒経過"

	stop

*owari
	cvendcapture
	end