This forum is disabled, please visit https://forum.opencv.org

1 | initial version |

This is not really a histogram so calcHist won't really help you. But you could use other functions of OpenCV to help you. Shortest way will be:

```
int* histogram = new int[width];
for(i=0; i<width; i++)
histogram[i] = height - countNonZero(yourImage.col(i));
```

This is the shortest way but not the fastest because memory access that is going along columns of image. Better memory access can be achieved by:

```
Mat histogram(Size(width,1), CV_32S, Scalar(0));
for(j=0; j<height; j++)
histogram += (yourImage==0);
histogram /= 255;
```

2 | No.2 Revision |

This is not really a histogram so calcHist won't really help you. But you could use other functions of OpenCV ~~to help you. ~~for this task. Shortest way will be:

```
int* histogram = new int[width];
for(i=0; i<width; i++)
histogram[i] = height - countNonZero(yourImage.col(i));
```

Brief explanation: Function col(i) creates Mat from i-th column of your image (without memory allocations). countNonZero counts amount of non-black pixels. And since you are interested in black pixels you just do height of your image minus non-black pixels.

This is the shortest way but not the fastest because memory access ~~that is going ~~along columns of ~~image. ~~image is slow. Better memory access (and so the speed) can be achieved by:

```
Mat histogram(Size(width,1), CV_32S, Scalar(0));
for(j=0; j<height; j++)
histogram += (yourImage==0);
histogram /= 255;
```

Brief explanation: (yourImage==0) returns binary image. Every pixel that was 0 in your image will be set to 255 (this is the reason I divided values of histogram by 255), and the rest will be set to 0. You accumulate all this into your histogram.

3 | No.3 Revision |

This is not really a histogram so calcHist won't really help you. But you could use other functions of OpenCV for this task. Shortest way will be:

```
int* histogram = new int[width];
for(i=0; i<width; i++)
histogram[i] = height - countNonZero(yourImage.col(i));
```

Brief explanation: Function col(i) creates Mat from i-th column of your image (without memory allocations). countNonZero counts amount of non-black pixels. And since you are interested in black pixels you just do height of your image minus non-black pixels.

This is the shortest way but not the fastest because memory access along columns of image is slow. Better memory access (and so the speed) can be achieved by:

```
Mat histogram(Size(width,1), CV_32S, Scalar(0));
for(j=0; j<height; j++)
histogram +=
```~~(yourImage==0);
~~(yourImage.row(j)==0);
histogram /= 255;

Brief explanation: ~~(yourImage==0) ~~(yourImage.row(j)==0) returns binary image. Every pixel that was 0 in your image will be set to 255 (this is the reason I divided values of histogram by 255), and the rest will be set to 0. You accumulate all this into your histogram.

Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.