簡體   English   中英

如何刪除此圖像的特定顏色?

[英]How can I remove especific color of this image?

我想刪除圖像Brain Image的所有米色,從而產生其他圖像(棕色、綠色、黃色和紅色)。 我嘗試使用刪除白色背景的相同代碼來執行此操作,只修改了 RGB 代碼,但沒有奏效。 (對不起,如果在帖子上犯了一些錯誤)

import cv2 
import numpy as np

# Read image
img = cv2.imread(r'D:\PY\mk1\mk1\sagitale1.png')
hh, ww = img.shape[:2]

# threshold on white
# Define lower and uppper limits
lower = np.array([101, 69, 51])
upper = np.array([255, 171, 142])

# Create mask to only select black
thresh = cv2.inRange(img, lower, upper)

# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(20,20))
morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

# invert morp image
mask = 255 - thresh

# apply mask to image
result = cv2.bitwise_and(img, img, mask=mask)


# save results
cv2.imwrite('1_thresh.jpg', thresh)
cv2.imwrite('1_result.jpg', result)


cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

你有你的 colors 以相反的順序。 OpenCV 在 B,G,R 訂單中使用 colors。 你需要稍微改變你的范圍。 你也不需要形態。

所以以下對我來說適用於 Python/OpenCV。

輸入:

在此處輸入圖像描述

import cv2 
import numpy as np

# Read image
img = cv2.imread('brain.png')
hh, ww = img.shape[:2]

# threshold on white
# Define lower and uppper limits
lower = np.array([90, 120, 200])
upper = np.array([170, 200, 255])

# Create mask to only select black
thresh = cv2.inRange(img, lower, upper)

# invert threshold image
mask = 255 - thresh

# apply mask to image
result = cv2.bitwise_and(img, img, mask=mask)

# save results
cv2.imwrite('1_thresh.jpg', thresh)
cv2.imwrite('1_result.jpg', result)

cv2.imshow('thresh', thresh)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

閾值圖像:

在此處輸入圖像描述

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM