简体   繁体   English

OpenCV计算水平线中的像素数

[英]OpenCV counting number of pixels in a horizontal line

I wish to find number of white pixels in every row of binary image. 我希望在二进制图像的每一行中找到白色像素的数量。 And if that count is greater than 90, I wish to delete the entire row by changing each pixel value in that row to 0. The code that I wrote is not working. 如果该计数大于90,我希望通过将该行中的每个像素值更改为0来删除整行。我编写的代码无法正常工作。 And apparently, I am getting the same binary image at output. 显然,我在输出时得到了相同的二进制图像。 Please help me out in fixing the problem. 请帮助我解决问题。 BTW, am using openCV 2.0. 顺便说一句,我正在使用openCV 2.0。

using namespace std;

double a = 15;

double b = 255;

Mat I1;

int main(int argv, char **argc)

{

    cv: Mat I = imread("abc.bmp");

    if (I.empty())

    {

        std::cout << "!!! Failed imread(): image not found" << std::endl;

    }

    threshold(I, I1, a, b, THRESH_BINARY);


    int r = I.rows;

    int c = I.cols;

    for (int j = 0; j < r; j++)

    {

        int count = 0;

        for (int i = 0; i < c; i++)

        {

            if (I1.at<uchar>(j, i) == 255)

                count = count + 1;

        }

        if (count > 90)

        {

            for (int i = 0; i < c; i++)

                I1.at<uchar>(j, i) = 0;

        }

    }
    namedWindow("Display window", 0);// Create a window for display.

    imshow("Display window", I1);


    waitKey(0);

    return 0;
}

By default imread returns 3 channel BGR image. 默认情况下, imread返回3通道BGR图像。 If you want to load grayscale/binary image use cv::IMREAD_GRAYSCALE parameter: 如果要加载灰度/二进制图像,请使用cv::IMREAD_GRAYSCALE参数:

cv::Mat I = cv::imread("abc.bmp", cv::IMREAD_GRAYSCALE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM