//2014年4月18日15:43:42
//opencv中Mat类型数据的索引与修改,和多通道Mat数据的数据提取
#include <iostream>
#include "highgui.h"
#include "cxcore.h"
using namespace std;
using namespace cv;
//提取三通道的inputMat某个通道数据,填入outputMat中
//outputMat的数据类型必须是CV_8UC1
void extractMatChannelDate(const Mat *inputMat, int channelNum, Mat *outputMat);
int main(void)
{
Mat srcImg = imread("red.jpg");
imshow("original image", srcImg);
Mat temp = cvCreateMat(srcImg.rows, srcImg.cols, CV_8UC1);
//Mat temp;//会报错,调用函数extractMatChannelDate()需要提前开辟好内存空间
extractMatChannelDate(&srcImg, 2, &temp);
#if 0
//彩色的Mat中,三个通道的颜色顺序是BGR,下面的程序是特定位置的特定通道的像素值修改
//Mat.at()函数是个模板重载函数,需要指定调用的格式,这里参数类型Vec3b,是一个存放 3 个 uchar 数据的 Vec(向量),
//后面的[0][1][2]是其索引,代表索引不同的通道数值
for (int row=0;row<srcImg.rows; row++)
{
for (int col=0; col<srcImg.cols; col++)
{
srcImg.at<Vec3b>(row, col)[0] = 0;//blue
srcImg.at<Vec3b>(row, col)[1] = 255;//green
srcImg.at<Vec3b>(row, col)[2] = 255;//red
}
}
#endif
imshow("changed image", temp);
cvWaitKey(0);
return 0;
}
void extractMatChannelDate(const Mat *inputMat, int channelNum, Mat *outputMat)
{
for (int row=0;row<inputMat->rows; row++)
{
for (int col=0; col<inputMat->cols; col++)
{
outputMat->at<uchar>(row, col) = inputMat->at<Vec3b>(row, col)[channelNum-1];
}
}
return;
}
opencv中Mat类型数据的索引修改和多通道数据提
最新推荐文章于 2024-04-07 20:12:46 发布