图像叠加cv2.seamlessClone

opencv python用一个函数将两个图像进行叠加:cv2.seamlessClone

im = cv2.imread('E:/bglearn/pic/90.jpg')
    obj = cv2.imread('E:/bglearn/pic/90.jpg')

    mask = 255 * np.ones(obj.shape, obj.dtype)

    # The location of the center of the src in the dst
    width, height, channels = im.shape
    center = (height // 2, width // 2)

    # Seamlessly clone src into dst and put the results in output
    normal_clone = cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)
    mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)


    # Clone seamlessly.


    # 保存结果
    cv_show('normal_clone',normal_clone)
    #cv_show('mixed_clone',mixed_clone)
    new_bottle = cv2.absdiff(im, normal_clone)
    # cv_show('new_bottle', new_bottle)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))
    new_bottle_gray = cv2.cvtColor(new_bottle, cv2.COLOR_BGR2GRAY)
    new_bottle_thresh = cv2.threshold(new_bottle_gray, 10, 255, cv2.THRESH_BINARY)[1]
    # 得到新瓶子的mask图
    new_bottle_mask = cv2.morphologyEx(new_bottle_thresh, cv2.MORPH_CLOSE, kernel)
    # cv_show('new_bottle_mask', new_bottle_mask)
    _, new_bottle_mask_contours, _ = cv2.findContours(new_bottle_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(normal_clone, new_bottle_mask_contours, -1,
                         (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), 2)

    cv_show('normal_clone',normal_clone)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 `splitContainer1.Panel2` 控件中叠加绘制100个图像,您可以在 `splitContainer1_Panel2_Paint` 函数中使用 `Graphics` 对象的 `DrawImage` 方法,并将 `ImageAttributes` 对象的 `ColorMatrix` 属性设置为透明度小于 1 的值,从而实现图像叠加效果。 以下是示例代码: ```csharp private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e) { // 加载图像文件 Image image = Image.FromFile("image.jpg"); // 创建 ImageAttributes 对象 ImageAttributes imageAttributes = new ImageAttributes(); // 创建 ColorMatrix 对象,将透明度设置为0.5 ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.Matrix33 = 0.5f; imageAttributes.SetColorMatrix(colorMatrix); // 计算每个图像在控件中的位置和大小 int imageWidth = splitContainer1.Panel2.Width / 10; int imageHeight = splitContainer1.Panel2.Height / 10; Rectangle imageRect = new Rectangle(0, 0, imageWidth, imageHeight); // 在控件中循环绘制每个图像 for (int i = 0; i < 100; i++) { // 计算当前图像在控件中的位置 int x = (i % 10) * imageWidth; int y = (i / 10) * imageHeight; imageRect.Location = new Point(x, y); // 绘制当前图像 e.Graphics.DrawImage(image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); } } ``` 在上面的代码中,我们首先加载了一个图像文件,并创建了一个 `ImageAttributes` 对象。然后,我们创建了一个 `ColorMatrix` 对象,并将其中的 `Matrix33` 属性设置为 0.5,即将透明度设置为 0.5。接下来,我们计算了每个图像在控件中的位置和大小,并使用一个 for 循环在控件中叠加绘制每个图像。在绘制每个图像时,我们使用 `e.Graphics.DrawImage` 方法,并将 `ImageAttributes` 对象作为最后一个参数传递,从而实现图像叠加效果。 请注意,叠加绘制多个图像可能会对性能产生影响,具体取决于图像的大小和计算机的性能。如果您需要叠加绘制更多的图像,可以考虑使用双缓冲技术来提高性能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值