使用一个灰度图像(一个平面)在OpenCV上绘制2d直方图



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

一个维度很好:

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)

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

所以。。。有人能启发我吗?

灰度图像已经是二维直方图:像素的强度(Ab)是由A沿x维度和b按y维度定义的bin值。通常,当谈到计算机视觉中的直方图时,人们谈论的是强度值上的直方图。对于灰度图像,这是一维直方图,其中每个仓对应于强度值的范围,并且具有与强度落在该仓中的像素数量相对应的计数。

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

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

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

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

相关内容

  • 没有找到相关文章

最新更新