1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" using namespace cv;
int main() {
Mat srcImage, hsvImage; srcImage = imread("1.jpg"); cvtColor(srcImage, hsvImage, COLOR_BGR2HSV);
int hueBinNum = 30; int saturationBinNum = 32; int histSize[] = {hueBinNum, saturationBinNum}; float hueRanges[] = {0, 180}; float saturationRanges[] = {0, 256}; const float *ranges[] = {hueRanges, saturationRanges}; MatND dstHist; int channels[] = {0, 1};
calcHist(&hsvImage, 1, channels, Mat(), dstHist, 2, histSize, ranges, true, false);
double maxValue = 0; minMaxLoc(dstHist, 0, &maxValue, 0, 0); int scale = 10; Mat histImg = Mat::zeros(saturationBinNum * scale, hueBinNum * 10, CV_8UC3);
for (int hue = 0; hue < hueBinNum; hue++) for (int saturation = 0; saturation < saturationBinNum; saturation++) { float binValue = dstHist.at<float>(hue, saturation); int intensity = cvRound(binValue * 255 / maxValue);
rectangle(histImg, Point(hue * scale, saturation * scale), Point((hue + 1) * scale - 1, (saturation + 1) * scale - 1), Scalar::all(intensity), FILLED); }
imshow("Original", srcImage); imshow("H-S histogram", histImg);
waitKey(); }
|