简体   繁体   English

使用Opencv Python分析特定区域中的图像并确定其是否为红色

[英]Analyze an image in specific area and determine if it's red using Opencv Python

I'm using Opencv python in raspberry pi, to analize a heatmap, i'm looking for color red, which represents the highest temperature, i need to detect if in an specific area exist red color, in case it does i can use this information to activate a condition, i'm using a heatmap like this: 我在树莓派中使用Opencv python来分析热图,我正在寻找代表最高温度的红色,我需要检测特定区域中是否存在红色,以防万一,我可以使用它激活条件的信息,我正在使用像这样的热图: 在此处输入图片说明

for the red color detection i'm using this code: 对于红色检测,我正在使用以下代码:

import cv2
import numpy as np

while(1):

    # Take each frame

    frame = cv2.imread('heatmap.png')

    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV
    lower_red = np.array([-20, 100, 100])
    upper_red = np.array([13, 255, 255])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower_red, upper_red)


    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)

    cv2.imshow('heatmap',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()

the code above give me al the pixels in red, but i need to determine if the heatmap contour is red, i mean the image contour or border would be a red color not permited area, does anyone how i can do that? 上面的代码为我提供了所有红色像素,但是我需要确定热图轮廓是否为红色,我的意思是图像轮廓或边框将为红色的不允许区域,有人可以这样做吗?

Your HSV range is not right. 您的HSV范围不正确。 For red, (0,20,20)~(8,255,255), (170,20,20) ~ (180,255,255) . 对于红色, (0,20,20)~(8,255,255), (170,20,20) ~ (180,255,255)


Here is my result: 这是我的结果:

在此处输入图片说明


The code: 编码:

#!/usr/bin/python3
# 2018/05/16 13:54:09 
import cv2
import numpy as np

img = cv2.imread('heatmap.png')
# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask1 = cv2.inRange(hsv, (0,20,20), (8,255,255))
mask2 = cv2.inRange(hsv, (170,20,20), (180,255,255))
mask = cv2.bitwise_or(mask1, mask2)
dst  = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("dst", dst)
cv2.imwrite("__.png", dst)
cv2.waitKey()

Some useful links: 一些有用的链接:

  1. Choosing the correct upper and lower HSV boundaries for color detection with`cv::inRange` (OpenCV) 选择正确的HSV上下边界以使用`cv :: inRange`(OpenCV)进行颜色检测
  2. How to define a threshold value to detect only green colour objects in an image :Opencv 如何定义阈值以仅检测图像中的绿色对象:Opencv

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

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