Opencv二值图像的分布直方图

时间:2021-01-16 11:15:50
Mat img;
int color[];
int main()
{
VideoCapture video();
if (!video.isOpened())
{
return -;
}
Mat img;
Mat img1, img2, img3;
Mat grayimg1, grayimg2, grayimg3;
Mat binaryimg;
video >> img;
cvtColor(img, grayimg1, CV_BGR2GRAY);
while ()
{
video >> img; cvtColor(img, grayimg2, CV_BGR2GRAY);
absdiff(grayimg2, grayimg1, grayimg3);
medianBlur(binaryimg, binaryimg, );
threshold(grayimg3, binaryimg, , , CV_THRESH_BINARY_INV); imshow("gray", grayimg3);
imshow("binary", binaryimg);
//----------------------------------------------
int nRow = binaryimg.rows;
int nCol = binaryimg.cols;
//imshow("img", binaryimg);
memset(color, , sizeof(color));
for (int i = ; i < nRow; i++)
{
uchar *data = binaryimg.ptr<uchar>(i);
for (int j = ; j < nCol; j++)
{
if (*data == )
color[j]++;
*data++;
}
} int high = ;
Mat histimg(high, nCol, CV_8UC3); for (int j = ; j < nCol; j += )
{
if (color[j] < )
color[j] = ;
//else
//color[j] = 400;
line(histimg, Point(j, high - color[j]), Point(j, high), Scalar(, , ), );
} for (int j = ; j<nCol; j++)
{ }
imshow("Hist", histimg);
//--------------------------------------------------------
if(waitKey() > )
{
break;
}
}
waitKey();
return ;
}