我正在对 opencv 视频中的颜色范围进行阈值处理。目标是将 B 模式(黑白,位置信息但不是速度信息)与医学超声视频中的彩色血流多普勒模式(速度信息)分开,用于学术项目。我试图根据我从超声波机器提供的色标(浅蓝色 [opencv hue 90] 到黄色 [opencv hue 35])重建的 HSV 色调范围来设定阈值。不幸的是,结果并不好。我在阈值中犯了错误吗?还是有另一种方法可以达到预期的效果?下面是我的代码和我的结果的框架示例。
#!/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()