I might haven't completely understood histograms... but i think i could get a 2-dimension of a grayscale image, right?
The one dimension is fine:
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开发者_StackOverflow中文版_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)
But the 2d one, when i call CalcHist, says he needs two planes, or images:
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)
This error:
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
If i use:
CalcHist([gray, gray], hist, 0)
it works, but i get a screwed up histogram (diagonal colored and the rest is black)
So... can someone enlighten me?
A grayscale image already is a two-dimensional histogram: the intensity of pixel (a, b) is value of the bin defined by a along the x-dimension and b along the y-dimension. Typically when one speaks of histograms in computer vision, one is speaking of a histogram over intensity values. For a grayscale image, this is a one-dimensional histogram where each bin corresponds to a range of intensity values and has a count corresponding to the number of pixels whose intensity falls in that bin.
Higher-dimensional histograms only make sense if the image is of multiple channels. For example, one might compute the three-dimensional histogram of RGB values over a color image. Calling CalcHist([gray, gray], hist, 0)
results in a diagonal line because every pixel in the first image (gray
) has the same value as the corresponding pixel in the second image (gray
). This fills all of the bins along the diagonal in the output histogram.
Also, note that a multi-dimension histogram is very different from three one-dimensional histogram.
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
Higher-dim. hists do not only make sence in RGB-image-analysis - those are only intensity hists - but also in feature-extraction like in the GLCM (gray-level co-occurrence matrix, 2D), shape context (dim. depends on the algorithm) etc.
精彩评论