Android OpenCV Development (6) Image Processing (1)

Image processing using OpenCV (1)

For basic knowledge, you can review the articles I wrote before:
OpenCV import
OpenCV face detection
OpenCV vertical screen detection
OpenCV face recognition
OpenCV puppy recognition

This article will provide a look at how openCv processes images, and a code link is attached at the end of the article.
(1) Grayscale processing
(2) Corrosion operation
(3) Dilation operation
(4) Gaussian blur
(5) Median filtering
(6) Canny edge detection
(7) Blind watermarking

The renderings are as follows:
Insert image description here

Implementation process

Essentially, Android uses OpenCV for image processing, which means introducing the openCv library and then calling related classes for image processing. The following is the core code of all implementations:

(1) Grayscale processing

    public Bitmap toGray(Bitmap source) {
        Mat src = new Mat();
        Utils.bitmapToMat(source, src);
        Mat dst = new Mat();
        Imgproc.cvtColor(src, dst, Imgproc.COLOR_BGR2GRAY);
        Utils.matToBitmap(dst, source);
        src.release();
        dst.release();
        return source;
    }

(2) Corrosion operation

    public Bitmap toCorrosion(Bitmap bitmap, int width, int height) {
        //腐蚀
        Mat src = new Mat();
        Mat dst = new Mat();
        Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(width, height));
        Utils.bitmapToMat(bitmap, src);
        //src:源图像
        //dst:输出图像
        //element:这是我们将用来执行操作的内核。如果我们不指定,默认是一个简单的3x3矩阵。否则,我们可以指定它的形状。
        //为此,我们需要使用函数cv :: getStructuringElement:
        Imgproc.erode(src, dst, element);
        Utils.matToBitmap(dst, bitmap);
        src.release();
        dst.release();
        return bitmap;
    }

(3) Expansion operation

    public Bitmap toDilate(Bitmap bitmap, int width, int height) {
        //膨胀
        //src:源图像
        //dst:输出图像
        //element:这是我们将用来执行操作的内核。如果我们不指定,默认是一个简单的3x3矩阵。否则,我们可以指定它的形状。
        //为此,我们需要使用函数cv :: getStructuringElement:
        Mat src = new Mat();
        Mat dst = new Mat();
        Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(width, height));
        Utils.bitmapToMat(bitmap, src);
        Imgproc.dilate(src, dst, element);
        Utils.matToBitmap(dst, bitmap);
        src.release();
        dst.release();
        return bitmap;
    }

(4) Gaussian blur

    public Bitmap toGaussian(Bitmap bitmap) {
        //高斯模糊
        Mat src = new Mat();
        Mat ret = new Mat();
        Utils.bitmapToMat(bitmap, src);
        // src,输入图像,即源图像,填Mat类的对象即可。它可以是单独的任意通道数的图片,但需要注意,图片深度应该为CV_8U,CV_16U, CV_16S, CV_32F 以及 CV_64F之一。
        // dst,即目标图像,需要和源图片有一样的尺寸和类型。比如可以用Mat::Clone,以源图片为模板,来初始化得到如假包换的目标图。
        // ksize,高斯内核的大小。其中ksize.width和ksize.height可以不同,但他们都必须为正数和奇数(并不能理解)。或者,它们可以是零的,它们都是由sigma计算而来。
        // sigmaX,表示高斯核函数在X方向的的标准偏差。
        // sigmaY,表示高斯核函数在Y方向的的标准偏差。若sigmaY为零,就将它设为sigmaX,如果sigmaX和sigmaY都是0,那么就由ksize.width和ksize.height计算出来。
        Imgproc.GaussianBlur(src, ret, new Size(77, 77), 5, 5);
        Utils.matToBitmap(ret, bitmap);
        src.release();
        ret.release();
        return bitmap;
    }

(5) Median filtering

    public Bitmap toMedianBlur(Bitmap bitmap, int ksize) {
        //中值滤波
        Mat src = new Mat();
        Mat ret = new Mat();
        Utils.bitmapToMat(bitmap, src);
        // InputArray src: 输入图像,图像为1、3、4通道的图像,当模板尺寸为3或5时,图像深度只能为CV_8U、CV_16U、CV_32F中的一个,如而对于较大孔径尺寸的图片,图像深度只能是CV_8U。
        // OutputArray dst: 输出图像,尺寸和类型与输入图像一致,可以使用Mat::Clone以原图像为模板来初始化输出图像dst
        // int ksize: 滤波模板的尺寸大小,必须是大于1的奇数,如3、5、7……
        Imgproc.medianBlur(src, ret, ksize);
        Utils.matToBitmap(ret, bitmap);
        src.release();
        ret.release();
        return bitmap;
    }

(6) Canny edge detection

    public Bitmap CannyScan(Bitmap bitmap, int threshold1, int threshold2) {
        //边缘检测
        Mat src = new Mat();
        Utils.bitmapToMat(bitmap, src);
        Mat gray = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);//灰度处理
        Mat ret = src.clone();
        // image  输入图像,必须是CV_8U的单通道或者三通道图像。
        // edges  输出图像,与输入图像具有相同尺寸的单通道图像,且数据类型为CV_8U。
        // threshold1  第一个滞后阈值。
        // threshold2  第二个滞后阈值。
        Imgproc.Canny(src, ret, threshold1, threshold2);
        Utils.matToBitmap(ret, bitmap);
        src.release();
        gray.release();
        ret.release();
        return bitmap;
    }

(7) Blind watermark
Thanks for the idea.
The core is to perform discrete Fourier transform on the image, add text, and then convert it back.
Chinese is not currently supported

Project address
Branch dev
search: HomeActivity

that’s all--------------------------------------------------------------------------------------------------------------

Guess you like

Origin blog.csdn.net/motosheep/article/details/131677450