【问题标题】:Without using CV2, how do I display an image showing only the isolated green colors在不使用 CV2 的情况下,如何显示仅显示孤立绿色的图像
【发布时间】:2018-06-15 02:48:12
【问题描述】:

我的目标是读取植物的图像,然后输出仅显示绿色部分而没有任何其他背景颜色的图片。本质上,我只想从图片中提取叶子部分,而不是土壤或其他任何东西。到目前为止,我已经能够阅读我的图像并将它们更改为绿色,但是我的目标是实际提取绿色而不是更改整个绿色。关于我可能做错什么的任何想法?

这是我的代码

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)

错误信息: 文件“C:/Users/user/.spyder-py3/extract green.py”,第 28 行,在 如果颜色 >= lowerGreen 和颜色

ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()

【问题讨论】:

  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。
  • @Prune 好的,我将获取我的 MCVE 代码并编辑帖子。谢谢

标签: python image-processing spyder


【解决方案1】:

使用 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

【讨论】:

  • 这(植物的计算机视觉)恰好是我过去 6 个月以来专业从事的项目的一个组成部分。如果您对您的项目有任何其他问题,请随时私信我!
猜你喜欢
  • 1970-01-01
  • 2011-12-17
  • 2010-11-18
  • 2020-10-08
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
相关资源
最近更新 更多