-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hist.java
121 lines (99 loc) · 6 KB
/
Hist.java
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package app;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
class Hist {
/**
*
* @param mat Assumed to be BGR. Histogram is overlaid on the input image.
*/
public void displayHist(Mat mat) {
Mat histImage = new Mat();
//////////////////////////////////////
// RGB HISTOGRAM OF IMAGE
// May be better to try the HSV histogram
//
// histogram of image mat before any other drawing on mat
//! [Separate the image in 3 places ( B, G and R )]
List<Mat> bgrPlanes = new ArrayList<>();
// try converts
//Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2GRAY);
//Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2HLS);
Core.split(mat, bgrPlanes);
//! [Separate the image in 3 places ( B, G and R )]
//! [Establish the number of bins]
int histSize = 128;
//! [Establish the number of bins]
//! [Set the ranges ( for B,G,R) )]
float[] range = {0, 256}; //the upper boundary is exclusive
MatOfFloat histRange = new MatOfFloat(range);
//! [Set the ranges ( for B,G,R) )]
//! [Set histogram param]
boolean accumulate = false;
//! [Set histogram param]
//! [Compute the histograms]
Mat bHist = new Mat(), gHist = new Mat(), rHist = new Mat();
Imgproc.calcHist(bgrPlanes, new MatOfInt(0), new Mat(), bHist, new MatOfInt(histSize), histRange, accumulate);
Imgproc.calcHist(bgrPlanes, new MatOfInt(1), new Mat(), gHist, new MatOfInt(histSize), histRange, accumulate);
Imgproc.calcHist(bgrPlanes, new MatOfInt(2), new Mat(), rHist, new MatOfInt(histSize), histRange, accumulate);
//System.out.println("bHist = " + bHist + "gHist = " + gHist + "rHist = " + rHist);
//! [Compute the histograms]
//! [Draw the histograms for B, G and R]
int histW = 128;
int histH = 99;
histImage = new Mat( histH, histW, CvType.CV_8UC3, new Scalar( 0,0,0) );
int binW = (int) Math.round((double) histW / histSize);
//! [Draw the histograms for B, G and R]
//! [Normalize the result to ( 0, histImage.rows )]
Core.normalize(bHist, bHist, 0, histImage.rows(), Core.NORM_MINMAX);
Core.normalize(gHist, gHist, 0, histImage.rows(), Core.NORM_MINMAX);
Core.normalize(rHist, rHist, 0, histImage.rows(), Core.NORM_MINMAX);
//! [Normalize the result to ( 0, histImage.rows )]
//! [Draw for each channel]
float[] bHistData = new float[(int) (bHist.total() * bHist.channels())];
bHist.get(0, 0, bHistData);
float[] gHistData = new float[(int) (gHist.total() * gHist.channels())];
gHist.get(0, 0, gHistData);
float[] rHistData = new float[(int) (rHist.total() * rHist.channels())];
rHist.get(0, 0, rHistData);
for( int i = 1; i < histSize; i++ ) {
Imgproc.line(histImage.submat(0, histH/3, 0, histW), new Point(binW * (i - 1), histH/3 - Math.round(bHistData[i - 1])),
new Point(binW * (i), histH/3 - Math.round(bHistData[i])), new Scalar(255, 0, 0), 2, Imgproc.LINE_4);
Imgproc.line(histImage.submat(histH/3, histH*2/3, 0, histW), new Point(binW * (i - 1), histH/3 - Math.round(gHistData[i - 1])),
new Point(binW * (i), histH/3 - Math.round(gHistData[i])), new Scalar(0, 255, 0), 2, Imgproc.LINE_4);
Imgproc.line(histImage.submat(histH*2/3, histH, 0, histW), new Point(binW * (i - 1), histH/3 - Math.round(rHistData[i - 1])),
new Point(binW * (i), histH/3 - Math.round(rHistData[i])), new Scalar(0, 0, 255), 2, Imgproc.LINE_4);
// System.out.print(
// binW * (i - 1) + " " +
// (histH - Math.round(rHistData[i - 1])) + " ");
}
//! [Draw for each channel]
// end histogram of image mat before any other drawing on mat
Mat subMat = new Mat(); // place for small image inserted into large image
subMat = mat.submat(0, histImage.rows(), 0, histImage.cols()); // define the
// insert area on the main image
if( ! histImage.empty() )
{
Core.addWeighted(subMat, .20, histImage, .80, 0, subMat);
}
//
// END RGB HISTOGRAM OF IMAGE - except the presentation of it a couple oflines below
//////////////////////////////////////
}
}
/*
images; Source arrays. They all should have the same depth, CV_8U or CV_32F, and the same size. Each of them can have an arbitrary number of channels.
channels:List of the dims channels used to compute the histogram. The first array channels are numerated from 0 to images[0].channels()-1, the second array channels are counted from images[0].channels() to images[0].channels() + images[1].channels()-1, and so on.
mask: Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size as images[i]. The non-zero mask elements mark the array elements counted in the histogram.
hist:L Output histogram, which is a dense or sparse dims -dimensional array.
histSize: Array of histogram sizes in each dimension.
ranges: Array of the dims arrays of the histogram bin boundaries in each dimension. When the histogram is uniform (uniform =true), then for each dimension i it is enough to specify the lower (inclusive) boundary L_0 of the 0-th histogram bin and the upper (exclusive) boundary U_(histSize[i]-1) for the last histogram bin histSize[i]-1. That is, in case of a uniform histogram each of ranges[i] is an array of 2 elements. When the histogram is not uniform (uniform=false), then each of ranges[i] contains histSize[i]+1 elements: L_0, U_0=L_1, U_1=L_2,..., U_(histSize[i]-2)=L_(histSize[i]-1), U_(histSize[i]-1). The array elements, that are not between L_0 and U_(histSize[i]-1), are not counted in the histogram.
accumulate: Accumulation flag. If it is set, the histogram is not cleared in the beginning when it is allocated. This feature enables you to compute a single histogram from several sets of arrays, or to update the histogram in time.
*/