一些常用的,但是一时半会想不到来查(自用)

(1)c++简易方法算dice

#include <opencv2/opencv.hpp>

double calculateDiceCoefficient(const cv::Mat &mask1, const cv::Mat &mask2) {
    // 确保输入是二值图像(0和1),并且两个图像大小相同
    CV_Assert(mask1.type() == CV_8UC1 && mask2.type() == CV_8UC1);
    CV_Assert(mask1.size() == mask2.size());
    
    // 计算交集(mask1 AND mask2)
    cv::Mat intersection;
    cv::bitwise_and(mask1, mask2, intersection);
    double intersectionCount = cv::countNonZero(intersection);
    
    // 计算每个掩膜中的非零元素的总数(即面积)
    double area1 = cv::countNonZero(mask1);
    double area2 = cv::countNonZero(mask2);
    
    // 计算Dice系数
    double dice = (2.0 * intersectionCount) / (area1 + area2);
    return dice;
}

(2)python的rsplit()函数用法

text = "apple,banana,cherry,dates"
result = text.rsplit(",", maxsplit=2)


['apple,banana', 'cherry', 'dates']

#2


import os


a = 'datasets/coco/images/train2017/000000000009.jpg'
sa, sb = (
    os.sep + "images" + os.sep,
    os.sep + "labels" + os.sep,
) 

b = a.rsplit(sa, 1)
c = sb.join(b)
d = c.rsplit(".", 1)[0]
print(b)
print(c)
print(d)

['datasets/coco', 'train2017/000000000009.jpg']
datasets/coco/labels/train2017/000000000009.jpg
datasets/coco/labels/train2017/000000000009

(3)getattr()函数

class Car:
    def __init__(self, color, brand):
        self.color = color
        self.brand = brand

# 创建一个 Car 实例
my_car = Car("red", "Toyota")


# 用户输入想要获取的属性名称
property_name = input("Which property do you want to know? (color or brand) ")

# 使用 getattr() 获取属性
property_value = getattr(my_car, property_name, "Property not found")

print(property_value)

(4)random一些用法

random.uniform 随机浮点数

random.choices,k为返回几个元素

items = ['apple', 'banana', 'cherry']
random.choices(items, weights=[10, 1, 1], k=1)

random.shuffle(x, random=None)

random (可选): 这是一个可选参数,用于指定一个函数或方法,用来生成随机数。如果不提供,shuffle 将使用默认的随机数生成器。

(5) np.full(shape, fill_value, dtype=None, order='C')填值

(6)c++与python 获得时间计时相关

c++

#include <iostream>
#include <chrono>

int main() {
    // 开始时间点
    auto start = std::chrono::high_resolution_clock::now();

    // 这里放置你想要测量执行时间的代码
    // ...

    // 结束时间点
    auto end = std::chrono::high_resolution_clock::now();

    // 计算并输出经过的时间,转换为微秒
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "Time taken: " << duration << " microseconds." << std::endl;

    return 0;
}

python

import time

# 开始时间
start_time = time.time()

# 这里放置你想要测量执行时间的代码
# ...

# 结束时间
end_time = time.time()

# 计算并输出经过的时间,以秒为单位
duration = end_time - start_time
print(f"Time taken: {duration} seconds.")

(7)python isinstance函数作用

class Fruit:
    pass

class Apple(Fruit):
    pass

apple = Apple()

# 检查 apple 是否是 Apple 的实例
print(isinstance(apple, Apple))  # 输出 True

# 检查 apple 是否是 Fruit 的实例(考虑到继承)
print(isinstance(apple, Fruit))  # 输出 True

# 检查 apple 是否是字符串类型
print(isinstance(apple, str))  # 输出 False

(8)图片转gif

import imageio
import os

# 图片所在文件夹路径
image_folder = './image_folder'
# 输出GIF文件路径
gif_path = os.path.join(image_folder, 'output.gif')

# 获取文件夹中所有图片文件的路径
images = [os.path.join(image_folder, file) for file in os.listdir(image_folder) if file.endswith(('.png', '.jpg', '.jpeg'))]
images.sort()  # 如果需要,根据文件名排序

# 准备一个GIF写入器,指定每秒帧数
writer = imageio.get_writer(gif_path, fps=10)

# 遍历图片文件
for image_path in images:
    image = imageio.imread(image_path)  # 读取图片
    writer.append_data(image)  # 添加当前图片到GIF

# 完成GIF的写入
writer.close()
print(f'Converted images in {image_folder} to {gif_path}')

(9)np和torch判断数组的连续性

np.ascontiguousarray(img)
tensor.contiguous()

(10)安装完opencv,还需要安装opencv-contrib-python

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值