gpt4 book ai didi

python - 通过将背景设为白色从背景中提取前景

转载 作者:行者123 更新时间:2023-12-02 17:21:42 24 4
gpt4 key购买 nike

谢谢您的时间,我是opencv的新手,我想通过将背景设为白色来从图像中提取前景。到目前为止,我已经能够将图像背景转换为黑色,我怎样才能让它变白..??

Input ImageOutput Image

import numpy as np
import cv2 as cv
import cv2
import glob
import os


image_data_path = os.path.abspath('../8470p/corn_weed_datasets/bluegrass')
gt_data_path = os.path.abspath('../8470p/GT data')
image_out_path = os.path.abspath('../8470p/corn_weed_datasets/corn1')

curr_image = 0


for image_name in sorted(os.listdir(image_data_path)):

curr_image += 1


image_path = os.path.join(image_data_path, image_name)

img = cv2.imread(image_path)



hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)


l_b = np.array([23,26,12])
# the array is the our range of lower blue colors
u_b = np.array([277,255,277])
# the array is the our range of upper blue colors
mask = cv.inRange(hsv, l_b, u_b)
# threshold d hsv image to get only d blue color
res = cv2.bitwise_and(img,img, mask = mask)

Image = cv2.cvtColor(res, cv2.COLOR_RGB2BGR)

cv.imshow('res',Image)
k = cv.waitKey(0) & 0xFF
if k ==27:
break
for bb,file in enumerate (glob.glob(image_data_path)):
cv2.imwrite('../8470p/corn_weed_datasets/corn1/bluegrass{}.png'.format(bb), Image)
# cv2.imwrite(os.path.join(image_out_path,Image ))


cv.destroyAllWindows()

最佳答案

这是在 Python/OpenCV 中执行此操作的一种方法

  • 阅读输入
  • 转换为 HSV 颜色空间
  • 对绿色进行颜色阈值
  • 将形态学应用于阈值图像以去除无关的白色区域并保存蒙版
  • 将蒙版应用于输入图像
  • 黑改白
  • 保存结果

  • 输入:

    enter image description here
    import cv2
    import numpy as np

    # load image
    img = cv2.imread("green_plant.jpg")

    # convert to hsv
    hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

    # threshold using inRange
    range1 = (30,0,100)
    range2 = (255,255,255)
    mask = cv2.inRange(hsv,range1,range2)

    # apply morphology closing and opening to mask
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

    # make mask 3 channel
    mask = cv2.merge([mask,mask,mask])

    # invert mask
    mask_inv = 255 - mask

    # create white image for background
    white = np.full_like(img, (255,255,255))

    # apply mask to input
    img_masked = cv2.bitwise_and(img, mask)

    # apply inverted mask to white image
    white_masked = cv2.bitwise_and(white, mask_inv)

    # combine inverted mask with masked image
    result = cv2.add(img_masked, mask_inv)

    # write result to disk
    cv2.imwrite("green_plant_mask.png", mask)
    cv2.imwrite("green_plant_white_background.jpg", result)

    # display it
    cv2.imshow("mask", mask)
    cv2.imshow("mask_inv", mask_inv)
    cv2.imshow("img_masked", img_masked)
    cv2.imshow("result", result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    面具:

    enter image description here

    输入屏蔽:

    enter image description here

    结果:

    enter image description here

    关于python - 通过将背景设为白色从背景中提取前景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61956105/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com