本帖最后由 流放荒帝 于 2016-3-31 13:19 编辑
C++: void calcHist(vector_Mat images, vector_int channels, Mat mask, Mat& hist, vector_int histSize, vector_float ranges, bool accumulate = false)
void calcHist( InputArrayOfArrays images, const vector<int>& channels,InputArray mask, OutputArray hist,const vector<int>& histSize,const vector<float>& ranges,bool accumulate=false );
java:void calcHist(List<Mat> images, MatOfInt channels, Mat mask, Mat hist, MatOfInt histSize, MatOfFloat ranges, boolean accumulate)
//蓝绿色字体是java版本的
Mat src, dst;
// 装载图像 Mat src = imread( "文件路径" ); Mat frame= Imgproc.imread( "文件路径" );
/// 分割成3个单通道图像 ( R, G 和 B ) vector<Mat> rgb_planes; split(src, rgb_planes ); //List<Mat> images = new ArrayList<Mat>(); //Core.split(frame, images);
/// 设定bin数目 int histSize = 255;
/// 设定取值范围 ( R,G,B) ) float range[] = { 0, 255 } ; const float* histRange = { range };
//MatOfInt histSize = new MatOfInt(256); //MatOfFloat histRange = new MatOfFloat(0, 256); //MatOfInt channels = new MatOfInt(0);
bool uniform = true; bool accumulate = false;
Mat r_hist,g_hist, b_hist; //Mat hist_b = new Mat(); //Mat hist_g = new Mat(); //Mat hist_r = new Mat();
/// 计算直方图: calcHist(&rgb_planes[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist(&rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist(&rgb_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); //Imgproc.calcHist(images.subList(0, 1), channels, new Mat(), hist_b , histSize, histRange, false); //Imgproc.calcHist(images.subList(1, 2), channels, new Mat(), hist_g , histSize, histRange, false); //Imgproc.calcHist(images.subList(2, 3), channels, new Mat(), hist_r , histSize, histRange, false);
// 创建直方图画布 int hist_w = 400; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); //int hist_w = 400; // width of the histogram image //int hist_h = 400; // height of the histogram image //int bin_w = (int) Math.round(hist_w / histSize.get(0, 0)[0]);
Mat histImage(hist_w, hist_h, CV_8UC3, Scalar( 0,0,0) ); //Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC3, new Scalar(0, 0, 0));
/// 将直方图归一化到范围 [ 0, histImage.rows ] normalize(r_hist,r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(g_hist,g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(b_hist,b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); //Core.normalize(hist_b, hist_b, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat()); //Core.normalize(hist_g, hist_g, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat()); //Core.normalize(hist_r, hist_r, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
/// 在直方图画布上画出直方图 for( int i = 1; i < histSize; i++ ) { line(histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), Scalar( 0, 0, 255), 2, 8, 0 ); line(histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), Scalar( 0, 255, 0), 2, 8, 0 ); line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), Scalar( 255, 0, 0), 2, 8, 0 ); } // effectively draw the histogram(s) for (int i = 1; i < histSize.get(0, 0)[0]; i++) { // B component or gray image Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - Math.round(hist_b.get(i - 1, 0)[0])), new Point(bin_w * (i), hist_h - Math.round(hist_b.get(i, 0)[0])), new Scalar(255, 0, 0), 2, 8, 0); // G and R components (if the image is not in gray scale) Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - Math.round(hist_g.get(i - 1, 0)[0])), new Point(bin_w * (i), hist_h - Math.round(hist_g.get(i, 0)[0])), new Scalar(0, 255, 0), 2, 8,0); Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - Math.round(hist_r.get(i - 1, 0)[0])), new Point(bin_w * (i), hist_h - Math.round(hist_r.get(i, 0)[0])), new Scalar(0, 0, 255), 2, 8,0); }
Image histImg = mat2Image(histImage);
/// 显示直方图 namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE ); imshow("calcHist Demo", histImage );
waitKey(0);
return 0;
} private Image mat2Image(Mat frame) { // create a temporary buffer MatOfByte buffer = new MatOfByte(); // encode the frame in the buffer, according to the PNG format Imgcodecs.imencode(".png", frame, buffer); // build and return an Image created from the image encoded in the buffer return new Image(new ByteArrayInputStream(buffer.toArray())); }
|