使用一个灰度图像(一个平面)在OpenCV上进行2d直方图?

时间:2011-06-21 05:07:03

标签: opencv 2d histogram grayscale

我可能还没有完全理解直方图...但我想我可以得到一个二维的灰度图像,对吗?

一维很好:

from cv import *
import os, glob, sys


original = LoadImage('test.jpg')
gray  = CreateImage(GetSize(original), IPL_DEPTH_8U, 1)
canny = CreateImage(GetSize(original), IPL_DEPTH_8U, 1)
NamedWindow('Circles', 1)


CvtColor(original, gray, CV_BGR2GRAY)

bins = 30
scale = 10
hist = CreateHist([bins], CV_HIST_ARRAY, [[0,256]], 1)
CalcHist([gray], hist)


hist_img = CreateImage([bins*scale,50], 8, 1)
Rectangle(hist_img, (0,0), (bins*scale,50), CV_RGB(255,255,255), -1)


(_, max_value, _, _) = GetMinMaxHistValue(hist)

for i in range(0,bins):
  bin_val = QueryHistValue_1D(hist, i)
  #print bin_val
  norm = Round((bin_val/max_value)*50)
  Rectangle(hist_img, (i*scale, 50), (i*scale+scale-1,50-norm), CV_RGB(0, 0, 0), CV_FILLED)             


ShowImage('Circles', hist_img)
WaitKey(0)

但是当我打电话给CalcHist时,第二个人说他需要两架飞机或图像:

from cv import *
import os, glob, sys


original = LoadImage('test.jpg')
gray  = CreateImage(GetSize(original), IPL_DEPTH_8U, 1)
NamedWindow('Circles', 1)


CvtColor(original, gray, CV_BGR2GRAY)

bins = 30
scale = 3

hist = CreateHist([bins,bins], CV_HIST_ARRAY, [[0,255], [0,255]], 1)
CalcHist([gray], hist)


hist_img = CreateImage([bins*scale,bins*scale], 8, 1)
#Rectangle(hist_img, (0,0), (bins*scale,50), CV_RGB(255,255,255), -1)
Zero(hist_img)

(_, max_value, _, _) = GetMinMaxHistValue(hist)

for h in range(0,bins):
  for s in range(0,bins):
    bin_val = QueryHistValue_2D(hist, h, s)
    inte = Round(bin_val*255/max_value)
    Rectangle(hist_img, (h*scale, s*scale), ((h+1)*scale-1,(s+1)*scale-1), CV_RGB(inte, inte, inte), CV_FILLED)             


ShowImage('Circles', hist_img)
WaitKey(0)

此错误:

OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_graphics_opencv/work/OpenCV-2.2.0/modules/core/src/matrix.cpp, line 641
Traceback (most recent call last):
  File "hist2d.py", line 16, in <module>
    CalcHist([gray], hist, 0)
cv.error: Unknown array type

如果我使用:

CalcHist([gray, gray], hist, 0)

它有效,但我得到了一个搞乱的直方图(对角线颜色,其余为黑色)

所以...有人可以启发我吗?

3 个答案:

答案 0 :(得分:4)

灰度图像已经是二维直方图:像素的强度( a b )是 a 定义的bin的值沿着x维度的em>和沿着y维度的 b 。通常,当人们谈到计算机视觉中的直方图时,人们会谈到histogram over intensity values。对于灰度图像,这是一维直方图,其中每个bin对应于一系列强度值,并且具有与强度落在该bin中的像素数对应的计数。

如果图像是多个通道,则高维直方图才有意义。例如,可以计算彩色图像上RGB值的三维直方图。调用CalcHist([gray, gray], hist, 0)会产生对角线,因为第一个图像(gray)中的每个像素都具有与第二个图像(gray)中相应像素相同的值。这会在输出直方图中沿对角线填充所有区域。

另外,请注意多维直方图与三维一维直方图非常不同

答案 1 :(得分:1)

bins = 10 # specify the number of bins
ranges = (10,255) % specify the top and bottom range of the bins. This truncates the image
hist = cv.CreateHist([bins], cv.CV_HIST_ARRAY, [ranges], 1) # create histograms
cv.CalcHist([gr], hist) # calculate the histograms for the image
(min_value, max_value, min_idx, max_idx) = cv.GetMinMaxHistValue(hist) # get the min and max values for the histogram

答案 2 :(得分:0)

更高暗淡。 hists 不仅 RGB-image-analysis中查看 - 这些只是强度嘶嘶声 - 而且还在特征提取中 < / strong>就像在GLCM(灰度共生矩阵,2D),形状上下文(昏暗。取决于算法)等。