提取LBP直方图时部分图像bin数异常(预期59实际58)的原因咨询
Hey there! Let's dig into why you're seeing occasional 58-bin histograms instead of the expected 59 when using the nri-uniform LBP.
First, let's recap the basics: for non-rotation-invariant uniform LBP with 8 sampling points (your points=8 setting), the total number of possible bins is 59 — that's calculated as P*(P-1)+3 where P=8 (8*7+3=59). These bins range from 0 to 58.
Now looking at your code, the key issue is in how you're calculating n_bins in the lbp_feature_correct function:
n_bins = int(lbp.max() + 1)
Here's the problem: if a particular ROI's LBP values never hit the maximum possible value (58), then lbp.max() will be 57, making n_bins=58. When you pass this to np.histogram, it only creates bins up to 57, resulting in a 58-element histogram instead of 59.
Fixes you can try:
- Hardcode the expected bin count: Since you're using fixed LBP parameters (
points=8,method="nri_uniform"), you can directly setn_bins=59instead of calculating it dynamically. This ensures every histogram has the full 59 bins, even if some bins have a count of 0.# Replace the n_bins line with this n_bins = 59 (hist, bins) = np.histogram(lbp.ravel(), density=True, bins=n_bins, range=(0, n_bins)) - Calculate bin count programmatically: If you ever change your LBP parameters later, you can compute the correct bin count based on the method:
if lbp_params["method"] == "nri_uniform": n_bins = lbp_params["points"] * (lbp_params["points"] - 1) + 3 elif lbp_params["method"] == "uniform": n_bins = lbp_params["points"] + 2 else: # For default/ror method, bins are 2^points n_bins = 2 ** lbp_params["points"]
Other things to check:
- Make sure your ROI cropping isn't resulting in an empty or tiny region (though your fixed coordinates seem consistent, it's worth verifying if any problematic images have unexpected dimensions after resizing).
- Double-check that
cv2.imreadis successfully loading every image (a missing image might lead to unexpected LBP values, but you said most work so this is less likely).
This should resolve the inconsistent bin count issue!
备注:内容来源于stack exchange,提问作者thaidy_04

