一直想用摄像头拍摄来检查LED的颜色。但一直存在以下几个问题:
- LDE太亮,拍出来LDE中心的颜色基本上都是白色的。很多时候要加滤光片,而合适的滤光片不好找,这就增加了难度和成本。
- 摄像头拍照的参数无法修改。比如曝光补偿EV,感光度ISO,快门时间shutter speed, 这几个参数直接影响了成像的颜色和亮度。
- 摄像头像数,成像效果,速度,成本。一般的USB摄像头便宜,但是成像效果不好,无法很好地还原颜色,速度也偏慢。传统的工业摄像头成像效果可以,速度也快,但是价格不菲,无法不心痛地大量使用。
难道没有解决方法吗?有的!经过对比多种摄像头,包括无牌子的USB网络摄像头,有名的罗技带自动对焦的USB网络摄像头,某宝9.9的便宜手动对焦摄像头,还有带显微镜功能的USB摄像头都达不到想要的效果。终于双11,淘了几款树莓派的摄影像头,发现带手动对焦功能的一款500W像数,可以达到效果,而且也不到100RMB, 比较惊喜的是加上红外补光灯还可以夜间拍摄!这不是官方的那款,而是兼容的。
先来看看效果,一般拍摄的效果,这种情况只能判断出亮还是灭,无法判断颜色:
调节参数(感光度,快门时间)之后:
上面第一张图片使用的命令如下,明显LED的地方已经过曝光发白了。
1 |
raspistill -t 10000 -v -q 100 -roi 0,0,1,1 -o roi2.jpg |
第二张图片使用的命令如下,这里主要是把快门时间ss调成100微妙,感光度ISO调成200,LED的封装体正确地显示出颜色来,发光的点还是很明亮显白色,但这不影响我们判断颜色了,我们可以选择封装体的地方来判断,这样就可以让摄像头直接对着LED拍照检查颜色了。
1 |
raspistill -t 10000 -v -q 100 -roi 0,0,1,1 -ss 100 -ISO 200 -o roi.jpg |
这里使用的摄像头如下,这是一款CSI接口的摄像头,兼用树莓派的接口,所以要用在树莓派上了。使用命令raspistill, raspipid可以方便地获取到图片。在raspi-config里启用摄像头后,重启树莓派就可以用这两个命令了。
还有v2.1的一款800W像数的,有空再试试效果。
来个合照和raspistill的帮助信息:
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
pi@raspberrypi:~ $ raspistill raspistill Camera App v1.3.8 Runs camera for specific time, and take JPG capture at end if requested usage: raspistill [options] Image parameter commands -?, --help : This help information -w, --width : Set image width <size> -h, --height : Set image height <size> -q, --quality : Set jpeg quality <0 to 100> -r, --raw : Add raw bayer data to jpeg metadata -o, --output : Output filename <filename> (to write to stdout, use '-o -'). I f not specified, no file is saved -l, --latest : Link latest complete image to filename <filename> -v, --verbose : Output verbose information during run -t, --timeout : Time (in ms) before takes picture and shuts down (if not speci fied, set to 5s) -th, --thumb : Set thumbnail parameters (x:y:quality) or none -d, --demo : Run a demo mode (cycle through range of camera options, no cap ture) -e, --encoding : Encoding to use for output file (jpg, bmp, gif, png) -x, --exif : EXIF tag to apply to captures (format as 'key=value') or none -tl, --timelapse : Timelapse mode. Takes a picture every <t>ms. %d == fra me number (Try: -o img_%04d.jpg) -fp, --fullpreview : Run the preview using the still capture resolution (ma y reduce preview fps) -k, --keypress : Wait between captures for a ENTER, X then ENTER to exit -s, --signal : Wait between captures for a SIGUSR1 from another process -g, --gl : Draw preview to texture instead of using video render componen t -gc, --glcapture : Capture the GL frame-buffer instead of the camera imag e -set, --settings : Retrieve camera settings and write to stdout -cs, --camselect : Select camera <number>. Default 0 -bm, --burst : Enable 'burst capture mode' -md, --mode : Force sensor mode. 0=auto. See docs for other modes available -dt, --datetime : Replace output pattern (%d) with DateTime (MonthDayHourMinSec) -ts, --timestamp : Replace output pattern (%d) with unix timestamp (secon ds since 1970) -fs, --framestart : Starting frame number in output pattern(%d) -rs, --restart : JPEG Restart interval (default of 0 for none) Preview parameter commands -p, --preview : Preview window settings <'x,y,w,h'> -f, --fullscreen : Fullscreen preview mode -op, --opacity : Preview window opacity (0-255) -n, --nopreview : Do not display a preview window Image parameter commands -sh, --sharpness : Set image sharpness (-100 to 100) -co, --contrast : Set image contrast (-100 to 100) -br, --brightness : Set image brightness (0 to 100) -sa, --saturation : Set image saturation (-100 to 100) -ISO, --ISO : Set capture ISO -vs, --vstab : Turn on video stabilisation -ev, --ev : Set EV compensation - steps of 1/6 stop -ex, --exposure : Set exposure mode (see Notes) -awb, --awb : Set AWB mode (see Notes) -ifx, --imxfx : Set image effect (see Notes) -cfx, --colfx : Set colour effect (U:V) -mm, --metering : Set metering mode (see Notes) -rot, --rotation : Set image rotation (0-359) -hf, --hflip : Set horizontal flip -vf, --vflip : Set vertical flip -roi, --roi : Set region of interest (x,y,w,d as normalised coordinates [0.0 -1.0]) -ss, --shutter : Set shutter speed in microseconds -awbg, --awbgains : Set AWB gains - AWB mode must be off -drc, --drc : Set DRC Level (see Notes) -st, --stats : Force recomputation of statistics on stills capture pass -a, --annotate : Enable/Set annotate flags or text -3d, --stereo : Select stereoscopic mode -dec, --decimate : Half width/height of stereo image -3dswap, --3dswap : Swap camera order for stereoscopic -ae, --annotateex : Set extra annotation parameters (text size, text colou r(hex YUV), bg colour(hex YUV)) Notes Exposure mode options : off,auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixed fps,antishake,fireworks AWB mode options : off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon Image Effect mode options : none,negative,solarise,sketch,denoise,emboss,oilpaint,hatch,gpen,pastel,watercol our,film,blur,saturation,colourswap,washedout,posterise,colourpoint,colourbalanc e,cartoon Metering Mode options : average,spot,backlit,matrix Dynamic Range Compression (DRC) options : off,low,med,high Preview parameter commands -gs, --glscene : GL scene square,teapot,mirror,yuv,sobel -gw, --glwin : GL window settings <'x,y,w,h'> |
很多时候想用python来做这个拍摄工作,然后可以结合其它的功能,有2个方法,一是在python里调用raspistill. 二是用picamera模块。以下是用picamera模块做的小程序,获取到一张图片。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from picamera import PiCamera from io import BytesIO from PIL import Image camera = PiCamera(resolution=(1280,720), framerate=30) # Set ISO to the desired value camera.iso = 200 # Now fix the values camera.exposure_mode = 'off' camera.shutter_speed = 100 # Finally, take several photos with the fixed settings #camera.capture_sequence(['009image%02d.jpg' % i for i in range(10)]) #camera.capture('009image_led.jpg') stream = BytesIO() camera.capture(stream, format='jpeg') stream.seek(0) image=Image.open(stream) image.save("009image_led_using_stream_to_PIL.jpg") |
程序里通过BytesIO()流,把图片转成了PIL的image, 然后再用image的save()方法保存成图片,这样就可以应用PIL对图片在内存里进行相应的计算,有时候不是必须存为文件的时候,这样做就可以减少IO操作,毕竟树莓派的写操作还是比较慢的,而且如果反复写SD卡的寿命估计也不长。
其实也可以用注悉掉的那两行其中一行都可以直接把图像保存起来,不是必须转成流再转成PIL的image再保存的。
同样获取到一张理想的图片: