OpenCV, MatBGR2ARGB, ARGB2MatBGR

代码片段~

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)