Opencv二值图像的分布直方图

Mat img;
int color[111111];
int main()
{
    VideoCapture video(0);
    if (!video.isOpened())
    {
        return -1;
    }
    Mat img;
    Mat img1, img2, img3;
    Mat grayimg1, grayimg2, grayimg3;
    Mat binaryimg;
    video >> img;
    cvtColor(img, grayimg1, CV_BGR2GRAY);
    while (1)
    {
        video >> img;

        
        cvtColor(img, grayimg2, CV_BGR2GRAY);
        absdiff(grayimg2, grayimg1, grayimg3);
        medianBlur(binaryimg, binaryimg, 3);
        threshold(grayimg3, binaryimg, 40, 250, CV_THRESH_BINARY_INV);
        
        imshow("gray", grayimg3);
        imshow("binary", binaryimg);
        //----------------------------------------------
        int nRow = binaryimg.rows;
        int nCol = binaryimg.cols;
        //imshow("img", binaryimg);
        memset(color, 0, sizeof(color));
        for (int i = 0; i < nRow; i++)
        {
            uchar *data = binaryimg.ptr<uchar>(i);
            for (int j = 0; j < nCol; j++)
            {
                if (*data == 0)
                    color[j]++;
                *data++;
            }
        }
        
        int high = 450;
        Mat histimg(high, nCol, CV_8UC3);

        
        for (int j = 0; j < nCol; j += 2)
        {
            if (color[j] < 260)
                color[j] = 0;
            //else
                //color[j] = 400;
            line(histimg, Point(j, high - color[j]), Point(j, high), Scalar(0, 0, 250), 3);
        }

        
        for (int j = 0; j<nCol; j++)
        {
            
        }
        imshow("Hist", histimg);
        //--------------------------------------------------------
        if(waitKey(40) > 0)
        {
            break;
        }
    }
    waitKey(100);
    return 0;
}

 

posted @ 2015-12-17 15:31  一样菜  阅读(1393)  评论(0编辑  收藏  举报