2
我爲opencv視頻中的顏色範圍設置閾值。目標是在醫學超聲波視頻中爲彩色多普勒模式(速度信息)分配B模式(黑白信息,但不包括速度)。我嘗試根據HSV色調範圍進行閾值測試,該範圍是由超聲波機器提供的色階(淺藍色[opencv hue 90]到黃色[opencv hue 35])重建的。不幸的是,結果並不好。我在閾值中犯了錯誤嗎?或者是否會有另一種方法來達到預期的效果?以下是我的代碼和我的結果的框架示例。僅基於色相成分 opencv視頻中的顏色閾值
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##IMPORTS
import cv2.cv as cv
import numpy as np
##VARIABLES
#colors
doppler_hues=np.concatenate([np.arange(90,181),np.arange(0,36)])
##MAIN
#start video stream analysis
frames = raw_input('Please enter video file:')
if not frames:
print "This program requires a file as input!"
sys.exit(1)
# first, create the necessary windows
cv.NamedWindow ('image', cv.CV_WINDOW_AUTOSIZE)
cv.NamedWindow ('original', cv.CV_WINDOW_AUTOSIZE)
#File capture
vidFile = cv.CaptureFromFile(frames)
nFrames = int( cv.GetCaptureProperty(vidFile, cv.CV_CAP_PROP_FRAME_COUNT))
fps = cv.GetCaptureProperty(vidFile, cv.CV_CAP_PROP_FPS)
waitPerFrameInMillisec = int(1/fps * 1000/1)
for f in xrange(nFrames):
#frame capture
frame = cv.QueryFrame(vidFile)
# create the images we need
original = cv.CreateImage (cv.GetSize (frame), 8, 3)
cv.Copy(frame,original)
image = cv.CreateImage (cv.GetSize (frame), 8, 3)
cv.CvtColor(frame, image, cv.CV_BGR2HSV)
image2 = cv.CreateImage (cv.GetSize (frame), 8, 3)
if not frame:
break
#Replace pixel colors
image=np.asarray(image[:,:])
hue=np.resize(image,(480,640,1))
hue[np.where((np.not_equal(hue,doppler_hues)).all(axis=2))]=[0]
hue2=np.resize(hue,(480,640,3))
image[np.where((hue2==[0,0,0]).all(axis=2))]=[0,0,0]
image=cv.fromarray(image[:,:])
cv.CvtColor(image, image2, cv.CV_HSV2BGR)
#show the image
cv.ShowImage("image", image2)
cv.ShowImage("original", original)
#quit command ESC
if cv.WaitKey(waitPerFrameInMillisec)==27:
break
else:
cv.WaitKey(waitPerFrameInMillisec) % 0x100
cv.DestroyAllWindows()
謝謝!你的答案加快了我的計劃,並且它很好地工作! – Raoul
不客氣。 –