opencv中Mat类型数据的索引修改和多通道数据提

//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;
}


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值