改变图像/掩码的背景

改变图像/掩码的背景

import cv2
import numpy as np 
import time

def mask_adjust_background(
    mask, mask_backgroud_color=(192, 192, 192),
    mask_color=(255, 255, 255)
):
    contours, _ = cv2.findContours(
        mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
    )
    mask = cv2.cvtColor(
        mask,
        cv2.COLOR_GRAY2BGR
    )
    mask_background = np.ones_like(mask)*mask_backgroud_color
    mask_visual = cv2.fillPoly(mask_background, contours, mask_color)

    return mask_visual

if __name__ == '__main__':
  start_time = time.time()
  mask_path = "./masks/test.png"
  mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
  mask_visual = mask_adjust_background(mask)
  cv2.imwrite("./masks_backgroud_test/test.png", mask_visual)
  print("--- %s seconds ---" % (time.time() - start_time))
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取 IPv6 地址的方法与获取 IPv4 地址有所不同。IPv6 地址由 16 个字节组成,通常用 8 组 16 进制数表示,每组之间用冒号分隔,例如:2001:0db8:85a3:0000:0000:8a2e:0370:7334。 要获取一个 IPv6 网段中的所有 IP 地址,可以使用如下代码: ```java import java.net.Inet6Address; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; public class IPv6Utils { public static List<String> getIpsFromIPv6(String ipv6, int prefixLength) throws UnknownHostException { List<String> ips = new ArrayList<>(); InetAddress inetAddress = InetAddress.getByName(ipv6); byte[] bytes = inetAddress.getAddress(); // 计算掩码二进制数组 byte[] masks = new byte[bytes.length]; for (int i = 0; i < masks.length; i++) { masks[i] = (byte) (prefixLength >= 8 ? 0xff : (0xff << (8 - prefixLength))); if (prefixLength > 8) { prefixLength -= 8; } else { prefixLength = 0; } } // 将掩码数组和 IPv6 地址数组进行按位与运算 for (int i = 0; i < bytes.length; i++) { bytes[i] &= masks[i]; } // 生成所有可能的 IP 地址 for (int i = 0; i < (1 << (16 - prefixLength)); i++) { byte[] newBytes = bytes.clone(); for (int j = 0; j < (16 - prefixLength) / 8; j++) { newBytes[bytes.length - j - 1] |= (byte) ((i >> (j * 8)) & 0xff); } InetAddress newInetAddress = Inet6Address.getByAddress(newBytes); ips.add(newInetAddress.getHostAddress()); } return ips; } } ``` 该方法接受两个参数,一个是 IPv6 地址,一个是前缀长度(即掩码长度),返回一个包含所有 IP 地址的列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值