Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

i think, you want a histogram here. one could use calcHist for this, but it's probably overkill.

// demo data: Mat_<uchar> data(1,48); data << 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0;

// uchar Mat's can hold 256 distinct values: Mat_<int> hist(256, 1); hist = 0; // all set to 0 initially

for (int i=0; i<data.cols; i++)="" {="" uchar="" v="data(0,i);" hist(v)="" ++;="" increase="" bin="" for="" number="" }<="" p="">

cerr << hist(0) << " zeros." << endl; cerr << hist(2) << " twos." << endl; cerr << hist(4) << " fours." << endl;

// output 35 zeros. 8 twos. 5 fours.

i think, you want a histogram here. one could use calcHist for this, but it's probably overkill.

// demo data:
Mat_<uchar> data(1,48);
data << 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0;

0;

// uchar Mat's can hold 256 distinct values: Mat_<int> hist(256, 1); hist = 0; // all set to 0 initially

initially

for (int i=0; i<data.cols; i++)="" {="" uchar="" v="data(0,i);" hist(v)="" ++;="" increase="" bin="" for="" number="" }<="" p=""> i++) { uchar v = data(0,i); hist(v) ++; // increase bin for number }

cerr << hist(0) << " zeros." << endl; cerr << hist(2) << " twos." << endl; cerr << hist(4) << " fours." << endl;

endl;

// output 35 zeros. 8 twos. 5 fours.

fours.