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:
- Read the bitmap
- Covert to greyscale
- 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 imageMat edges
– output edge map; single channels 8-bit image, which has the same size as imagedouble treshold1
– first threshold for the hysteresis proceduredobule 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!
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.
Popular posts
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 moreFears 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 moreWhat 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