本文介绍了opencv视频上的颜色阈值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为opencv视频中的颜色范围设置阈值。目标是在医学超声视频中为学术项目从彩色流多普勒模式(速度信息)中分离B模式(黑白,位置信息,而不是速度)。我已经尝试基于HSV色调范围的阈值,我已经从超声机(浅蓝色[opencv色调90]到黄色[opencv色调35])提供的色标重建。不幸的是,结果不好。我在阈值处理中犯了一个错误?还是会有另一种方式来实现预期的结果?下面是我的代码和我的结果的框架示例。

I am thresholding for a color range in an opencv video. The goal is to seperate the B-mode (black and white, information on location but not velocity) from color-flow doppler mode (velocity infomation) in medical ultrasound videos for an academic project. I have tried to threshold based on an HSV hue range that I have rebuilt from the color scale delivered by the ultrasound machine (light blue [opencv hue 90] to yellow [opencv hue 35]). Unfortunately, the results are not good. Have I made a mistake in the thresholding? Or would there be a another way to achieve the desired results? Below is my code and a frame example of my results.

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


推荐答案

仅基于Hue组件的阈值处理在某种程度上是无用的。

如下所示,对于特定的色相,可能的颜色范围还包括灰色。

Thresholding based on only the Hue component is somehow useless.
As you can see below, for a speceific Hue, the range of possible colors also includes gray colors.

此外,看到H,S,V通道,我可以说H通道单独不能帮助你。您还应使用饱和度通道:

Also, seeing the H,S,V channels, I can say that H channel alone can't help you. You should also use the Saturation channel:



(Hue Channel)


(Hue Channel)

虽然,您可以看到饱和度通道可以帮助您更轻松地找到丰富多彩的区域:

Though, you can see the Saturation channel can help you find the colorful areas easier:

过滤饱和度< 180色,会给你这样:

Filtering Saturation<180 colors, would give you this:

现在你有了丰富多彩的地区。如果该侧边栏总是在您处理的图片中,则可以过滤值通道中的值< 150以过滤它们:

Now you have the colorful areas. if that sidebar, is always in the picture you process, you can just filter the Value<150 in the Value channel to filter them out too:

然后,使用cv2,你的代码变得很更易读且易于维护:

And BTW, using cv2, your code becomes much more readable and easier to maintain:

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)

这篇关于opencv视频上的颜色阈值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 05:42