提取mask点

本文介绍了使用OpenCV库在Python中进行图像处理(如绘制边界框)以及基本的文件操作,包括图片文件名重命名、创建多级文件夹。还展示了如何从文本中提取特定内容的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import cv2

def draw_bounding_box(image, x, y, w, h):
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
    cv2.imshow("Image with Bounding Box", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# 示例使用
image_path = "path/to/your/image.jpg"
x = 100
y = 100
w = 200
h = 150

image = cv2.imread(image_path)
draw_bounding_box(image, x, y, w, h)



import cv2
import numpy as np
img = cv2.imread(r'C:\Users\lifan\Desktop\0afffc38803b6d42899ec84ede9b5a2.png')
array = np.sum(img[:,:,0],axis = 0)
for i in range(len(array)):
    if(array[i]>0):
        idx_y = i
        break
for i in range(img.shape[0]):
    if(img[i,idx_y,0]>0):
        idx_x = i
        break
for j in range(len(array)-1,0,-1):
    if (array[j] > 0):
        idx_y1 = j
        break
for k in range(img.shape[0]-1,0,-1):
    if(img[k,idx_y1,0]>0):
        idx_x1 = k
        break
cv2.circle(img, (idx_y,idx_x), 2, (0,0,255),1)
cv2.circle(img, (idx_y1,idx_x1), 2, (0,0,255),1)
cv2.imshow('a',img)
cv2.waitKey(0)

import os

def rename_images(folder_path):
    # 获取文件夹下的所有图片文件
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif']  # 可根据需要添加其他图片扩展名
    image_files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))
                   and os.path.splitext(f)[1].lower() in image_extensions]

    # 按照文件名排序
    image_files.sort()

    # 重命名图片文件
    for i, file_name in enumerate(image_files):
        # 构造新的文件名
        new_name = str(i + 1).zfill(8) + os.path.splitext(file_name)[1]
        # 构造旧的文件路径和新的文件路径
        old_path = os.path.join(folder_path, file_name)
        new_path = os.path.join(folder_path, new_name)
        # 重命名文件
        os.rename(old_path, new_path)

# 指定文件夹路径
folder_path = '/path/to/folder'
# 调用函数进行重命名
rename_images(folder_path)

import os

# 指定要创建的文件夹数量
num_folders = 5

# 循环创建文件夹
for i in range(1, num_folders+1):
    # 创建主文件夹
    folder_name = f"needle_{i}"
    folder_path = os.path.join(os.getcwd(), folder_name)
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
        print(f"文件夹 '{folder_name}' 已创建成功!")
    else:
        print(f"文件夹 '{folder_name}' 已存在。")
    
    # 创建子文件夹
    subfolder_name = "image"
    subfolder_path = os.path.join(folder_path, subfolder_name)
    if not os.path.exists(subfolder_path):
        os.makedirs(subfolder_path)
        print(f"子文件夹 'image' 已在 '{folder_name}' 文件夹下创建成功!")
    else:
        print(f"子文件夹 'image' 已在 '{folder_name}' 文件夹下存在。")

# 假设这是您从文本文件中读取的内容
file_content = "needle,line:This is the content, other stuff"

# 查找第一个 "line:" 的位置
line_start_index = file_content.find("line:")
if line_start_index != -1:
    # 查找逗号的位置,从 "line:" 的位置开始搜索
    comma_index = file_content.find(",", line_start_index)
    if comma_index != -1:
        # 提取所需内容
        extracted_content = file_content[line_start_index + len("line:"):comma_index]
        print("提取的内容:", extracted_content)
    else:
        print("找不到逗号")
else:
    print("找不到 line:")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值