Apply Threshold
Apply a threshold to an image to create a binary (black and white) image.
/api/image/threshold
Request Parameters
Parameter | Type | Required | Description |
---|---|---|---|
file | File | Yes | The image file to threshold. Supported formats: JPEG, PNG, WebP, BMP, TIFF. |
threshold_value | Integer | No | The threshold value. Pixels with intensity above this value become white, below become black. Range: 0-255. Default: 128 . |
Response
Returns the thresholded binary image in the same format as the input file.
Example Request
curl -X POST "https://oyyi.xyz/api/image/threshold" -H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@image.jpg" \
-F "threshold_value=128" \
--output threshold.jpg
Example with Python
import requests
url = "https://oyyi.xyz/api/image/threshold"
files = {
'file': open('image.jpg', 'rb')
}
data = {
'threshold_value': 128
}
response = requests.post(url, files=files, data=data)
# Save the thresholded image
with open('threshold.jpg', 'wb') as f:
f.write(response.content)
Example with JavaScript
// Using fetch API
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('threshold_value', '128');
fetch('https://oyyi.xyz/api/image/threshold', {
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 = 'threshold.jpg';
a.click();
// Clean up by revoking the URL
URL.revokeObjectURL(url);
})
.catch(error => console.error('Error:', error));
Thresholding Methods
Simple
Uses a global threshold value. All pixels above the threshold become white, all below become black.
Adaptive
Calculates thresholds for different regions of the image. Better for images with varying lighting conditions.
Otsu
Automatically determines the optimal threshold value based on the image histogram. Ignores the threshold_value parameter.
How It Works
Thresholding converts a grayscale image to a binary (black and white) image by setting each pixel to either black or white based on its intensity compared to a threshold value:
- Simple method: Uses a single global threshold value for the entire image.
- Adaptive method: Calculates different threshold values for different regions of the image based on local statistics. This is controlled by the
block_size
(size of the local region) andc
(adjustment constant) parameters. - Otsu method: Automatically calculates the optimal threshold value by analyzing the image histogram to minimize the variance between black and white pixels.
Threshold Value Examples
Low Value (0-85)
More pixels become white, good for dark images
Medium Value (86-170)
Balanced threshold, good starting point for most images
High Value (171-255)
More pixels become black, good for bright images
Error Responses
Status Code | Description |
---|---|
400 | Bad request. Missing required parameters, invalid threshold value, invalid method, 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
- For the simple method, a threshold value of 128 is a good starting point for most images.
- Lower threshold values will result in more white pixels, higher values in more black pixels.
- The adaptive method is better for images with uneven lighting or shadows.
- The Otsu method automatically calculates the optimal threshold and ignores the threshold_value parameter.
- The maximum file size allowed is 10MB.
- The image is converted to grayscale before thresholding if it's not already grayscale.
- For adaptive thresholding, the
block_size
must be an odd number. Larger values result in more global thresholding, smaller values in more local adaptation.
Use Cases
- Document scanning and text extraction
- Creating silhouettes or cutouts
- Simplifying complex images for analysis
- Creating high-contrast artistic effects
- Preparing images for edge detection or contour finding
- Isolating objects from backgrounds
- Barcode or QR code reading
- Medical image analysis