2023年6月21日发(作者:)
图像处理(⼆)opencv处理图像⼆值化,灰度化等这⾥主要实现的 opencv 基于 android 对图像进⾏常⽤处理,⽐如说灰度化,⼆值化,rgb的转换,这⾥就不贴主要代码,只是⼯具代码。⽅法的使⽤需要在中添加此⽅法(好像是掉⽤opencv,android⼿机⾸次使⽤需要下载)://OpenCV库加载并初始化成功后的回调函数
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
// TODO Auto-generated method stub
switch (status){
case S:
Log.i(TAG, "成功加载");
break;
default:
gerConnected(status);
Log.i(TAG, "加载失败");
break;
}
}
};
@Override
protected void onResume() {
// TODO Auto-generated method stub
me();
//load OpenCV engine and init OpenCV library
ync(_VERSION_2_4_4, getApplicationContext(), mLoaderCallback);
Log.i(TAG, "onResume sucess ");
// new Handler().postDelayed(new Runnable(){
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// procSrc2Gray();
// }
//
// }, 1000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;
}
Utils(处理⽅法)//灰度化 public Bitmap procSrc2Gray(Bitmap bm){ Mat rgbMat = new Mat();
Mat grayMat = new Mat();
Bitmap graybm = Bitmap(th(), ght(), _8888); //创建图像 ToMat(bm, rgbMat);//bitmap转RGB,常⽤ or(rgbMat, grayMat, _RGB2GRAY);//rgbMat to gray grayMat itmap(grayMat, graybm); return graybm; }//(额外附上bitmap的处理⽅法)灰度化//(额外附上bitmap的处理⽅法)灰度化 public Bitmap bitmap2Gray(Bitmap bmSrc) {
// 得到图⽚的长和宽
int width = th();
int height = ght();
// 创建⽬标灰度图像
Bitmap bmpGray = null;
bmpGray = Bitmap(width, height, _565);
// 创建画布
Canvas c = new Canvas(bmpGray);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
uration(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
orFilter(f);
tmap(bmSrc, 0, 0, paint);
return bmpGray;
}
//旋转 public Bitmap rotaingImageView(int angle , Bitmap bitmap) {
//旋转图⽚ 动作
Matrix matrix = new Matrix(); tate(angle); // 创建新的图⽚
Bitmap bm = Bitmap(bitmap, 0, 0, th(), ght(), matrix, true);
return bm; } //RGB转换
public Bitmap transparentImage(Bitmap bm){ Mat rgbMat = new Mat();
Mat grayMat = new Mat(); double[] data = {250,250,250,250};//data即颜⾊ Bitmap graybm = Bitmap(th(), ght(), _8888);
ToMat(bm, rgbMat); for(int i=0;i<();i++) { for(int j=0;j<();j++) { (i, j, data); } } itmap(rgbMat, graybm); return graybm; }//(额外附上bitmap⽅法)RGB转换 public Bitmap transparentImage(Bitmap bmp) {
/* pixels 接收位图颜⾊值的数组
offset 写⼊到pixels[]中的第⼀个像素索引值
stride pixels[]中的⾏间距个数值(必须⼤于等于位图宽度)。可以为负数
x 从位图中读取的第⼀个像素的x坐标值。
y 从位图中读取的第⼀个像素的y坐标值
width 从每⼀⾏中读取的像素宽度
height 读取的⾏数
*/
int m_ImageWidth, m_ImageHeigth;
m_ImageWidth = th();
m_ImageHeigth = ght();
m_BmpPixel = new int[m_ImageWidth * m_ImageHeigth];
els(m_BmpPixel, 0, m_ImageWidth, 0, 0, m_ImageWidth,
m_ImageHeigth);
n("AAAAAAAAAAAA2");
for (int i = 0; i < m_ImageWidth * m_ImageHeigth; i++) {
if (m_BmpPixel[i] == ) {
m_BmpPixel[i] = ;
} }
}
n("AAAAAAAAAAAA3");
Bitmap pro = Bitmap(m_ImageWidth,m_ImageHeigth, _8888); els(m_BmpPixel, 0, m_ImageWidth, 0, 0, m_ImageWidth,
m_ImageHeigth);
n("AAAAAAAAAAAA4");
return pro;
}//⼆值化
/* 名称 常量 ⼆值阈值化 _BINARY 阈值化到零 _TOZERO
截断阈值化 _TRUNC 反转⼆值阈值化 _BINARY_INV 反转阈值化到零 _TOZERO_INV
*/ public Bitmap changeBitmap(Bitmap bm) { Mat rgbMat = new Mat();
Mat grayMat = new Mat();
ToMat(bm,rgbMat); old(rgbMat,grayMat,100,255,_BINARY); itmap(grayMat,bm); return bm; }
//(额外附上bitmap⽅法)⼆值化 public Bitmap gray2Binary(Bitmap graymap) {
//得到图形的宽度和长度
int width = th();
int height = ght();
//创建⼆值化图像
Bitmap binarymap = null;
binarymap = (_8888, true);
//依次循环,对图像的像素进⾏处理
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
//得到当前像素的值
int col = el(i, j);
//得到alpha通道的值
int alpha = col & 0xFF000000;
//得到图像的像素RGB的值
int red = (col & 0x00FF0000) >> 16;
int green = (col & 0x0000FF00) >> 8;
int blue = (col & 0x000000FF);
// ⽤公式X = 0.3×R+0.59×G+0.11×B计算出X代替原来的RGB
int gray = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
//对图像进⾏⼆值化处理
if (gray <= 95) {
gray = 0;
} else {
gray = 255;
}
// 新的ARGB
int newColor = alpha | (gray << 16) | (gray << 8) | gray;
//设置新图像的当前像素值
el(i, j, newColor);
}
}
return binarymap;
}
2023年6月21日发(作者:)
图像处理(⼆)opencv处理图像⼆值化,灰度化等这⾥主要实现的 opencv 基于 android 对图像进⾏常⽤处理,⽐如说灰度化,⼆值化,rgb的转换,这⾥就不贴主要代码,只是⼯具代码。⽅法的使⽤需要在中添加此⽅法(好像是掉⽤opencv,android⼿机⾸次使⽤需要下载)://OpenCV库加载并初始化成功后的回调函数
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
// TODO Auto-generated method stub
switch (status){
case S:
Log.i(TAG, "成功加载");
break;
default:
gerConnected(status);
Log.i(TAG, "加载失败");
break;
}
}
};
@Override
protected void onResume() {
// TODO Auto-generated method stub
me();
//load OpenCV engine and init OpenCV library
ync(_VERSION_2_4_4, getApplicationContext(), mLoaderCallback);
Log.i(TAG, "onResume sucess ");
// new Handler().postDelayed(new Runnable(){
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// procSrc2Gray();
// }
//
// }, 1000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;
}
Utils(处理⽅法)//灰度化 public Bitmap procSrc2Gray(Bitmap bm){ Mat rgbMat = new Mat();
Mat grayMat = new Mat();
Bitmap graybm = Bitmap(th(), ght(), _8888); //创建图像 ToMat(bm, rgbMat);//bitmap转RGB,常⽤ or(rgbMat, grayMat, _RGB2GRAY);//rgbMat to gray grayMat itmap(grayMat, graybm); return graybm; }//(额外附上bitmap的处理⽅法)灰度化//(额外附上bitmap的处理⽅法)灰度化 public Bitmap bitmap2Gray(Bitmap bmSrc) {
// 得到图⽚的长和宽
int width = th();
int height = ght();
// 创建⽬标灰度图像
Bitmap bmpGray = null;
bmpGray = Bitmap(width, height, _565);
// 创建画布
Canvas c = new Canvas(bmpGray);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
uration(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
orFilter(f);
tmap(bmSrc, 0, 0, paint);
return bmpGray;
}
//旋转 public Bitmap rotaingImageView(int angle , Bitmap bitmap) {
//旋转图⽚ 动作
Matrix matrix = new Matrix(); tate(angle); // 创建新的图⽚
Bitmap bm = Bitmap(bitmap, 0, 0, th(), ght(), matrix, true);
return bm; } //RGB转换
public Bitmap transparentImage(Bitmap bm){ Mat rgbMat = new Mat();
Mat grayMat = new Mat(); double[] data = {250,250,250,250};//data即颜⾊ Bitmap graybm = Bitmap(th(), ght(), _8888);
ToMat(bm, rgbMat); for(int i=0;i<();i++) { for(int j=0;j<();j++) { (i, j, data); } } itmap(rgbMat, graybm); return graybm; }//(额外附上bitmap⽅法)RGB转换 public Bitmap transparentImage(Bitmap bmp) {
/* pixels 接收位图颜⾊值的数组
offset 写⼊到pixels[]中的第⼀个像素索引值
stride pixels[]中的⾏间距个数值(必须⼤于等于位图宽度)。可以为负数
x 从位图中读取的第⼀个像素的x坐标值。
y 从位图中读取的第⼀个像素的y坐标值
width 从每⼀⾏中读取的像素宽度
height 读取的⾏数
*/
int m_ImageWidth, m_ImageHeigth;
m_ImageWidth = th();
m_ImageHeigth = ght();
m_BmpPixel = new int[m_ImageWidth * m_ImageHeigth];
els(m_BmpPixel, 0, m_ImageWidth, 0, 0, m_ImageWidth,
m_ImageHeigth);
n("AAAAAAAAAAAA2");
for (int i = 0; i < m_ImageWidth * m_ImageHeigth; i++) {
if (m_BmpPixel[i] == ) {
m_BmpPixel[i] = ;
} }
}
n("AAAAAAAAAAAA3");
Bitmap pro = Bitmap(m_ImageWidth,m_ImageHeigth, _8888); els(m_BmpPixel, 0, m_ImageWidth, 0, 0, m_ImageWidth,
m_ImageHeigth);
n("AAAAAAAAAAAA4");
return pro;
}//⼆值化
/* 名称 常量 ⼆值阈值化 _BINARY 阈值化到零 _TOZERO
截断阈值化 _TRUNC 反转⼆值阈值化 _BINARY_INV 反转阈值化到零 _TOZERO_INV
*/ public Bitmap changeBitmap(Bitmap bm) { Mat rgbMat = new Mat();
Mat grayMat = new Mat();
ToMat(bm,rgbMat); old(rgbMat,grayMat,100,255,_BINARY); itmap(grayMat,bm); return bm; }
//(额外附上bitmap⽅法)⼆值化 public Bitmap gray2Binary(Bitmap graymap) {
//得到图形的宽度和长度
int width = th();
int height = ght();
//创建⼆值化图像
Bitmap binarymap = null;
binarymap = (_8888, true);
//依次循环,对图像的像素进⾏处理
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
//得到当前像素的值
int col = el(i, j);
//得到alpha通道的值
int alpha = col & 0xFF000000;
//得到图像的像素RGB的值
int red = (col & 0x00FF0000) >> 16;
int green = (col & 0x0000FF00) >> 8;
int blue = (col & 0x000000FF);
// ⽤公式X = 0.3×R+0.59×G+0.11×B计算出X代替原来的RGB
int gray = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
//对图像进⾏⼆值化处理
if (gray <= 95) {
gray = 0;
} else {
gray = 255;
}
// 新的ARGB
int newColor = alpha | (gray << 16) | (gray << 8) | gray;
//设置新图像的当前像素值
el(i, j, newColor);
}
}
return binarymap;
}
发布评论