如何使用OpenCV(Java)为文档图像添加色彩效果
Hey there! Since you're already comfortable with those blur and edge detection filters in OpenCV, adding color effects to your document scans should be totally manageable. Let's break down some practical, document-friendly color adjustments you can implement right away:
Documents often look best in high-contrast black and white, which cuts down on noise and improves readability.
- Convert to grayscale: The foundation for most document color adjustments
import cv2 import numpy as np img = cv2.imread("your_document.jpg") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - Simple binary thresholding: Works for evenly lit documents
_, binary_img = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) - Adaptive thresholding: Way better for documents with uneven lighting/shadows
The Gaussian adaptive method calculates thresholds for small tile regions, so shadows don't wash out text.adaptive_binary = cv2.adaptiveThreshold( gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2 )
Want to mimic the look of old, yellowed papers? This filter adds a warm, brownish tint:
# Start with grayscale, convert back to BGR to apply color tweaks sepia = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) # Apply sepia color matrix sepia[:, :, 0] = sepia[:, :, 0] * 0.272 + sepia[:, :, 1] * 0.534 + sepia[:, :, 2] * 0.131 # Blue channel sepia[:, :, 1] = sepia[:, :, 0] * 0.349 + sepia[:, :, 1] * 0.686 + sepia[:, :, 2] * 0.168 # Green channel sepia[:, :, 2] = sepia[:, :, 0] * 0.393 + sepia[:, :, 1] * 0.769 + sepia[:, :, 2] * 0.189 # Red channel # Make sure values stay within 0-255 (uint8 range) sepia = np.clip(sepia, 0, 255).astype(np.uint8)
If your scan is faded or has low contrast, use these methods to make text pop:
- Global histogram equalization: Quick but can over-brighten light areas
equalized = cv2.equalizeHist(gray) - CLAHE (Contrast Limited Adaptive Histogram Equalization): Recommended for documents
It enhances contrast locally without blowing out bright regions:
Adjustclahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) enhanced_doc = clahe.apply(gray)clipLimit(higher = more contrast) andtileGridSizebased on your document's size.
Flip colors to turn white-background text into black-background—great for low-light reading:
# Invert full color image inverted_color = cv2.bitwise_not(img) # Invert grayscale image inverted_gray = cv2.bitwise_not(gray)
Tweak the BGR channels (OpenCV uses BGR by default) to add warm or cool tones:
- Warm tone: Boost red, reduce blue
warm_img = img.copy() warm_img[:, :, 0] = warm_img[:, :, 0] * 0.8 # Dim blue channel warm_img[:, :, 2] = warm_img[:, :, 2] * 1.2 # Brighten red channel warm_img = np.clip(warm_img, 0, 255).astype(np.uint8) - Cool tone: Boost blue, reduce red
cool_img = img.copy() cool_img[:, :, 0] = cool_img[:, :, 0] * 1.2 # Brighten blue channel cool_img[:, :, 2] = cool_img[:, :, 2] * 0.8 # Dim red channel cool_img = np.clip(cool_img, 0, 255).astype(np.uint8)
Quick Tip
Remember OpenCV reads images in BGR format, not RGB. If you're using matplotlib to display results, convert it first with cv2.cvtColor(img, cv2.COLOR_BGR2RGB)—otherwise colors will look off!
内容的提问来源于stack exchange,提问作者EAS

