代码片段~

unsigned int* abMatBGR2ARGB(Mat imag)
{
  int nCols;
  int nRows;
  unsigned int *pbuff = NULL;
  if(imag.empty())
  {
    cerr << "Failed read the image data." << endl;
    return NULL;
  }
  if (imag.dims != 2)
  {
    cerr << "not a image data" << endl;
    return NULL;
  }
  nCols = imag.cols;
  nRows = imag.rows;
  pbuff = new unsigned int[nCols*nRows];
  if (!pbuff)
  {
    cerr << "failed to allocate memory for the input image." << endl;
    return NULL;
  }

  if (imag.depth()!=CV_8U || imag.channels() != 3)
  {
    cerr << "error type of image channels and depth." << endl;
    return NULL;
  }
  Vec3b pix;
  uchar *tp=NULL;
  for (int row = 0; row < nRows; row++)
  {
    for (int col = 0; col < nCols; col++)
    {
      pix = imag.at<Vec3b>(row, col);
      tp = (uchar*)(pbuff + row*nCols + col);
      tp[0] = 0;//A
      tp[1] = pix[2];//R
      tp[2] = pix[1];//G
      tp[3] = pix[0];//B
    }
  }
  return pbuff;
}

void abARGB2MatBGR(unsigned int *pbuff, int nRows, int nCols, Mat &imag)
{
  uchar *tp=NULL;
  imag.create(nRows, nCols, CV_8UC3);
  for (int row = 0; row < nRows; row++)
  {
    for (int col = 0; col < nCols; col++)
    {
      tp = (uchar*)(pbuff + row*nCols + col);
      imag.at<Vec3b>(row, col)[0] = tp[3];
      imag.at<Vec3b>(row, col)[1] = tp[2];
      imag.at<Vec3b>(row, col)[2] = tp[1];
    }
  }
}

测试代码:

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

using namespace std;
using namespace cv;

unsigned int* abMatBGR2ARGB(Mat imag)
{
  int nCols;
  int nRows;
  unsigned int *pbuff = NULL;
  if(imag.empty())
  {
    cerr << "Failed read the image data." << endl;
    return NULL;
  }
  if (imag.dims != 2)
  {
    cerr << "not a image data" << endl;
    return NULL;
  }
  nCols = imag.cols;
  nRows = imag.rows;
  pbuff = new unsigned int[nCols*nRows];
  if (!pbuff)
  {
    cerr << "failed to allocate memory for the input image." << endl;
    return NULL;
  }

  if (imag.depth()!=CV_8U || imag.channels() != 3)
  {
    cerr << "error type of image channels and depth." << endl;
    return NULL;
  }
  Vec3b pix;
  uchar *tp=NULL;
  for (int row = 0; row < nRows; row++)
  {
    for (int col = 0; col < nCols; col++)
    {
      pix = imag.at<Vec3b>(row, col);
      tp = (uchar*)(pbuff + row*nCols + col);
      tp[0] = 0;//A
      tp[1] = pix[2];//R
      tp[2] = pix[1];//G
      tp[3] = pix[0];//B
    }
  }
  return pbuff;
}

void abARGB2MatBGR(unsigned int *pbuff, int nRows, int nCols, Mat &imag)
{
  uchar *tp=NULL;
  imag.create(nRows, nCols, CV_8UC3);
  for (int row = 0; row < nRows; row++)
  {
    for (int col = 0; col < nCols; col++)
    {
      tp = (uchar*)(pbuff + row*nCols + col);
      imag.at<Vec3b>(row, col)[0] = tp[3];
      imag.at<Vec3b>(row, col)[1] = tp[2];
      imag.at<Vec3b>(row, col)[2] = tp[1];
    }
  }
}

int main()
{
	Mat im = imread("../opencvt.jpeg");
	if (im.empty())
	{
		cerr << "empty" << endl;
		return -1;
	}
	imshow("org", im);
	cvWaitKey();

	unsigned int *pbuff = NULL;
	pbuff = abMatBGR2ARGB(im);

	Mat nIm;
	abARGB2MatBGR(pbuff, im.rows, im.cols, nIm);

	imshow("new", nIm);
	cvWaitKey();

	delete[] pbuff;

	return 0;
}

