python调用摄像头显示图像的实例
如下所示:
importcv2
importnumpyasnp
bins=np.arange(256).reshape(256,1)
defhist_curve(im):
h=np.zeros((300,256,3))
iflen(im.shape)==2:
color=[(255,255,255)]
elifim.shape[2]==3:
color=[(255,0,0),(0,255,0),(0,0,255)]
forch,colinenumerate(color):
hist_item=cv2.calcHist([im],[ch],None,[256],[0,256])
cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
hist=np.int32(np.around(hist_item))
pts=np.int32(np.column_stack((bins,hist)))
cv2.polylines(h,[pts],False,col)
y=np.flipud(h)
returny
defhist_lines(im):
h=np.zeros((300,256,3))
iflen(im.shape)!=2:
print"hist_linesapplicableonlyforgrayscaleimages"
#print"soconvertingimagetograyscaleforrepresentation"
im=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
hist_item=cv2.calcHist([im],[0],None,[256],[0,256])
cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
hist=np.int32(np.around(hist_item))
forx,yinenumerate(hist):
cv2.line(h,(x,0),(x,y),(255,255,255))
y=np.flipud(h)
returny
if__name__=='__main__':
importsys
iflen(sys.argv)>1:
im=cv2.imread(sys.argv[1])
else:
im=cv2.imread('../cpp/lena.jpg')
print"usage:pythonhist.py"
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
print'''Histogramplotting\n
Keymap:\n
a-showhistogramforcolorimageincurvemode\n
b-showhistograminbinmode\n
c-showequalizedhistogram(alwaysinbinmode)\n
d-showhistogramforcolorimageincurvemode\n
e-showhistogramforanormalizedimageincurvemode\n
Esc-exit\n
'''
cv2.imshow('image',im)
whileTrue:
k=cv2.waitKey(0)&0xFF
ifk==ord('a'):
curve=hist_curve(im)
cv2.imshow('histogram',curve)
cv2.imshow('image',im)
print'a'
elifk==ord('b'):
print'b'
lines=hist_lines(im)
cv2.imshow('histogram',lines)
cv2.imshow('image',gray)
elifk==ord('c'):
print'c'
equ=cv2.equalizeHist(gray)
lines=hist_lines(equ)
cv2.imshow('histogram',lines)
cv2.imshow('image',equ)
elifk==ord('d'):
print'd'
curve=hist_curve(gray)
cv2.imshow('histogram',curve)
cv2.imshow('image',gray)
elifk==ord('e'):
print'e'
norm=cv2.normalize(gray,alpha=0,beta=255,norm_type=cv2.NORM_MINMAX)
lines=hist_lines(norm)
cv2.imshow('histogram',lines)
cv2.imshow('image',norm)
elifk==27:
print'ESC'
cv2.destroyAllWindows()
break
cv2.destroyAllWindows()
以上这篇python调用摄像头显示图像的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。