Advanced Image Manipulation in Android using OpenCV

Prabin Bhusal
Bajra Technologies Blog
6 min readMar 7, 2023

--

Image manipulation is an essential part of modern mobile application development. With the help of the OpenCV library, it has become easier to manipulate images in Android Kotlin. OpenCV is a computer vision library that provides a wide range of image processing and analysis tools. In this blog post, we will explore advanced image manipulation techniques in Android Kotlin using the OpenCV library, including filters, noise reduction, rotation, and more.

To get started, we need to add the OpenCV library to our Android Kotlin project. First, download the OpenCV SDK for Android from here.

  1. Open your OpenCV project in Android Studio, click File in the top menu, then select New and import Module. Browse to the /sdk directory of your OpenCV download and select it. Choose a meaningful name for your module, then wait for the import procedure to complete.
  2. Click on File in the top menu and select Project Structure. In the left-hand menu, select Dependencies. Click on the app module and the + icon in the Declared Dependencies tab. Choose Module Dependency and select the checkbox for the OpenCV SDK module that you imported earlier. Click on Apply and OK to exit the Project Structure.
  3. Check compileSdkVersion, minSdkVersion, and targetSdkVersion inside your openCV library app module build.gradle file to ensure it matches with your app build.gradle file.
  4. Finally, sync your project with the Gradle files. After syncing, add the following code snippet to your MainActivity class to check if OpenCV has loaded successfully:
if (OpenCVLoader.initDebug()) {
Log.d("openCV", "OpenCV loaded successfully")
}

This will add the OpenCV library to our project, and we can now use its functions to manipulate images. Let’s explore some advanced image manipulation techniques that we can implement using OpenCV in Android Kotlin.

Filters

Filters are one of the most common image manipulation techniques, and they can be used to enhance or modify the appearance of an image. With OpenCV, we can apply various filters to an image, including grayscale, blur, and edge detection. Let’s take a look at ways we can apply these filters to an image using OpenCV in Android Kotlin.

First, we need to load an image into our application. We can do this by using the following code:

val bitmap = BitmapFactory.decodeResource(resources, R.drawable.image)
val srcMat = Mat()
Utils.bitmapToMat(bitmap, srcMat)

This code loads the image into a bitmap. Then it converts it to a Mat object, a matrix representation of the image that can be processed using OpenCV.

Next, let’s apply some filters to the image.

  • Grayscale filter

To convert an image to grayscale, we can use the `cvtColor` method of the `Imgproc` class. This filter removes the color information from the image, resulting in a black-and-white image.

The code to apply a grayscale filter to an image is as follows:

Imgproc.cvtColor(srcMat, srcMat, Imgproc.COLOR_BGR2GRAY)

Here, `srcMat` is the source image matrix. The second argument, also `srcMat`, is the output matrix that will contain the converted grayscale image. `Imgproc.COLOR_BGR2GRAY` is the color conversion code that specifies the type of conversion to be performed.

After applying this filter, the resulting image will have a single channel with pixel values ranging from 0 to 255. Here, 0 represents black, and 255 represents white.

  • Blur filter

To apply a blur filter to an image, we can use the `GaussianBlur` method of the `Imgproc` class. The following code snippet demonstrates how to apply a Gaussian blur to an image:

Imgproc.GaussianBlur(srcMat, srcMat, Size(15.0, 15.0), 0.0)

This code applies a Gaussian blur to the `srcMat` image, with a kernel size of 15x15 pixels and a standard deviation of 0.0. The Gaussian blur filter smooths out the edges and reduces noise in the image, resulting in a softer appearance.

  • Edge Detection filter

To apply an edge detection filter to an image, we can use the Canny edge detection algorithm, which is implemented using the Canny method of the `Imgproc` class.

Imgproc.Canny(srcMat, srcMat, 100.0, 200.0)

