0

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

一维很好:

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)

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

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

4

3 回答 3

4

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

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

另外,请注意,多维直方图与三个一维直方图有很大不同

于 2011-06-21T06:13:55.237 回答
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
于 2012-08-24T00:16:14.893 回答
0

更高的暗淡。hists不仅RGB 图像分析中有意义 - 这些只是强度 hists - 而且在GLCM(灰度共现矩阵,2D)、形状上下文(dim。取决于算法)中的特征提取中也有意义) ETC。

于 2012-09-12T21:35:32.417 回答