You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何将图像居中放入36×36零矩阵?

Center Your Binary Mask in a 36×36 Zero Matrix (Python)

Hey there! As someone new to Python image processing, I totally get how these small but specific operations can feel a bit overwhelming at first. Let's walk through exactly how to center your binary mask—whether it's smaller or larger than 36×36—into a 36×36 zero matrix, step by step.

The Core Approach

The logic is straightforward:

  • If your mask is smaller than 36×36: Create a blank 36×36 zero matrix, then paste your mask right in the middle, leaving zeros around it.
  • If your mask is larger than 36×36: Crop the central 36×36 portion of your mask (since that's the part we want to keep centered in our target size).

Implementation with NumPy

NumPy is perfect for this kind of matrix manipulation. Here's a reusable function that handles both cases:

import numpy as np

def center_mask_in_36x36(original_mask):
    # Grab the height and width of your original binary mask
    h, w = original_mask.shape[:2]
    # Create an empty 36x36 matrix filled with zeros, matching your mask's data type
    target_matrix = np.zeros((36, 36), dtype=original_mask.dtype)
    
    if h <= 36 and w <= 36:
        # Calculate where to place the top-left corner of your mask to center it
        y_start = (36 - h) // 2
        x_start = (36 - w) // 2
        # Paste the original mask into the zero matrix
        target_matrix[y_start:y_start+h, x_start:x_start+w] = original_mask
    else:
        # Calculate where to crop the original mask to get its central 36x36 area
        y_crop_start = (h - 36) // 2
        x_crop_start = (w - 36) // 2
        # Crop the center and assign it to our target matrix
        target_matrix = original_mask[y_crop_start:y_crop_start+36, x_crop_start:x_crop_start+36]
    
    return target_matrix

How to Use This Function

Let's test it with example masks to see how it works:

# Example 1: A small 20x20 binary mask
small_mask = np.random.randint(0, 2, (20, 20), dtype=np.uint8)
centered_small_mask = center_mask_in_36x36(small_mask)

# Example 2: A large 40x40 binary mask
large_mask = np.random.randint(0, 2, (40, 40), dtype=np.uint8)
centered_large_mask = center_mask_in_36x36(large_mask)

Working with PIL Images (If You Prefer)

If you're using PIL to load/save your masks, here's how to integrate the function:

from PIL import Image
import numpy as np

# Load your binary mask as a PIL Image (convert to grayscale for binary compatibility)
pil_mask = Image.open("your_mask_file.png").convert("L")
# Convert the PIL Image to a NumPy array
np_mask = np.array(pil_mask)
# Center the mask using our function
centered_mask_np = center_mask_in_36x36(np_mask)
# Convert back to a PIL Image to save or display
centered_mask_pil = Image.fromarray(centered_mask_np)
# Save the result
centered_mask_pil.save("centered_36x36_mask.png")

Quick Notes

  • We use integer division (//) to ensure whole-number positions—no floating-point errors here!
  • Matching the data type of the zero matrix to your original mask ensures consistency (e.g., if your mask uses uint8 for 0/255 values, the zero matrix will too).

内容的提问来源于stack exchange,提问作者Bruno Miguel Gonçalves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:07:56