【问题标题】:How to select/mask this object from black background如何从黑色背景中选择/屏蔽此对象
【发布时间】:2021-09-24 05:35:13
【问题描述】:

我只想裁剪对象而不是黑色背景。 如何使用 python / openCV 完成它?

我用过下面的代码,我只需要对象

import cv2
import numpy as np

# original image
# -1 loads as-is so if it will be 3 or 4 channel as the original
image = cv2.imread('/content/image1.jpg', -1)
# mask defaulting to black for 3-channel and transparent for 4-channel
# (of course replace corners with yours)
mask = np.zeros(image.shape, dtype=np.uint8)
roi_corners = np.array([[(10,10), (300,300), (10,300)]], dtype=np.int32)
# fill the ROI so it doesn't get wiped out when the mask is applied
channel_count = image.shape[2]  # i.e. 3 or 4 depending on your image
ignore_mask_color = (255,)*channel_count
cv2.fillPoly(mask, roi_corners, ignore_mask_color)
# from Masterfool: use cv2.fillConvexPoly if you know it's convex

# apply the mask
masked_image = cv2.bitwise_and(image, mask)

# save the result
cv2.imwrite('image_masked.png', masked_image)

【问题讨论】:

  • 我不明白你的问题。您能否从广义的角度或通过模拟示例阐明输出的外观?
  • 输出应该只有对象作为图像,用于在黑色图像上绘制多个对象
  • 当我运行时没有删除黑色背景
  • 我还是不明白。让我们称对象为流星,所以我假设您只想要流星 - 那么输出图像中黑色当前所在的位置会是什么?
  • 如果您不喜欢这两个现有答案中的任何一个......这可能是因为透明度蒙版是二进制的(是/否)。可以计算灰度透明度通道......但这有点复杂。必须(1)对对象周围逐渐消失的黑色做出反应(2)从每个像素的混合中去除黑色(或者将图片声明为“预乘 alpha”)

标签: python opencv image-processing


【解决方案1】:

您可以使用下面的代码来完成。你可以玩最小阈值来获得更好的结果(我发现 50 效果很好)

import cv2

image = cv2.imread(PathToYourImageFile)
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(imageGray, 80, 255, cv2.THRESH_BINARY)
b, g, r = cv2.split(image)
rgba = [b, g, r, thresh]
imageResult = cv2.merge(rgba, 4)
cv2.imwrite("ImageResult.png", imageResult)

结果:

【讨论】:

    【解决方案2】:

    您可以如下使黑色背景透明。

    import cv2
    
    img = cv2.imread("0byhS.jpg", 1)
    tmp = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    _,alpha = cv2.threshold(tmp,0,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU)
    b, g, r = cv2.split(img)
    rgba = [b,g,r, alpha]
    dst = cv2.merge(rgba,4)
    cv2.imwrite("test.png", dst)
    cv2.waitKey(0)
    

    【讨论】:

    • 感谢您的回答,我如何使用此图像以随机角度生成包含多个这些对象的图像?
    【解决方案3】:

    在以前的解决方案的基础上构建。

    如果您想以随机角度显示其中的多个,一个问题将是纵横比,因此会缩小尺寸。

    对于这个例子,我们将。

    • 将图片尺寸缩小 40%
    • 以 60 度的倍数随机复制和旋转。
    • 水平排列旋转后的图像

    代码:

    import cv2
    import numpy as np
    
    image = cv2.imread('comet.jpg')
    imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(imageGray, 80, 255, cv2.THRESH_BINARY)
    b, g, r = cv2.split(image)
    rgba = [b, g, r, thresh]
    imageResult = cv2.merge(rgba, 4)
    
    # code to merge images
    (h, w) = imageResult.shape[:2] 
    center = (w//2,h//2)
    arr = [ i for i in range( 10 , 360, 60)]
    
    all_images = []
    
    for _ in range(len(arr)):
           angle = np.random.choice(arr)
           M = cv2.getRotationMatrix2D(center, angle, 0.4)
           rotated = cv2.warpAffine(imageResult, M, (w, h))
           all_images.append(rotated)
    
    #merged_images = cv2.vconcat(all_images) #vertical concat
    merged_images = cv2.hconcat(all_images) #horizontal concat
    cv2.imwrite("comet_merged.png", merged_images)
    

    结果:

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 2014-01-30
      • 2021-12-12
      • 1970-01-01
      相关资源
      最近更新 更多