OpenCV, MatBGR2ARGB, ARGB2MatBGR的更多相关文章

  1. opencv在图像显示中文

    在图像定位和模式识别时,经常需要把结果标注到图片上,标注内容可以是数字字母.矩形框等(opencv支持的)或者是中文汉字(借助freetype). 1.显示数字/矩形框 #include <op ...

  2. opencv中Mat与IplImage,CVMat类型之间转换

    opencv中对图像的处理是最基本的操作,一般的图像类型为IplImage类型,但是当我们对图像进行处理的时候,多数都是对像素矩阵进行处理,所以这三个类型之间的转换会对我们的工作带来便利. Mat类型 ...

  3. opencv源码:cascadedetect

    级联分类器检测类CascadeClassifier,提供了两个重要的方法: CascadeClassifier cascade_classifier; cascade_classifier.load( ...

  4. 基于OpenCV的车辆检测与追踪的实现

    最近老师布置了一个作业,是做一个基于视频的车辆检测与追踪,用了大概两周的时间做了一个简单的,效果不是很理想,但抑制不住想把自己的一些认识写下来,这里就把一些网络上的博客整理一下分享给大家,希望帮助到大 ...

  5. OpenCV人脸识别Eigen算法源码分析

    1 理论基础 学习Eigen人脸识别算法需要了解一下它用到的几个理论基础,现总结如下: 1.1 协方差矩阵 首先需要了解一下公式: 共公式可以看出:均值描述的是样本集合的平均值,而标准差描述的则是样本 ...

  6. OpenCV人脸识别LBPH算法源码分析

    1 背景及理论基础 人脸识别是指将一个需要识别的人脸和人脸库中的某个人脸对应起来(类似于指纹识别),目的是完成识别功能,该术语需要和人脸检测进行区分,人脸检测是在一张图片中把人脸定位出来,完成的是搜寻 ...

  7. OpenCV模板匹配算法详解

    1 理论介绍 模板匹配是在一幅图像中寻找一个特定目标的方法之一,这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否“相似”,当相似度足够高时,就认为找到了我们的目标.OpenCV ...

  8. android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测

    目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. (原创:转载声明出处:http://www.cnblogs.com/lin ...

  9. 海康网络摄像机YV12转换为BGR,由opencv Mat显示 (转)

    我使用的是海康DS-2CD852MF-E, 200万,网络摄像机,已经比较老了,不过SDK在海康官网下载的,开发流程都差不多. 海康摄像机回调解码后的视频数据格式为YV12,顺便说一下YV12的数据格 ...

随机推荐

  1. [LeetCode] Minimum Index Sum of Two Lists 两个表单的最小坐标和

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  2. vim 多行缩进

    按v进入可视化模式后, 选中要缩进的多行, 后按shift+.实现多行缩进.

  3. burp的dns记录功能

    写个备忘. 先点copy复制url,然后生成的url就能用了. 一关就没了,比较鸡肋些,不适合持久的,适合马上就能见效果的那种.

  4. SSH执行hql报错:Caused by: org.hibernate.hql.ast.QuerySyntaxException: user is not mapped [from user where username = ?]

    报错信息: ERROR Dispatcher:38 - Exception occurred during processing request: user is not mapped [from u ...

  5. [APIO 2016]Gap

    Description 题库链接 给你一个长度为 \(N\) 的单调递增序列 \(A\) .交互时允许你调用 MinMax(s, t, &mn, &mx) 函数,表示序列元素的值在 \ ...

  6. [HNOI 2002]营业额统计

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...

  7. hihoCoder 1595 : Numbers

    Description You are given n constant integers c[1], c[2], ..., c[n] and an integer k. You are to ass ...

  8. ●洛谷P3233 [HNOI2014]世界树

    题链: https://www.luogu.org/problemnew/show/P3233题解: 虚树,dp,倍增. 首先对于每个询问,要把虚树建出来,这一步就从略了.这里着重分享一下如何求答案. ...

  9. ●SPOJ 1811 Longest Common Substring

    题链: http://poj.org/problem?id=2774 题解: 求两个字符串(S,T)的最长公共子串.对 S串建后缀自动机.接下来就用这个自动机去求出能和 S串匹配的 T的每一个前缀的最 ...

  10. Tenka1 Programmer Contest D - IntegerotS

    Problem Statement Seisu-ya, a store specializing in non-negative integers, sells N non-negative inte ...