OpenCV in advanced Android development: Edge detection

Android Development, Tutorials

In the last Android development tutorial we’ve cover light source detection using OpenCV. This time we’ll cover edge detection, which allows us to distinguish objects from background. This task is usually the starting point to more advanced content analysis (e.g. determining object features, quantities etc.).

Edge detection OpenCV

Again, OpenCV makes it easier by providing the Imgproc.Canny() method, which does all the work for us. The method itself, nomen omen, uses Canny’s algorithm to perform this task.


private void detectEdges(Bitmap bitmap) {
	Mat rgba = new Mat();
	Utils.bitmapToMat(bitmap, rgba);

	Mat edges = new Mat(rgba.size(), CvType.CV_8UC1);
	Imgproc.cvtColor(rgba, edges, Imgproc.COLOR_RGB2GRAY, 4);
	Imgproc.Canny(edges, edges, 80, 100);

	// Don't do that at home or work it's for visualization purpose.
	BitmapHelper.showBitmap(this, bitmap, imageView);
	Bitmap resultBitmap = Bitmap.createBitmap(edges.cols(), edges.rows(), Bitmap.Config.ARGB_8888);
	Utils.matToBitmap(edges, resultBitmap);
	BitmapHelper.showBitmap(this, resultBitmap, detectEdgesImageView);
}

The process is pretty straight-forward:

  1. Read the bitmap
  2. Covert to greyscale
  3. Apply Canny’s algorithm

As always, we start by create an array from the initial bitmap.


Mat rgba = new Mat();
Utils.bitmapToMat(bitmap, rgba);

Next make an array, in which all the calculation results will be saved, and convert the picture to greyscale. And at the end use the Imgproc.Canny() method to detect edges.


Mat edges = new Mat(rgba.size(), CvType.CV_8UC1);
Imgproc.cvtColor(rgba, edges, Imgproc.COLOR_RGB2GRAY, 4);
Imgproc.Canny(edges, edges, 80, 100);

The Imgproc.Canny() method takes the following parameters:

  • Mat image – 8-bit input image
  • Mat edges output edge map; single channels 8-bit image, which has the same size as image
  • double treshold1– first threshold for the hysteresis procedure
  • dobule treshold2– second threshold for the hysteresis procedure

Now we can display two pictures: the original one and the other that is the result of our Imgproc.Canny() method.


// Don't do that at home or work it's for visualization purpose.
BitmapHelper.showBitmap(this, bitmap, imageView);
Bitmap resultBitmap = Bitmap.createBitmap(edges.cols(), edges.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(edges, resultBitmap);
BitmapHelper.showBitmap(this, resultBitmap, detectEdgesImageView);

And that is it!

OpenCV in advanced Android development: Edge detection

OpenCV: Edge detection

As you can see, OpenCV can deliver great effects with minimum effort when building your mobile app. It’s a great and useful library, which provides loads of solutions out-of-the-box.


A code sample that we’ve used in a commercial Android application can be found on our GitHub page.


Slawomir Onyszko Mobile Developer

Sławek is our mobile developer who mostly takes care of creating Android applications. He is constantly enhancing his skills of advanced Android app development and he wants to share this knowledge via Zaven’s blog. In his spare time Sławek enjoys watching a good movie at the cinema.

Popular posts

From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.

From Hype to Hard Hats: Practical Use Cases for AI chatbots in Construction and Proptech.

Remember the multimedia craze in the early 2000s? It was everywhere, but did it truly revolutionize our lives? Probably not. Today, it feels like every piece of software is labeled "AI-powered." It's easy to dismiss AI chatbots in construction as just another tech fad.

Read more
Fears surrounding external support. How to address concerns about outsourcing software development?

Fears surrounding external support. How to address concerns about outsourcing software development?

Whether you’ve had bad experiences in the past or no experience at all, there will always be fears underlying your decision to outsource software development.

Read more
What do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget

What do you actually seek from external support? Identify what’s preventing you from completing a project on time and within budget

Let’s make it clear: if the capabilities are there, a project is best delivered internally. Sometimes, however, we are missing certain capabilities that are required to deliver said project in a realistic timeline. These may be related to skills (e.g. technical expertise, domain experience), budget (hiring locally is too expensive) or just capacity (not enough manpower). What are good reasons for outsourcing software development?

Read more
Mobile Apps

Get your mobile app in 3 easy steps!

1

Spec out

with the help of our
business analyst

2

Develop

design, implement
and test, repeat!

3

Publish

get your app out
to the stores


back to top