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的更多相关文章
- opencv在图像显示中文
在图像定位和模式识别时,经常需要把结果标注到图片上,标注内容可以是数字字母.矩形框等(opencv支持的)或者是中文汉字(借助freetype). 1.显示数字/矩形框 #include <op ...
- opencv中Mat与IplImage,CVMat类型之间转换
opencv中对图像的处理是最基本的操作,一般的图像类型为IplImage类型,但是当我们对图像进行处理的时候,多数都是对像素矩阵进行处理,所以这三个类型之间的转换会对我们的工作带来便利. Mat类型 ...
- opencv源码:cascadedetect
级联分类器检测类CascadeClassifier,提供了两个重要的方法: CascadeClassifier cascade_classifier; cascade_classifier.load( ...
- 基于OpenCV的车辆检测与追踪的实现
最近老师布置了一个作业,是做一个基于视频的车辆检测与追踪,用了大概两周的时间做了一个简单的,效果不是很理想,但抑制不住想把自己的一些认识写下来,这里就把一些网络上的博客整理一下分享给大家,希望帮助到大 ...
- OpenCV人脸识别Eigen算法源码分析
1 理论基础 学习Eigen人脸识别算法需要了解一下它用到的几个理论基础,现总结如下: 1.1 协方差矩阵 首先需要了解一下公式: 共公式可以看出:均值描述的是样本集合的平均值,而标准差描述的则是样本 ...
- OpenCV人脸识别LBPH算法源码分析
1 背景及理论基础 人脸识别是指将一个需要识别的人脸和人脸库中的某个人脸对应起来(类似于指纹识别),目的是完成识别功能,该术语需要和人脸检测进行区分,人脸检测是在一张图片中把人脸定位出来,完成的是搜寻 ...
- OpenCV模板匹配算法详解
1 理论介绍 模板匹配是在一幅图像中寻找一个特定目标的方法之一,这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否“相似”,当相似度足够高时,就认为找到了我们的目标.OpenCV ...
- android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测
目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. (原创:转载声明出处:http://www.cnblogs.com/lin ...
- 海康网络摄像机YV12转换为BGR,由opencv Mat显示 (转)
我使用的是海康DS-2CD852MF-E, 200万,网络摄像机,已经比较老了,不过SDK在海康官网下载的,开发流程都差不多. 海康摄像机回调解码后的视频数据格式为YV12,顺便说一下YV12的数据格 ...
随机推荐
- [LeetCode] Zuma Game 祖玛游戏
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...
- xcode8的那些坑儿
前几天手又贱,更新了xcode8....被几个坑玩坏了.最起码,字体改了,我现在还没有适应.下面列举了这两天遇到的问题 1.关于相册,照相,通讯录,麦克风的权限问题 xcode8打完包安装后,你会发现 ...
- [SDOI 2016]征途
Description 题库链接 将一个长度为 \(n\) 的正整数序列分为 \(m\) 段,问你这 \(m\) 段最小的方差 \(v\) 为多少.输出 \(v\times m^2\) . \(1\l ...
- [SDOI 2009]HH的项链
Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝 壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此,他的项链变得 ...
- [HNOI 2004]L语言
Description 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D ...
- 【LSGDOJ 1333】任务安排 dp
题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti.在每批任务开始 ...
- 【USACO】AC自动机
Description 对,这就是裸的AC自动机. 要求:在规定时间内统计出模版字符串在文本中出现的次数. Input 第一行:模版字符串的个数N. 第2->N+1行:N个字符串.(每个模版字符 ...
- 基于 Hexo + GitHub Pages 搭建个人博客(三)
一.添加扫描二维码关注功能 打开 themes 目录下的 next 主题配置文件,找到 Wechat Subscriber 标签,将该标签下的配置改成如下形式: # Wechat Subscriber ...
- 使用JdbcTemplate过程中使用到多个参数和like模糊
项目中经常会用到模糊查询,最近使用JdbcTemplate过程中就遇到了. 一开始尝试了拼接的方式去 String sql = "select count(1) from web_users ...
- mysql 合并left join 数据条目
查询两张关联表信息,原sql: SELECT * FROM car_life_info c left JOIN shop_label sl ON c.shop_id = sl.shop_id 出现 ...