简体   繁体   English

在Open CV中为彩色图像应用蒙版

[英]Applying mask for coloured image in Open cv

What changes should I make in below code so that if I know HSV value of green colour, I should get image segments which don't contain the green colour as my output? 我应该在下面的代码中进行哪些更改,以便如果我知道绿色的HSV值,则应该获得不包含绿色的图像段作为输出?

Image on which I was working is: source image 我正在处理的图像是: 源图像

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread("sun.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

mask = cv2.inRange(hsv, (36, 25, 25), (70, 255,255))         ## mask of green (36,25,25) ~ (86, 255,255)

imask = mask>0      ## slice the green
green = np.zeros_like(img, np.uint8)
green[imask] = img[imask]

cv2.imshow('image ',green)
cv2.waitKey(0)
cv2.imwrite("green1.png", green)     ##saving

actual output: is the only green coloured portion 实际输出:是唯一的绿色部分
expected output: except the green colour portion 预期输出:绿色部分除外

You should change the line 你应该换线

green[imask] = img[imask]

with

green = img[!imask] # not of mask to get non-green regions in image

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM