Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenCV上使用一个灰度图像(一个平面)的2d直方图?_Opencv_2d_Histogram_Grayscale - Fatal编程技术网

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

OpenCV上使用一个灰度图像(一个平面)的2d直方图?,opencv,2d,histogram,grayscale,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

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

一维很好:

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打电话时,2d说他需要两个平面或图像:

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
它是有效的,但我得到了一个错误的直方图(对角线颜色,其余为黑色)


所以。。。有人能告诉我吗?

灰度图像已经是二维直方图:像素(A,b)的强度是由沿x维的A和沿y维的b定义的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

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

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

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
高维直方图只有在图像具有多个通道时才有意义。例如,可以计算彩色图像上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

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

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

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

感谢澄清,因为我不能做2D,我最终还是做了1d。谢谢你的澄清,因为我不能做2d,所以我最终还是做了1d。
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