数码控科技猎奇Iphone动漫星座游戏电竞lolcosplay王者荣耀攻略allcnewsBLOGNEWSBLOGASKBLOGBLOGZSK全部技术问答问答技术问答it问答代码软件新闻开发博客电脑/网络手机/数码笔记本电脑互联网操作系统软件硬件编程开发360产品资源分享电脑知识文档中心IT全部全部分类全部分类技术牛文全部分类教程最新网页制作cms教程平面设计媒体动画操作系统网站运营网络安全服务器教程数据库工具网络安全软件教学vbscript正则表达式javascript批处理更多»编程更新教程更新游戏更新allitnewsJava新闻网络医疗信息化安全创业站长电商科技访谈域名会议专栏创业动态融资创投创业学院 / 产品经理创业公司人物访谈营销开发数据库服务器系统虚拟化云计算嵌入式移动开发作业作业1常见软件all电脑网络手机数码生活游戏体育运动明星影音休闲爱好文化艺术社会民生教育科学医疗健康金融管理情感社交地区其他电脑互联网软件硬件编程开发360相关产品手机平板其他电子产品摄影器材360硬件通讯智能设备购物时尚生活常识美容塑身服装服饰出行旅游交通汽车购房置业家居装修美食烹饪单机电脑游戏网页游戏电视游戏桌游棋牌游戏手机游戏小游戏掌机游戏客户端游戏集体游戏其他游戏体育赛事篮球足球其他运动球类运动赛车健身运动运动用品影视娱乐人物音乐动漫摄影摄像收藏宠物幽默搞笑起名花鸟鱼虫茶艺彩票星座占卜书画美术舞蹈小说图书器乐声乐小品相声戏剧戏曲手工艺品历史话题时事政治就业职场军事国防节日风俗法律法规宗教礼仪礼节自然灾害360维权社会人物升学入学人文社科外语资格考试公务员留学出国家庭教育学习方法语文物理生物工程学农业数学化学健康知识心理健康孕育早教内科外科妇产科儿科皮肤科五官科男科整形中医药品传染科其他疾病医院两性肿瘤科创业投资企业管理财务税务银行股票金融理财基金债券保险贸易商务文书国民经济爱情婚姻家庭烦恼北京上海重庆天津黑龙江吉林辽宁河北内蒙古山西陕西宁夏甘肃青海新疆西藏四川贵州云南河南湖北湖南山东江苏浙江安徽江西福建广东广西海南香港澳门台湾海外地区

利用OPENCV为android开发畸变校正的JNI库方法

来源:脚本之家  责任编辑:小易  

需要为项目提供一套畸变校正的算法,由于需要大量的矩阵运算,考虑到效率和适时性,使用JNI开发,希望把有关数组短阵的处理的变换全部放入C语言中处理。

主要用于android移动端,大致的数据来源一是从camera直接读取YUV数据,一种是从第三方接读取RGB数据,另一种是直接对BITMAP进行处理。

1.考虑到硬件设备接口,第三方软件接口,图像接口,OPENCV接口,希望能够开发出通用的算法库,一劳永逸的解决各种复杂的使用场景,因此数据要支持YUV,支持ARGB,支持MAT

2android对BITMAP有获取像素点的操作,也有通过象素点生成BITMAP的操作,而且有很多图像处理接口和第三方可以处理RGB矩阵,如

bm.getPixels(pixs, 0, w, 0, 0, w, h);
int[] pixs1 = new int[w*h]; 
    final Bitmap bm2 = Bitmap.createBitmap(pixs1, w, h, Bitmap.Config.ARGB_8888);

因此设计如下接口,入口为ARGB的整型,输出也是整型

public static native boolean RgbaUndistort(int[] argb, int width, int height, int[] pixels);

3考虑到有些情况需要二维数组,

public static native boolean RgbaUndistort2(int[][] rgb, int width, int height, int[] pixels);

4考虑到OPENCV的MAT结构,由于MAT有matToBitmap可以直接转化为BITMAP,应用MAT 提供

public static native boolean RgbaUndistortMat(int[] argb, int width, int height, long pArgbOutMatAddr);

5考虑到第三方使用MAT的情况,因此输入也可以支持MAT因此设计接口

public static native boolean RgbMatUndistortMat(long pArgbMatAddr, int width, int height, long pArgbOutMatAddr);

6考虑到摄像头输出YUV,提供YUV数据处理, 一个输出RGB, 一个输出MAT

public static native boolean YuvNv21UndistortRgba(byte[] YuvNv21, int width, int height, int[] pixels);
public static native boolean YuvNv21UndistortRgbaMat(byte[] YuvNv21, int width, int height, long pMatAddr);

