Edge Detection
Detect and highlight edges in an image using various edge detection algorithms.
/api/image/edge-detection
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
file | File | Yes | The image file to process. Supported formats: JPEG, PNG, WebP, BMP, TIFF. |
Response
Returns the edge-detected image in the same format as the input file.
Example Request
// example.sh
curl -X POST "https://oyyi.xyz/api/image/edge-detection" -H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@image.jpg" \
-F "method=canny" \
-F "threshold1=100" \
-F "threshold2=200" \
--output edges.jpg
Example with Python
// edge_detection.py
import requests
url = "https://oyyi.xyz/api/image/edge-detection"
files = {
'file': open('image.jpg', 'rb')
}
data = {
'method': 'canny',
'threshold1': 100,
'threshold2': 200
}
response = requests.post(url, files=files, data=data)
# Save the edge-detected image
with open('edges.jpg', 'wb') as f:
f.write(response.content)
Example with JavaScript
// edge_detection.js
// Using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('method', 'canny');
formData.append('threshold1', '100');
formData.append('threshold2', '200');
fetch('https://oyyi.xyz/api/image/edge-detection', {
method: 'POST',
body: formData
})
.then(response => response.blob())
.then(blob => {
// Create a URL for the blob
const url = URL.createObjectURL(blob);
// Create a link to download the image
const a = document.createElement('a');
a.href = url;
a.download = 'edges.jpg';
a.click();
// Clean up by revoking the URL
URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
Edge Detection Methods
Canny
The most advanced edge detection algorithm. Uses a multi-stage process to detect a wide range of edges. Provides the cleanest and most precise results.
Sobel
Calculates the gradient of image intensity at each pixel. Emphasizes edges in horizontal and vertical directions. Good for detecting strong edges.
Prewitt
Similar to Sobel but uses a different kernel. Sometimes performs better on certain types of images. Provides a good balance between noise sensitivity and edge detection.
Laplacian
Calculates the second derivative of the image. Detects edges as zero-crossings. More sensitive to noise but can detect finer edges.
How It Works
Edge detection algorithms identify areas in an image where the brightness changes sharply, which typically correspond to object boundaries:
- Canny Edge Detection: A multi-stage algorithm that:
- Applies Gaussian blur to reduce noise
- Finds intensity gradients of the image
- Applies non-maximum suppression to thin edges
- Uses hysteresis thresholding with two thresholds to determine potential edges and connect them
- Sobel/Prewitt: These operators compute the gradient approximation of image intensity, highlighting areas of high spatial frequency (rapid intensity change).
- Laplacian: Computes the second derivative of the image, which highlights regions of rapid intensity change.
Canny Threshold Parameters
The Canny edge detector uses two thresholds for hysteresis:
- threshold1 (lower threshold): Edges with gradient magnitude below this value are rejected.
- threshold2 (upper threshold): Edges with gradient magnitude above this value are accepted.
- Edges between the thresholds: Accepted if they are connected to edges above the upper threshold.
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters, invalid method, invalid threshold values, or invalid file format. |
413 | Payload too large. The file size exceeds the maximum allowed limit. |
500 | Internal server error. Something went wrong on the server. |
Notes
- The Canny edge detector is generally the best choice for most applications due to its ability to detect true edges while minimizing noise.
- For the Canny method,
threshold1
andthreshold2
control the sensitivity of edge detection. Lower values detect more edges but may include noise. - The image is converted to grayscale before edge detection if it's not already grayscale.
- The maximum file size allowed is 10MB.
- Edge detection is often used as a preprocessing step for more complex image analysis tasks.
- The
invert
parameter can be useful depending on your application. By default, edges are white on a black background.
Use Cases
- Object detection and recognition
- Feature extraction for computer vision
- Creating artistic line drawings from photographs
- Medical image analysis
- Document scanning and text extraction
- Industrial quality control and defect detection
- Architectural and engineering drawing generation