How to Detect and Highlight Edges in an Image

Cloudmersive
2 min readSep 27, 2022

--

Edge detection refers to the process of detecting and highlighting objects with edges in an image by detecting gaps in the image’s brightness. This has limitless practical and aesthetic applications — and you can easily incorporate this operation as a service in your app using our Edge Highlighting & Detection API. When configuring this API, you just need to provide a radius (in pixels) for the edge detection operation along with your input image file path, and you’re good to go. This API can be used for free with a free-tier Cloudmersive API key (which you can get by registering a free account on our website), and you can structure your API call easily using the ready-to-run Java code examples provided below in this article.

Without further ado, I’ll demonstrate how we can install the API client & call the API. First things first: let’s add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next let’s add a reference to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now let’s add the imports to the top of our file:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.FilterApi;

And finally let’s call the function, first adding our API key & then configuring our parameters where shown by the code comments:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
FilterApi apiInstance = new FilterApi();
Integer radius = 56; // Integer | Radius in pixels of the edge detection operation; a larger radius will produce a greater effect
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
byte[] result = apiInstance.filterEdgeDetect(radius, imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling FilterApi#filterEdgeDetect");
e.printStackTrace();
}

After that, you’re all done — no more code required. Easy as pie!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet