opencv-如何获取图像区域特定像素区域大小

需求

通过鼠标框选某个区域,返回这个区域的像素大小。

源码

# e:path\to\cal_rectangle_area.py
import cv2  
import numpy as np  

# 初始化变量
image = cv2.imread('./vlcsnap-2024-09-25-10h51m27s007.png')  
if image is None:  
    print("Error: Image could not be read.")  
    exit()  

# 窗口和缩放参数
scale = 1.0
pan_x, pan_y = 0, 0
rectangles = []  # 存储所有矩形框
refPt = []

def draw_rectangle(event, x, y, flags, param):  
    global refPt  
    if event == cv2.EVENT_LBUTTONDOWN:  
        # 计算真实坐标
        real_x = int((x + pan_x) / scale)
        real_y = int((y + pan_y) / scale)
        print(f"Mouse Position (Real): ({real_x}, {real_y})")
        refPt = [[x, y]]  
    elif event == cv2.EVENT_LBUTTONUP and len(refPt) == 1:  
        refPt.append([x, y])  
        rectangles.append(refPt)  # 保存矩形框
        cv2.rectangle(image, tuple(refPt[0]), tuple(refPt[1]), (0, 255, 0), 2)  
        cv2.imshow('image', image)  
        print(f"Width: {abs(refPt[1][0] - refPt[0][0])}, Height: {abs(refPt[1][1] - refPt[0][1])}")  
        refPt = []  

def update_image():
    # 应用缩放和平移
    h, w = image.shape[:2]
    new_image = cv2.resize(image, (int(w * scale), int(h * scale)))
    new_image = new_image[pan_y:pan_y + int(h * scale), pan_x:pan_x + int(w * scale)]
    
    # 绘制所有矩形框
    for rect in rectangles:
        cv2.rectangle(new_image, tuple(rect[0]), tuple(rect[1]), (0, 255, 0), 2)
    
    cv2.imshow('image', new_image)

def crop_and_save_image(x, y):
    # 裁剪图像
    h, w = image.shape[:2]
    x1 = max(x - 320, 0)  # 640/2
    y1 = max(y - 320, 0)  # 640/2
    x2 = min(x + 320, w)  # 640/2
    y2 = min(y + 320, h)  # 640/2

    cropped_image = image[y1:y2, x1:x2]
    cv2.imwrite('cropped_image.png', cropped_image)  # 保存裁剪后的图像
    print(f"Cropped image saved as 'cropped_image.png'.")

# 设置鼠标回调函数  
cv2.namedWindow('image')  
cv2.setMouseCallback('image', draw_rectangle)  

# 显示图像直到有键被按下  
while True:  
    update_image()  # 更新显示的图像
    key = cv2.waitKey(1) & 0xFF  
    if key == ord('q'):  
        break  
    elif key == ord('+'):  # 缩放放大
        scale *= 1.1
    elif key == ord('-'):  # 缩放缩小
        scale /= 1.1
    elif key == ord('w'):  # 向上移动
        pan_y -= 10
    elif key == ord('s'):  # 向下移动
        pan_y += 10
    elif key == ord('a'):  # 向左移动
        pan_x -= 10
    elif key == ord('d'):  # 向右移动
        pan_x += 10
    elif key == ord('c'):  # 输入坐标裁剪图像
        x = int(input("Enter x coordinate: "))
        y = int(input("Enter y coordinate: "))
        crop_and_save_image(x, y)

cv2.destroyAllWindows()
相关推荐
计算机科研狗@OUC几秒前
【TMM2024】Frequency-Guided Spatial Adaptation for Camouflaged Object Detection
人工智能·深度学习·目标检测·计算机视觉
聪明的墨菲特i4 分钟前
Django前后端分离基本流程
后端·python·django·web3
悟兰因w8 分钟前
论文阅读(三十五):Boundary-guided network for camouflaged object detection
论文阅读·人工智能·目标检测
大山同学10 分钟前
多机器人图优化:2024ICARA开源
人工智能·语言模型·机器人·去中心化·slam·感知定位
工业3D_大熊10 分钟前
【虚拟仿真】CEETRON SDK在船舶流体与结构仿真中的应用解读
java·python·科技·信息可视化·c#·制造·虚拟现实
Topstip16 分钟前
Gemini 对话机器人加入开源盲水印技术来检测 AI 生成的内容
人工智能·ai·机器人
SEEONTIME19 分钟前
python-24-一篇文章彻底掌握Python HTTP库Requests
开发语言·python·http·http库requests
Bearnaise19 分钟前
PointMamba: A Simple State Space Model for Point Cloud Analysis——点云论文阅读(10)
论文阅读·笔记·python·深度学习·机器学习·计算机视觉·3d
小嗷犬32 分钟前
【论文笔记】VCoder: Versatile Vision Encoders for Multimodal Large Language Models
论文阅读·人工智能·语言模型·大模型·多模态
Struart_R37 分钟前
LVSM: A LARGE VIEW SYNTHESIS MODEL WITH MINIMAL 3D INDUCTIVE BIAS 论文解读
人工智能·3d·transformer·三维重建