First OpenCV Android Project — Simple Image Filter in Android

First OpenCV Android Project — Simple Image Filter in Android

Hello Android enthusiasts! 🤗 Are you ready to explore the fascinating world of computer vision in your Android apps? If so, let’s dive into our first OpenCV Android project and learn how to apply simple image filter! 🌟

🖌️ The Magic of OpenCV

OpenCV (Open Source Computer Vision Library) is a powerful open-source library that allows developers to perform computer vision tasks, including image processing, object detection, and more. It provides a wide range of tools and algorithms that can be leveraged to create stunning visual experiences in Android applications. 🌈

Checkout this blog for Step-By-Step installation of OpenCV in Android Studio —https://medium.com/@fierydinesh/android-studio-step-by-step-guide-to-download-and-install-opencv-for-android-9ddcb78a8bc3

🔍 The Quest for Image Filtering

Our quest today is to apply image filter to enhance and modify images in exciting ways. We’ll transform color images into grayscale and adjust brightness. This will be the foundation of crafting captivating visual effects in your apps. 🧙‍♂️

🧐 Understanding the Code

Now, let’s delve into the code that performs the image filtering magic! The code snippet below demonstrates how to apply filters to an image and display the filtered result in an ImageView:

// Step 1: Create a Mat object to hold the image data
val imageMat = Mat()

// Step 2: Convert the input imageBitmap to a Mat object (OpenCV format)
Utils.bitmapToMat(imageBitmap, imageMat)

// Step 3: Create a new Mat object named grayMat to hold the grayscale version of the image
val grayMat = Mat()

Mat is a multi-dimensional array that represents an image in OpenCV. It holds pixel values, color information, and other image-related data. It allows developers to manipulate and process images efficiently, making it an essential component in computer vision tasks.

// Step 4: Convert the color image (BGR) to grayscale
Imgproc.cvtColor(imageMat, grayMat, Imgproc.COLOR_BGR2GRAY)

Imgproc.cvtColor is a function from the OpenCV library used to convert the color space of an image. In the code snippet, it is used to convert the original color image (imageMat) to grayscale (grayMat)

// Step 5: Adjust the brightness of the grayscale image (optional)
Core.add(grayMat, Scalar.all(-30.0), grayMat)

// Step 6: Create a new Bitmap named bwBitmap to hold the final filtered image
val bwBitmap = Bitmap.createBitmap(grayMat.cols(), grayMat.rows(), Bitmap.Config.RGB_565)

// Step 7: Convert the filtered grayscale Mat back to a Bitmap format
Utils.matToBitmap(grayMat, bwBitmap)

// Step 8: Set the filtered Bitmap (bwBitmap) to be displayed in the ImageView
imageView.setImageBitmap(bwBitmap)

Core.add is a function from the OpenCV library used to perform element-wise addition on two Mat objects. In the code snippet, it is used to adjust the brightness of the grayscale image (grayMat)

In OpenCV, Scalar is a class that represents a multi-channel scalar value. It is commonly used to represent colors in images, where each channel (e.g., Red, Green, Blue) is represented by a numerical value.

Here’s the syntax of Scalar.all():

fun Scalar.all(v: Double): Scalar

The v parameter is the value that will be assigned to all the channels of the Scalar object. It can be of type Double, meaning you can use decimal values for colors.

For example, if you want to create a Scalar object representing the color red, you can use Scalar.all() like this:

val redColor = Scalar.all(255.0) // Assuming the color channels are in the range of 0 to 255

In this case, redColor will be a Scalar object with three channels (assuming BGR color space), and each channel will have a value of 255, representing full intensity of red, and no intensity of green and blue.

Output Video :

Github Project Link :

GitHub - wh173d3v11/OpenCVSamples: Simple Image Filter using Opencv library.
_Simple Image Filter using Opencv library. Contribute to wh173d3v11/OpenCVSamples development by creating an account on…_github.com

🌟 Pros and Cons of Using OpenCV in Android

Now that we’ve explored the magic of OpenCV and image filtering, let’s take a closer look at the advantages and challenges of using OpenCV in Android development:

Pros:

  1. Rich Functionality: OpenCV provides an extensive set of algorithms and functions for computer vision tasks, allowing you to perform complex image processing and analysis.
  2. Cross-Platform Support: OpenCV is cross-platform, making it compatible with various operating systems, including Android, iOS, Windows, and Linux.
  3. Open-Source and Free: OpenCV is open-source, meaning it’s free to use and is actively maintained and improved by a large community of developers.
  4. Community Support: With a vast community of developers and enthusiasts, you’ll find ample resources, tutorials, and answers to your questions on forums like Stack Overflow.
  5. Real-Time Processing: OpenCV is optimized for real-time image processing, making it suitable for applications that require quick and efficient analysis.

Cons:

  1. Integration Complexity: Setting up OpenCV in an Android project may be a bit challenging for beginners, as it involves downloading and configuring the library.
  2. Learning Curve: Mastering OpenCV and its numerous functions may require time and effort, especially for developers new to computer vision.
  3. Performance Considerations: Some advanced computer vision tasks may demand substantial processing power and could impact app performance on lower-end devices.
  4. Large Library Size: OpenCV’s library size is relatively large, which may increase the size of your APK.

🚀 Conclusion

Congratulations on completing your first OpenCV Android project! You’ve taken your first steps into the enchanting world of computer vision and image filtering. Embrace this knowledge and keep honing your skills — there are endless possibilities to create magical experiences for your users! 🌌✨

By leveraging the power of OpenCV, you can create captivating image filters and unlock a world of possibilities in your Android apps. The pros outweigh the cons, making OpenCV an excellent addition to your development toolkit.

Happy coding, Android visionaries! 😄👩‍💻👨‍💻