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

Python实现基于选定矩形边界框的图像区域放大功能技术问询

Hey there! I’ve got you covered on this—adding a bounding box to your image and then cropping/zooming that specific region is totally straightforward with either OpenCV (which you’re already using) or Matplotlib. Let’s break down both approaches with clear, working code examples.

Using OpenCV (Your Current Tool)

Since you’re already reading images with OpenCV, let’s start here. This example walks through every step from drawing the box to showing the zoomed crop:

import cv2

# 1. Read your image (OpenCV defaults to BGR color format)
img = cv2.imread('your_image_path.jpg')

# 2. Define your bounding box coordinates
# (x1, y1) = top-left corner; (x2, y2) = bottom-right corner
x1, y1 = 100, 150
x2, y2 = 300, 350

# 3. Draw the red bounding box on the original image
# OpenCV uses BGR, so red is (0, 0, 255); last argument is line thickness
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)

# 4. Crop the region inside the bounding box
# Remember: OpenCV images are indexed as [y_start:y_end, x_start:x_end]
img_cropped = img[y1:y2, x1:x2]

# 5. Zoom the cropped region (e.g., scale it 2x larger)
scale_factor = 2
img_zoomed = cv2.resize(img_cropped, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_LINEAR)

# 6. Display both images
cv2.imshow('Original (with Bounding Box)', img)
cv2.imshow('Zoomed Cropped Area', img_zoomed)

# Wait for a key press to close windows
cv2.waitKey(0)
cv2.destroyAllWindows()

A quick heads-up: OpenCV uses BGR instead of the more common RGB format. If you ever need to switch to Matplotlib for display, just convert it with cv2.cvtColor(img, cv2.COLOR_BGR2RGB).

Using Matplotlib (Great for Side-by-Side Displays)

If you prefer Matplotlib for plotting (it’s perfect for including these visuals in reports), here’s how to do it:

import matplotlib.pyplot as plt
import cv2

# 1. Read image and convert to RGB (Matplotlib uses RGB)
img = cv2.imread('your_image_path.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# 2. Define bounding box coordinates (same as before)
x1, y1 = 100, 150
x2, y2 = 300, 350

# 3. Create a side-by-side plot
fig, (ax_original, ax_zoomed) = plt.subplots(1, 2, figsize=(12, 6))

# 4. Show original image with bounding box
ax_original.imshow(img_rgb)
# Draw the box: (x1,y1) = top-left, width = x2-x1, height = y2-y1
bounding_box = plt.Rectangle((x1, y1), x2-x1, y2-y1, edgecolor='red', facecolor='none', linewidth=2)
ax_original.add_patch(bounding_box)
ax_original.set_title('Original Image')
ax_original.axis('off')  # Hide axes for cleaner look

# 5. Show the cropped/zoomed region
cropped_rgb = img_rgb[y1:y2, x1:x2]
ax_zoomed.imshow(cropped_rgb)
ax_zoomed.set_title('Zoomed Cropped Region')
ax_zoomed.axis('off')

# Adjust layout and display
plt.tight_layout()
plt.show()

This setup is ideal for reports because you can easily tweak the figure size, add titles, and export the plot as a high-quality image (using plt.savefig('report_plot.png', dpi=300)).

内容的提问来源于stack exchange,提问作者Minchae

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 13:32:46