image - 背景图像的粒子分割

标签 image opencv image-processing deep-learning computer-vision

Wear Image

在此图像中,您可以看到的所有黑色线和小黑点都是粒子(前景),白色、黄色、绿色、灰色和小蓝色区域是背景。我正在寻找一种算法来提取前景并用一些已知的颜色(例如白色)替换背景。您能给我建议一个更好的解决方案来实现目标吗? 谢谢

最佳答案

enter image description here

简单的颜色阈值应该在这里起作用。这个想法是通过使用具有下/上阈值范围的 HSV 颜色阈值来隔离黑色以获得掩码,然后 cv2.bitwise_and 获得过滤结果

您没有指定语言,因此这是 Python 中的实现

代码

import cv2
import numpy as np

image = cv2.imread('1.jpg')

# Set minimum and maximum HSV values to display
lower = np.array([0,0,0])
upper = np.array([179,255,52])

# Convert to HSV format and color threshold
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(image, image, mask=mask)
result[mask==0] = (255,255,255)

# Display
cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
# cv2.imwrite('result.png', result)
cv2.waitKey()

带有 slider 的HSV颜色阈值脚本,记得更改图像文件路径。您可以使用 slider 来细化分割。

import cv2
import numpy as np

def nothing(x):
    pass

# Load image
image = cv2.imread('1.jpg')

# Create a window
cv2.namedWindow('image')

# Create trackbars for color change
# Hue is from 0-179 for Opencv
cv2.createTrackbar('HMin', 'image', 0, 179, nothing)
cv2.createTrackbar('SMin', 'image', 0, 255, nothing)
cv2.createTrackbar('VMin', 'image', 0, 255, nothing)
cv2.createTrackbar('HMax', 'image', 0, 179, nothing)
cv2.createTrackbar('SMax', 'image', 0, 255, nothing)
cv2.createTrackbar('VMax', 'image', 0, 255, nothing)

# Set default value for Max HSV trackbars
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)

# Initialize HSV min/max values
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0

while(1):
    # Get current positions of all trackbars
    hMin = cv2.getTrackbarPos('HMin', 'image')
    sMin = cv2.getTrackbarPos('SMin', 'image')
    vMin = cv2.getTrackbarPos('VMin', 'image')
    hMax = cv2.getTrackbarPos('HMax', 'image')
    sMax = cv2.getTrackbarPos('SMax', 'image')
    vMax = cv2.getTrackbarPos('VMax', 'image')

    # Set minimum and maximum HSV values to display
    lower = np.array([hMin, sMin, vMin])
    upper = np.array([hMax, sMax, vMax])

    # Convert to HSV format and color threshold
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower, upper)
    result = cv2.bitwise_and(image, image, mask=mask)
    result[mask==0] = (255,255,255)

    # Print if there is a change in HSV value
    if((phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
        print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
        phMin = hMin
        psMin = sMin
        pvMin = vMin
        phMax = hMax
        psMax = sMax
        pvMax = vMax

    # Display result image
    cv2.imshow('image', result)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

关于image - 背景图像的粒子分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69277368/

相关文章:

c - 在 opencv 序列中需要帮助!

c++ - 从缓冲区中获取最清晰的 iplimage

java - 使用 GAE Image API 进行图像转换

python - 使用 Python 镜像图像

css - div 标签中的图像

image - 在 LaTeX 中 float 图像后如何清除?

image - 使用SVG居中图像

python - 为什么在更改Sobel运算符的ddepth参数时输出图像有显着差异?

css - 使用图像作为输入文件显示输入类型文件中的图像文件名

Java AffineTransformOp内存问题