本文介绍了如何将OpenCV Mat的所有像素设置为特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CV_8UC1类型的图像.如何将所有像素值设置为特定值?

I have an image of type CV_8UC1. How can I set all pixel values to a specific value?

推荐答案

  • 对于灰度图像:

    cv::Mat m(100, 100, CV_8UC1); //gray
    m = Scalar(5);  //used only Scalar.val[0]
    

    cv::Mat m(100, 100, CV_8UC1); //gray
    m.setTo(Scalar(5));  //used only Scalar.val[0]
    

    Mat mat = Mat(100, 100, CV_8UC1, cv::Scalar(5));
    

  • 用于彩色图像(例如3个通道)

    cv::Mat m(100, 100, CV_8UC3); //3-channel
    m = Scalar(5, 10, 15);  //Scalar.val[0-2] used
    

    cv::Mat m(100, 100, CV_8UC3); //3-channel
    m.setTo(Scalar(5, 10, 15));  //Scalar.val[0-2] used
    

    Mat mat = Mat(100, 100, CV_8UC3, cv::Scalar(5,10,15));
    

  • PS :如果您进一步想知道如何设置给定值,请查看此线程cv::Mat的通道有效地转换为给定值,而无需更改其他通道.

    P.S.: Check out this thread if you further want to know how to set given channel of a cv::Mat to a given value efficiently without changing other channels.

    这篇关于如何将OpenCV Mat的所有像素设置为特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 05:28