opencv如何给单通道的Mat赋值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv如何给单通道的Mat赋值相关的知识,希望对你有一定的参考价值。
Mat dist(10,10,CV_8UC1);
循环体的赋值语句怎么写
Mat dist(10, 10, CV_8UC1);
for (int i = 0; i<dist.rows; ++i)
for (int j = 0; j<dist.cols; ++j)
uchar& val = dist.at<uchar>(i, j);
val = rng.uniform(0, 255);
本回答被提问者和网友采纳
opencv学习-第四节:图像对象的创建与赋值
知识点: OpenCV中图像对象创建与赋值
C+ +中Mat对象与创建
Python中Numpy数组对象
1.Mat基本结构
数据部分是像素集的一个集合
头部:图像的属性,尺寸、通道数、
2.Mat的赋值方法
一共三种方法:1.克隆 2.复制 3.赋值
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
//创建一个空白图像,zeros表示将所有像素点初始化为0,长宽为8*8,1表示单通道
Mat m3 = Mat::zeros(Size(8, 8), CV_8UC1);
std::cout << m3 << std::endl;
}
单通道是一位,等于八个字节。
三通道是3位,等于24个字节。
如何操作mat,如何遍历访问它每一个像素点
如何创建一个空的图像(mat)
3.获取图像的宽度、长度和通道数
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::zeros(Size(8, 8), CV_8UC3);
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
}
4.所有像素点初始化为1
以下代码将所有像素点初始化为1,注意通道数要为1,如果是3的话,第一个通道是1,其余为0
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::ones(Size(8, 8), CV_8UC1);
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
}
5.将第一个通道自定义一个数值
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::ones(Size(8, 8), CV_8UC3);
m3 = 125;
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
}
6.将三个通道都赋值
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::ones(Size(8, 8), CV_8UC3);
m3 = Scalar(125,125,125);
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
}
7.显示创建的图像-imshow(“创建图像”, m3);
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::ones(Size(8, 9), CV_8UC3);
m3 = Scalar(125,125,125);
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
imshow("创建图像", m3);
}
左上角即为刚刚创建好的图像(m3)
其三个通道的像素值都是125。
8.创建有颜色的图像
蓝色:
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::ones(Size(400, 400), CV_8UC3);
m3 = Scalar(255,0,0);
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
imshow("创建图像", m3);
}
红色:
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::ones(Size(300,300), CV_8UC3);
m3 = Scalar(0,0,255);
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
imshow("创建图像", m3);
}
绿色
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::ones(Size(300,300), CV_8UC3);
m3 = Scalar(0,255,0);
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
imshow("创建图像", m3);
}
8.定义一个新的图像窗口,克隆或是复制旧图像赋值给新图像
方法1:克隆
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::ones(Size(300,300), CV_8UC3);
m3 = Scalar(0,255,0);
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
Mat m4 = m3.clone();//m4为m3的克隆体
m4 = Scalar(0, 255, 255); //在克隆的基础上更改像素值。
imshow("图像3", m3);//命名并显示两张图片m3、m4
imshow("图像4", m4);
}
方法2:复制
void QuickDemo::mat_creation_demo(Mat &image) {
Mat m1, m2;
m1 = image.clone();
image.copyTo(m2);
Mat m3 = Mat::ones(Size(300,300), CV_8UC3);
m3 = Scalar(0,255,0);
std::cout << "width: " << m3.cols << "height: " << m3.rows << "channels: "<<m3.channels()<< std::endl;
std::cout << m3 << std::endl;
Mat m4;
m3.copyTo(m4);//m4为m3的克隆体
m4 = Scalar(0, 255, 255); //在克隆的基础上更改像素值。
imshow("图像3", m3);//命名并显示两张图片m3、m4
imshow("图像4", m4);
}
以上是关于opencv如何给单通道的Mat赋值的主要内容,如果未能解决你的问题,请参考以下文章