简体   繁体   English

不使用CV2,如何显示仅显示孤立绿色的图像

[英]Without using CV2, how do I display an image showing only the isolated green colors

My goal is to read an image of a plant and then output the picture showing only the green parts without any other background colors. 我的目标是读取植物的图像,然后输出仅显示绿色部分而没有任何其他背景颜色的图片。 Essentially, I only want to extract the leaf part from the picture and not the soil or anything else. 本质上,我只想从图片中提取叶子部分,而不是土壤或其他任何东西。 So far I've been able to read my images and change them to green, however my goal was to actually extract the green rather than change the whole green. 到目前为止,我已经能够读取图像并将其更改为绿色,但是我的目标是实际提取绿色而不是更改整个绿色。 Any ideas on what I might doing wrong? 关于我可能做错了什么的任何想法?

HERE IS MY CODE 这是我的密码

import matplotlib.pyplot as plt
import numpy as np


# Read and display picture
originalImage = plt.imread("C:/Users/user/Desktop/Image for detection1.jpg")
imgplot = plt.imshow(originalImage)

# Copy 1st picture and turn into an array
edittedImage = originalImage.copy()

arr = np.asarray(originalImage) # create array for image

lowerGreen = np.array([130,137,10]) # define lower values for green
upperGreen = np.array([220,235,130]) # define higher values for green


for color in arr: # loop through pixels to find the color green
    if color >= lowerGreen and <= upperGreen:
        print (color) # print the image showing only the green sections


imgplot = plt.imshow(edittedImage)

ERROR MSG: File "C:/Users/user/.spyder-py3/extract green.py", line 28, in if color >= lowerGreen and color <= upperGreen: 错误MSG:如果颜色> = lowerGreen和颜色<= upperGreen,则文件“ C:/Users/user/.spyder-py3/extract green.py”的第28行:

ValueError: The truth value of an array with more than one element is ambiguous. ValueError:具有多个元素的数组的真值不明确。 Use a.any() or a.all() 使用a.any()或a.all()

With OpenCv (cv2 module), I would do as follows: 使用OpenCv(cv2模块),我将执行以下操作:

import cv2 as cv
import numpy as np

img = cv.imread('MyImage.jpg')

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

# range of colors to filter by; you can adjust these parameters to fit your image:
lower_red = np.array([40,50,50])
upper_red = np.array([170,200,180])

# select parts of image in color range
mask = cv.inRange(hsv, lower_red, upper_red)
res = cv.bitwise_and(img,img, mask= mask)

cv.imshow('res',res) 
cv.waitKey() & 0xFF

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

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