2013-06-02 68 views
2

我爲opencv視頻中的顏色範圍設置閾值。目標是在醫學超聲波視頻中爲彩色多普勒模式(速度信息)分配B模式(黑白信息,但不包括速度)。我嘗試根據HSV色調範圍進行閾值測試,該範圍是由超聲波機器提供的色階(淺藍色[opencv hue 90]到黃色[opencv hue 35])重建的。不幸的是,結果並不好。我在閾值中犯了錯誤嗎?或者是否會有另一種方法來達到預期的效果?以下是我的代碼和我的結果的框架示例。僅基於色相成分 result exampleopencv視頻中的顏色閾值

#!/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() 

回答

3

閾值在某種程度上無用。
如下圖所示,對於特定色調,可能的顏色範圍也包括灰色。

spectrum

而且,眼看着H,S,V通道,我可以說,H信道本身並不能幫助你。你也應該使用飽和度通道:

Hue Channel
(色度通道)

雖然,你可以看到飽和渠道可以幫助你找到多姿多彩的區域更容易:

Saturation

過濾飽和度< 180點的顏色,會給你:
Thresh Sat

現在你有豐富多彩的領域。如果邊欄,總是在你處理的圖片,你可以過濾值< 150在值通道也將它們過濾出來:

Sat and Val Filter

而且順便說一句,使用CV2,你的代碼變得更可讀性和易於維護:

import cv2 

img = cv2.imread('image.png') 
image_thr = img.copy() 

imh = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) 
image_thr[(imh[...,1]<180) | (imh[...,2]<150)]=0 

cv2.imshow('filtered',image_thr) 
+0

謝謝!你的答案加快了我的計劃,並且它很好地工作! – Raoul

+0

不客氣。 –