Commonly used image operations in OpenCv Android

In the project, OpenCv Android SDK (version 4.5.4) is used for image processing. The supported operations are complete and the effect is good. Some of the image operation methods are recorded as follows:
  • Mat and Bitmap

Mat is a memory object used to store image information in OpenCv. It also includes image width, height, type, dimension, size, depth and other information.

int width = srcMat.cols();
int height = srcMat.rows();
int dims = srcMat.dims(); //维度
int channels = srcMat.channels();
int depth = srcMat.depth();
int type = srcMat.type();

Bitmap is Android's color space on RGB, similar to Mat object.

//一顿Mat操作后可转换到Android Bitmap对象
Bitmap dstBitmap = Bitmap.createBitmap(dstMat.width(), dstMat.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dstMat, dstBitmap);
  • load local image
String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "test.png";
//本地载入
Mat srcMat = Imgcodecs.imread(path);
//Android Resource载入
Mat srcMat = Utils.loadResource(context, R.drawable.test, Imgcodecs.IMREAD_COLOR);          
  • image scaling
double ratio = (input_shape * 1.0) / Math.max(imgWH[0], imgWH[1]); //[h,w]数组
Size newSize = new Size(Math.round(imgWH[1] * ratio), Math.round(imgWH[0] * ratio));
//缩放成新的大小
Imgproc.resize(srcMat, dstMat, newSize);
  • Image Flip and Mirror
//根据第三个参数flipCode可实现图像的垂直、水平以及同时垂直镜像翻转
Core.flip(dstMat, dstMat, 1); //水平翻转镜像,resize后再处理翻转较快
  • image cropping
Rect rect = new Rect((int) (rectF.left), (int) (rectF.top), (int) (rectF.right - rectF.left), (int) (rectF.bottom - rectF.top));
//根据Rect区域裁剪生成新的Mat对象
Mat cropMat = new Mat(bgrMat, rect);
  • Image Boundary Extension
Scalar padColor = new Scalar(0, 0, 0);
//填充类型borderType参数,CONSTANT代表用固定像素值填充
Core.copyMakeBorder(dstMat, dstMat, 0, dh, 0, dw, Core.BORDER_CONSTANT, padColor);
  • Color format conversion
//获取YUV NV21格式数据
Mat yuvMat = new Mat(imgWH[0] * 3 / 2, imgWH[1], CvType.CV_8UC1);
yuvMat.put(0, 0, yuvData);
Mat bgrMat = new Mat();
Imgproc.cvtColor(yuvMat, bgrMat, Imgproc.COLOR_YUV2BGR_NV21);

//本地默认读取的是BGR格式图片,最后转换成RGB格式
Imgproc.cvtColor(dstMat, dstMat, Imgproc.COLOR_BGR2RGB);

//生成灰度图
Imgproc.cvtColor(srcMat, grayMat, Imgproc.COLOR_BGR2GRAY);
  • channel separation
//从Mat分离出每个像素对应的R,G.B三个通道值进行像素归一化处理
ArrayList<Mat> mats = new ArrayList<>(dstMat.channels());
Core.split(dstMat, mats); //通道分离
outImage.rewind();
for (int i = 0; i < mats.size(); i++) {
	Mat channelMat = mats.get(i);
    byte[] data = new byte[channelMat.width() * channelMat.height() * channelMat.channels()];
    channelMat.get(0, 0, data);
    int pixelValue;
    for (int j = 0; j < data.length; j++) {
		pixelValue = data[j] & 0xff;
        outImage.putFloat(pixelValue / 255.0f);
	}
	channelMat.release();
}
  • Image pixel value statistics
//截取ROI区域内的所有像素进行均值和标准差计算
Mat rangeMat = new Mat(dstMat, new Range(0, 8), new Range(0, 8));
MatOfDouble means = new MatOfDouble();
MatOfDouble stddevs = new MatOfDouble();
Core.meanStdDev(rangeMat, means, stddevs);
  • Pixel normalization
    limits the processed pixel values ​​within a certain range to facilitate subsequent data processing.
//线性归一化到(0, 1)区间
Core.normalize(dstMat, dstMat, 0, 1, Core.NORM_MINMAX);
  • read video stream
VideoCapture capture = new VideoCapture();
String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "output.mp4";
capture.open(path);
while (capture.isOpened()) {
	Mat frameMat = new Mat();
	boolean ret = capture.read(frameMat);
	if (ret) {
		//做业务处理
	}
}

Guess you like

Origin blog.csdn.net/qq_23069607/article/details/124497319