围绕轮廓对象生成颜色直方图 [英] Generate Color Histogram around a contoured object

查看:24
本文介绍了围绕轮廓对象生成颜色直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,OpenCV/Emgu 大师,

Hey OpenCV/Emgu gurus,

我有一张正在为其生成轮廓的图像,见下文.我正在尝试生成基于颜色直方图的图像搜索空间的修剪以查找.我怎样才能在突出的对象轮廓周围获得遮罩并阻挡其余部分.所以我有一个两部分的问题:

I have an image that I am generating contour for, see below. I am trying to generate a color histogram based pruning of search space of images to look for. How can I get the mask around just the prominent object contour and block out the remaining. So I have a 2 part question:

  1. 如何反转"轮廓外的图像?填埋反转,不是吗?我对 OpenCV 中的所有选项感到困惑.

  1. How do I "invert" the image outside the contour? Floodfill invert, not? I am confused with all the options in OpenCV.

其次,我如何从轮廓物体生成一维颜色直方图,在这种情况下红色汽车排除黑色背景,只生成包含汽车的颜色直方图.

Second, how do I generate a 1-d color histogram from the contoured object in this case the red car to exclude the black background and only generate the color histogram that includes the car.

我将如何在 OpenCV 中做到这一点(最好在 Emgu/C# 代码中)?

How would I do that in OpenCV (preferably in Emgu/C# code)?

推荐答案

大概是这样的吧?使用 Python 绑定完成,但很容易将方法转换为其他绑定...

Perhaps something like this? Done using the Python bindings, but easy to translate the methods to other bindings...

#!/usr/local/bin/python

import cv 
import colorsys

# get orginal image
orig = cv.LoadImage('car.jpg')

# show orginal 
cv.ShowImage("orig", orig)

# get mask image
maskimg = cv.LoadImage('carcontour.jpg')

# split original image into hue and value
hsv = cv.CreateImage(cv.GetSize(orig),8,3)
hue = cv.CreateImage(cv.GetSize(orig),8,1)
val = cv.CreateImage(cv.GetSize(orig),8,1)
cv.CvtColor(maskimg,hsv,cv.CV_BGR2HSV)
cv.Split(hsv, hue, None, val, None)

# build mask from val image, select values NOT black
mask = cv.CreateImage(cv.GetSize(orig),8,1)
cv.Threshold(val,mask,0,255,cv.CV_THRESH_BINARY)

# show the mask
cv.ShowImage("mask", mask)

# calculate colour (hue) histgram of only masked area
hue_bins = 180 
hue_range = [0,180]
hist = cv.CreateHist([hue_bins], cv.CV_HIST_ARRAY, [hue_range], 1) 
cv.CalcHist([hue],hist,0,mask)

# create the colour histogram 
(_, max_value, _, _) = cv.GetMinMaxHistValue(hist)
histimg = cv.CreateImage((hue_bins*2, 200), 8, 3) 
for h in range(hue_bins):
  bin_val = cv.QueryHistValue_1D(hist,h)
  norm_val = cv.Round((bin_val/max_value)*200)
  rgb_val = colorsys.hsv_to_rgb(float(h)/180.0,1.0,1.0) 
  cv.Rectangle(histimg,(h*2,0),
                ((h+1)*2-1, norm_val),
                cv.RGB(rgb_val[0]*255,rgb_val[1]*255,rgb_val[2]*255),
                cv.CV_FILLED)
cv.ShowImage("hist",histimg)

# wait for key press
cv.WaitKey(-1)

找到蒙版有点笨拙-我想知道可能是由于图像中的 JPEG 压缩伪影...如果您有原始轮廓,则很容易将其渲染"为蒙版.

This is a little bit clunky finding the mask - I wonder perhaps due to JPEG compression artefacts in the image... If you had the original contour it is easy enough to "render" this to a mask instead.

示例直方图渲染函数也有点基本 - 但我认为它显示了这个想法(以及汽车如何主要是红色的!).请注意 OpenCV 对 Hue 的解释仅在 [0-180] 度范围内.

The example histogram rendering function is also a wee bit basic - but I think it shows the idea (and how the car is predominately red!). Note how OpenCV's interpretation of Hue ranges only from [0-180] degrees.

如果您想使用蒙版来计算原始图像中的颜色 - 从第 15 行向下

if you want to use the mask to count colours in the original image - edit as so from line 15 downwards:

# split original image into hue
hsv = cv.CreateImage(cv.GetSize(orig),8,3)
hue = cv.CreateImage(cv.GetSize(orig),8,1)
cv.CvtColor(orig,hsv,cv.CV_BGR2HSV)
cv.Split(hsv, hue, None, None, None)

# split mask image into val
val = cv.CreateImage(cv.GetSize(orig),8,1)
cv.CvtColor(maskimg,hsv,cv.CV_BGR2HSV)
cv.Split(hsv, None, None, val, None)

(我认为这更符合预期,因为掩码是单独派生的,并应用于完全不同的图像.两种情况下的直方图大致相同......)

(I think this is more what was intended, as the mask is then derived separately and applied to a completely different image. The histogram is roughly the same in both cases...)

这篇关于围绕轮廓对象生成颜色直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