7考虑到可能有不需要畸变的场合,为YUV设计一个灰度,一个RGB接口

public static native boolean YuvNv21ToGray(byte[] YuvNv21,int width, int height, int[] pixels);
public static native boolean YuvNv21ToRGBA(byte[] YuvNv21, int width, int height, int[] pixels);

8于是编写简单的JAVA头源生类

public class ImageProc3 {
	static {
		System.loadLibrary("ImgProc3");
	}
	
	public static native boolean YuvNv21ToGray(byte[] YuvNv21,int width, int height, int[] pixels);
	public static native boolean YuvNv21ToRGBA(byte[] YuvNv21, int width, int height, int[] pixels);
	
	
	public static native boolean RgbaUndistort(int[] argb, int width, int height, int[] pixels);
	public static native boolean RgbaUndistort2(int[][] rgb, int width, int height, int[] pixels);
	public static native boolean RgbaUndistortMat(int[] argb, int width, int height, long pArgbOutMatAddr);
	public static native boolean RgbMatUndistortMat(long pArgbMatAddr, int width, int height, long pArgbOutMatAddr);
	
	public static native boolean YuvNv21UndistortRgba(byte[] YuvNv21, int width, int height, int[] pixels);
	public static native boolean YuvNv21UndistortRgbaMat(byte[] YuvNv21, int width, int height, long pMatAddr);
 
}

进入BIN目录的classes文件夹使用java -classpath . -jni 生成C头文件

根据头文件编写实现的C代码

#include <stdio.h>
#include <jni.h>
#include<Android/log.h>
 
 
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
 
 
using namespace std;
using namespace cv;
 
 
#define TAG  "Camera XXXXX" // 锟斤拷锟斤拷锟斤拷远锟斤拷锟斤拷LOG锟侥憋拷识
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__) // 锟斤拷锟斤拷LOGD锟斤拷锟斤拷
 
 
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:   ImgProc_ImageProc3
 * Method:  YuvNv21ToGray
 * Signature: ([BII[I)Z
 */
JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_YuvNv21ToGray
 (JNIEnv *jenv, jclass jclassz, jbyteArray YuvNv21, jint width, jint height, jintArray pixels){
 
 
	jbyte * pNV21FrameData = jenv->GetByteArrayElements(YuvNv21, 0);
	jint * poutPixels = jenv->GetIntArrayElements(pixels, 0);
 
 
	Mat mNV(height, width, CV_8UC1, (unsigned char*) pNV21FrameData);
	Mat mBgra(height, width, CV_8UC4, (unsigned char*) poutPixels);
 
 
	cvtColor(mNV, mBgra, CV_YUV420sp2RGBA);
 
 
	jenv->ReleaseByteArrayElements(YuvNv21, pNV21FrameData, 0);
	jenv->ReleaseIntArrayElements(pixels, poutPixels, 0);
 
 
  return true;
}
 
 
/*
 * Class:   ImgProc_ImageProc3
 * Method:  YuvNv21ToRGBA
 * Signature: ([BII[I)Z
 */
JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_YuvNv21ToRGBA
 (JNIEnv *jenv, jclass jclassz, jbyteArray YuvNv21, jint width, jint height, jintArray pixels){
	jbyte * pBuf = (jbyte*) jenv->GetByteArrayElements(YuvNv21, 0);
	jint * poutPixels = jenv->GetIntArrayElements(pixels, 0);
 
 
	Mat image(height + height / 2, width, CV_8UC1, (unsigned char *) pBuf);
	Mat rgba(height, width, CV_8UC4, (unsigned char*) poutPixels);
	Mat tmp(height, width, CV_8UC4);
	cvtColor(image, tmp, CV_YUV420sp2RGBA);
 
 
	vector <Mat> channels;
	split(tmp, channels);
	Mat r = channels.at(0);
	Mat g = channels.at(1);
	Mat b = channels.at(2);
	Mat a = channels.at(3);
 
 
	vector <Mat> mbgr(4);
	mbgr[0] = b;
	mbgr[1] = g;
	mbgr[2] = r;
	mbgr[3] = a;
 
 
	merge(mbgr, rgba);
 
 
	jenv->ReleaseByteArrayElements(YuvNv21, pBuf, 0);
	jenv->ReleaseIntArrayElements(pixels, poutPixels, 0);
 
 
	return true;
}
 
 
/*
 * Class:   ImgProc_ImageProc3
 * Method:  RgbaUndistort
 * Signature: ([III[I)Z
 */
JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_RgbaUndistort
 (JNIEnv *jenv, jclass jclassz, jintArray argb, jint width, jint height, jintArray pixels){
	jint * poutPixels = jenv->GetIntArrayElements(pixels, 0);
	jint * pinPixels = jenv->GetIntArrayElements(argb, 0);
 
 
	Mat out(height, width, CV_8UC4, (unsigned char*) poutPixels);
	Mat in(height, width, CV_8UC4, (unsigned char*) pinPixels);
 
 
	double cam[] = {width, 0, width / 2, 0, height, height / 2, 0, 0, 1 };
	double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 };
 
 
	Mat camMat = Mat(3, 3, CV_64FC1, cam);
	Mat disMat = Mat(5, 1, CV_64FC1, distort);
	undistort(in, out, camMat, disMat);
 
 
	jenv->ReleaseIntArrayElements(argb, pinPixels, 0);
	jenv->ReleaseIntArrayElements(pixels, poutPixels, 0);
	return true;
}
 
 
/*
 * Class:   ImgProc_ImageProc3
 * Method:  RgbaUndistort2
 * Signature: ([[III[I)Z
 */
JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_RgbaUndistort2(JNIEnv *jenv,
		jclass jclassz, jobjectArray argb, jint width, jint height,
		jintArray pixels) {
 
 
	jint i, j;
	int row = jenv->GetArrayLength(argb);
	jintArray myarray = (jintArray)(jenv->GetObjectArrayElement(argb, 0));
	int col = jenv->GetArrayLength(myarray);
	jint jniData[row][col];
	LOGD("jiaXXX %s", "Java_ImgProc_ImageProc_convertRGB3");
	for (i = 0; i < row; i++) {
		myarray = (jintArray)(jenv->GetObjectArrayElement(argb, i));
		jint *coldata = jenv->GetIntArrayElements(myarray, 0);
 
 
		for (j = 0; j < col; j++) {
			jniData[i][j] = coldata[j];
			LOGD("jiaXXX %d", jniData[i][j]);
		}
 
 
		jenv->ReleaseIntArrayElements(myarray, coldata, 0);
 
 
	}
 
 
	Mat img = Mat(row, col, CV_8UC4, jniData);
	LOGD("jiaXXX %x", img.at<unsigned int>(1, 1));
 
 
	double cam[] = {width, 0, width / 2, 0, height, height / 2, 0, 0, 1 };
	double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 };
 
 
	Mat camMat = Mat(3, 3, CV_64FC1, cam);
	Mat disMat = Mat(5, 1, CV_64FC1, distort);
 
 
	jint * poutPixels = jenv->GetIntArrayElements(pixels, 0);
	Mat out(height, width, CV_8UC4, (unsigned char*) poutPixels);
	undistort(img, out, camMat, disMat);
 
 
	jenv->ReleaseIntArrayElements(pixels, poutPixels, 0);
 
 
	return true;
}
 
 
/*
 * Class:   ImgProc_ImageProc3
 * Method:  RgbaUndistortMat
 * Signature: ([IIIJ)Z
 */
JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_RgbaUndistortMat
 (JNIEnv *jenv, jclass jclassz, jintArray argb, jint width, jint height, jlong pArgbOutMatAddr){
 
 
	//jint * poutPixels = jenv->GetIntArrayElements(pixels, 0);
	jint * pinPixels = jenv->GetIntArrayElements(argb, 0);
 
 
	//Mat out(height, width, CV_8UC4, (unsigned char*) poutPixels);
	Mat in(height, width, CV_8UC4, (unsigned char*) pinPixels);
	Mat out = *((Mat*)pArgbOutMatAddr);
 
 
	double cam[] = {width, 0, width / 2, 0, height, height / 2, 0, 0, 1 };
	double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 };
 
 
	Mat camMat = Mat(3, 3, CV_64FC1, cam);
	Mat disMat = Mat(5, 1, CV_64FC1, distort);
	undistort(in, out, camMat, disMat);
 
 
	jenv->ReleaseIntArrayElements(argb, pinPixels, 0);
	//jenv->ReleaseIntArrayElements(pixels, poutPixels, 0);
 
 
	return true;
}
 
 
/*
 * Class:   ImgProc_ImageProc3
 * Method:  RgbMatUndistortMat
 * Signature: (JIIJ)Z
 */
JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_RgbMatUndistortMat
 (JNIEnv *jenv, jclass jclassz, jlong pArgbMatAddr, jint width, jint height, jlong pArgbOutMatAddr){
 
 
	Mat in=*((Mat*)pArgbMatAddr);
	Mat out = *((Mat*)pArgbOutMatAddr);
 
 
	double cam[] = {width, 0, width / 2, 0, height, height / 2, 0, 0, 1 };
	double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 };
 
 
	Mat camMat = Mat(3, 3, CV_64FC1, cam);
	Mat disMat = Mat(5, 1, CV_64FC1, distort);
	undistort(in, out, camMat, disMat);
 
 
	return true;
}
 
 
/*
 * Class:   ImgProc_ImageProc3
 * Method:  YuvNv21UndistortRgba
 * Signature: ([BII[I)Z
 */
JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_YuvNv21UndistortRgba
 (JNIEnv *jenv, jclass jclassz, jbyteArray YuvNv21, jint width, jint height, jintArray pixels){
 
 
	jbyte * pBuf = (jbyte*) jenv->GetByteArrayElements(YuvNv21, 0);
	jint * poutPixels = jenv->GetIntArrayElements(pixels, 0);
 
 
	Mat image(height + height / 2, width, CV_8UC1, (unsigned char *) pBuf);
	Mat rgba(height, width, CV_8UC4, (unsigned char*) poutPixels);
	Mat tmp(height, width, CV_8UC4);
	cvtColor(image, tmp, CV_YUV420sp2RGBA);
 
 
	double cam[] = { width, 0, width / 2, 0, height, height / 2, 0, 0, 1 };
	double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 };
 
 
	Mat camMat = Mat(3, 3, CV_64FC1, cam);
	Mat disMat = Mat(5, 1, CV_64FC1, distort);
	undistort(tmp, tmp, camMat, disMat);
 
 
	vector < Mat > channels;
	split(tmp, channels);
	Mat r = channels.at(0);
	Mat g = channels.at(1);
	Mat b = channels.at(2);
	Mat a = channels.at(3);
 
 
	vector < Mat > mbgr(4);
	mbgr[0] = b;
	mbgr[1] = g;
	mbgr[2] = r;
	mbgr[3] = a;
 
 
	merge(mbgr, rgba);
 
 
	jenv->ReleaseByteArrayElements(YuvNv21, pBuf, 0);
	jenv->ReleaseIntArrayElements(pixels, poutPixels, 0);
 
 
	return true;
}
 
 
/*
 * Class:   ImgProc_ImageProc3
 * Method:  YuvNv21UndistortRgbaMat
 * Signature: ([BIIJ)Z
 */
JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_YuvNv21UndistortRgbaMat
 (JNIEnv *jenv, jclass jclassz, jbyteArray YuvNv21, jint width, jint height, jlong pMatAddr){
 
 
	jbyte * pBuf = (jbyte*) jenv->GetByteArrayElements(YuvNv21, 0);
	//jint * poutPixels = jenv->GetIntArrayElements(pixels, 0);
 
 
	Mat image(height + height / 2, width, CV_8UC1, (unsigned char *) pBuf);
	//Mat rgba(height, width, CV_8UC4, (unsigned char*) poutPixels);
	Mat rgba = *((Mat*) pMatAddr);
	Mat tmp(height, width, CV_8UC4);
	cvtColor(image, tmp, CV_YUV420sp2RGBA);
 
 
	double cam[] = { width, 0, width / 2, 0, height, height / 2, 0, 0, 1 };
	double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 };
 
 
	Mat camMat = Mat(3, 3, CV_64FC1, cam);
	Mat disMat = Mat(5, 1, CV_64FC1, distort);
	undistort(tmp, tmp, camMat, disMat);
 
 
	vector < Mat > channels;
	split(tmp, channels);
	Mat r = channels.at(0);
	Mat g = channels.at(1);
	Mat b = channels.at(2);
	Mat a = channels.at(3);
 
 
	vector < Mat > mbgr(4);
	mbgr[0] = b;
	mbgr[1] = g;
	mbgr[2] = r;
	mbgr[3] = a;
 
 
	merge(mbgr, rgba);
 
 
	jenv->ReleaseByteArrayElements(YuvNv21, pBuf, 0);
	//jenv->ReleaseIntArrayElements(pixels, poutPixels, 0);
 
 
	return true;
}
 
 
#ifdef __cplusplus
}
#endif

以上这篇利用OPENCV为android开发畸变校正的JNI库方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:


  • 本文相关:
  • android 通过jni返回mat数据类型方法
  • android将camera获取到的yuvdata在jni中转化为mat方法
  • android 跨进程通messenger(简单易懂)
  • gradle tool升级到3.0注意事项小结
  • android单元测试之对activity的测试示例
  • android recyclerview使用gridlayoutmanager间距设置的方法
  • edittext + listview 实现搜索listview中的内容方法(推荐)
  • android编程实现动态支持多语言的方法
  • android 完全退出应用程序实现代码
  • android bitmap详解及bitmap的内存优化
  • android如何判断当前点击位置是否在圆的内部
  • android实现可浏览和搜索的联系人列表
  • 免责声明 - 关于我们 - 联系我们 - 广告联系 - 友情链接 - 帮助中心 - 频道导航
    Copyright © 2017 www.zgxue.com All Rights Reserved