ZBLOG

C++ opencv 求每一列像素均值,根据像素均值对每列进行二值化

要根据每列的像素均值对图像进行二值化,你可以使用OpenCV库中的threshold函数。以下是示例代码:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main()
{
    Mat image = imread("image.jpg", IMREAD_GRAYSCALE); // 读取灰度图像
    if (image.empty())
    {
        std::cout << "无法读取图像" << std::endl;
        return -1;
    }

    int rows = image.rows;
    int cols = image.cols;

    // 计算每一列的像素均值
    Mat binaryImage(rows, cols, CV_8UC1);
    for (int col = 0; col < cols; ++col)
    {
        Scalar mean_value = mean(image.col(col));
        double threshold_value = mean_value[0]; // 使用均值作为阈值

        threshold(image.col(col), binaryImage.col(col), threshold_value, 255, THRESH_BINARY);
    }

    namedWindow("Original Image", WINDOW_NORMAL);
    imshow("Original Image", image);

    namedWindow("Binary Image", WINDOW_NORMAL);
    imshow("Binary Image", binaryImage);

    waitKey(0);

    return 0;
}

确保在编译和运行代码之前,将文件名 image.jpg 替换为实际图像文件的路径。该代码将显示原始图像和二值化后的图像窗口,并将使用每列的像素均值作为阈值进行二值化处理。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=17270

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?