The above code applies the Canny edge detection algorithm to the image, highlighting the edges and making them more visible. The first argument, `srcMat`, is the source image matrix, also the destination matrix where the result will be stored. The second and third arguments are the threshold values for the algorithm, which can be adjusted to control the strength of the edge detection.

Noise Reduction

Noise reduction is another important image manipulation technique that can be used to improve the quality of an image. With OpenCV, we can apply various noise reduction algorithms to an image, including median filtering and bilateral filtering.

Let’s take a look at how we can apply these noise reduction techniques to an image using OpenCV in Android Kotlin.

First, we need to load an image into our application. We can do this using the same code as before:

val bitmap = BitmapFactory.decodeResource(resources, R.drawable.image)
val srcMat = Mat()
Utils.bitmapToMat(bitmap, srcMat)
  • Gaussian Filter

The Gaussian filter is a commonly used filter for image smoothing or blurring. The filter works by convolving the image with a Gaussian kernel, which is a function that weights nearby pixels more heavily than distant pixels. This results in a blur effect that reduces the high-frequency components of the image.

The Gaussian filter can be applied to an image in OpenCV using the `Imgproc.GaussianBlur` method. The method takes the input image as the first parameter, the output image as the second parameter, the size of the Gaussian kernel as the third parameter, and the standard deviation of the Gaussian distribution as the fourth parameter.

Here’s an example code for applying Gaussian filter:

Imgproc.GaussianBlur(srcMat, srcMat, Size(5, 5), 0.0)

In this example, the input image is `srcMat`, and the output image is also `srcMat`. The size of the Gaussian kernel is 5x5, and the standard deviation of the Gaussian distribution is set to 0.0.

  • Median Filter

The Median filter is a non-linear filter used for image smoothing that replaces each pixel value with the median value of the neighboring pixels within a kernel. This is useful for removing an image’s salt-and-pepper noise or other impulsive noise.

The Median filter can be applied to an image in OpenCV using the `Imgproc.medianBlur` method. The method takes the input image as the first parameter, the output image as the second parameter, and the size of the kernel as the third parameter.

Here’s an example code for applying the Median filter:

Imgproc.medianBlur(srcMat, srcMat, 5)

In this example, the input image is `srcMat`, the output image is also `srcMat`, and the size of the kernel is 5x5.

  • Bilateral Filter

The Bilateral filter is a non-linear filter used for image smoothing that preserves edges while reducing noise. The filter works by applying a Gaussian filter to both the spatial and intensity domains of the image. This allows the filter to smooth the image while preserving edges and other high-frequency features.

The Bilateral filter can be applied to an image in OpenCV using the `Imgproc.bilateralFilter` method. The method takes the input image as the first parameter, the output image as the second parameter, the diameter of the pixel neighborhood as the third parameter, and the sigma values for color and space as the fourth and fifth parameters respectively.

Here’s an example code for applying Bilateral filter:

Imgproc.bilateralFilter(srcMat, srcMat, 15, 75, 75)

In this example, the input image is `srcMat`, the output image is also `srcMat`, the diameter of the pixel neighborhood is 15, and the sigma values for color and space are both set to 75.

Rotation

Rotation is another common image manipulation technique that can be used to adjust the orientation of an image. With OpenCV, you can rotate an image by a specified angle using the following code:

val center = Point(srcMat.cols() / 2.0, srcMat.rows() / 2.0)
val rotationMatrix = Imgproc.getRotationMatrix2D(center, 45.0, 1.0)
Imgproc.warpAffine(srcMat, srcMat, rotationMatrix, srcMat.size())

This code rotates the image by 45 degrees around its center point. The first parameter of the getRotationMatrix2D method is the image’s center point, the second parameter is the rotation angle in degrees, and the third is the scaling factor. The `warpAffine` method applies rotation to the image.

Here, we have explored some advanced image manipulation techniques in Android Kotlin using the OpenCV library. Through these techniques, we can create powerful and engaging image-processing applications that provide users with a rich and engaging experience.

--

--