视频 ROI 中的 OpenCV 颜色检测

发布于 2025-01-13 23:09:38 字数 2916 浏览 1 评论 0原文

我试图找出交通灯视频中特定投资回报率的颜色。该代码虽然正确预测了颜色,但它并没有针对我正在查看的特定投资回报率执行此操作。

最初,当交通视频开始时,ROI 区域没有 (RGY) 颜色,但它仍然根据其他区域预测并显示红色。我做错了什么。

已在此处上传测试视频进行测试 - https://ufile.io/ha20buns

下面的 Python 代码。

import cv2
import numpy as np

cap = cv2.VideoCapture('D:\Videos\Sample.mp4')

while True:
    ret,frame = cap.read()
    
    if ret == False: 
        break
    
    frame = cv2.resize(frame,(1920 ,1080))
    
    #Extract required section from entire frame
    
    roiColor = cv2.rectangle(frame.copy(),(1022, 565),(1411, 709),(255,255,255),2) #For SampleTL.mp4 
    
    blcolor = (255, 0, 0)
    cv2.rectangle(frame, (1022, 565),(1411, 709), blcolor)
    
    hsv = cv2.cvtColor(roiColor,cv2.COLOR_BGR2HSV) 

    #red 
    lower_hsv_red = np.array([157,177,122]) 
    upper_hsv_red = np.array([179,255,255]) 
    mask_red = cv2.inRange(hsv,lowerb=lower_hsv_red,upperb=upper_hsv_red) 
    red_blur = cv2.medianBlur(mask_red, 7) 
    
    #green 
    lower_hsv_green = np.array([49,79,137])
    upper_hsv_green = np.array([90,255,255])
    mask_green = cv2.inRange(hsv,lowerb=lower_hsv_green,upperb=upper_hsv_green) 
    green_blur = cv2.medianBlur(mask_green, 7) 
    
    lower_hsv_yellow = np.array([15,150,150])
    upper_hsv_yellow = np.array([35,255,255])
    mask_yellow = cv2.inRange(hsv,lowerb=lower_hsv_yellow,upperb=upper_hsv_yellow)
    yellow_blur = cv2.medianBlur(mask_yellow, 7)
    
    #Because the image is a binary image, If the image has a white point, which is 255, then take his maximum max value 255 
    red_color = np.max(red_blur)
    green_color = np.max(green_blur)
    yellow_color = np.max(yellow_blur)
    
    if red_color == 255: 
        print('red') 
        cv2.rectangle(frame,(1020,50),(1060,90),(0,0,255),2 ) #Draw a rectangular frame by coordinates
        cv2.putText(frame, "red", (1020, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255),2) #red text information
    
    elif green_color == 255: 
        print('green') 
        cv2.rectangle(frame,(1020,50),(1060,90),(0,255 ,0),2) 
        cv2.putText(frame, "green", (1020, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0),2)
    
    elif yellow_color == 255: 
        print('yellow') 
        cv2.rectangle(frame,(1020,50),(1060,90),(0,255 ,0),2) 
        cv2.putText(frame, "yellow", (1020, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0),2)

    cv2.imshow('frame',frame) 
    red_blur = cv2.resize(red_blur,(300,200)) 
    green_blur = cv2.resize(green_blur,(300,200))
    yellow_blur = cv2.resize(yellow_blur, (300,200))
    
    #cv2.imshow('red_window',red_blur) 
    #cv2.imshow('green_window',green_blur) 
    #cv2.imshow('yellow_window',yellow_blur)
    
    c = cv2.waitKey(10) 
    if c==27: 
        break
    
cap.release()
cv2.destroyAllWindows() # destroy all opened windows

I am trying to figure out color in a specific ROI in Traffic Light Video. The code although predicts the color correctly it doesn't do it for the specific ROI i am looking at.

Initially when the traffic video starts the ROI region has no (RGY) colors but it still predicts and shows RED based on other areas. What am i doing wrong.

Have uploaded the test Video here for testing -- https://ufile.io/ha20buns

Python Code below.

import cv2
import numpy as np

cap = cv2.VideoCapture('D:\Videos\Sample.mp4')

while True:
    ret,frame = cap.read()
    
    if ret == False: 
        break
    
    frame = cv2.resize(frame,(1920 ,1080))
    
    #Extract required section from entire frame
    
    roiColor = cv2.rectangle(frame.copy(),(1022, 565),(1411, 709),(255,255,255),2) #For SampleTL.mp4 
    
    blcolor = (255, 0, 0)
    cv2.rectangle(frame, (1022, 565),(1411, 709), blcolor)
    
    hsv = cv2.cvtColor(roiColor,cv2.COLOR_BGR2HSV) 

    #red 
    lower_hsv_red = np.array([157,177,122]) 
    upper_hsv_red = np.array([179,255,255]) 
    mask_red = cv2.inRange(hsv,lowerb=lower_hsv_red,upperb=upper_hsv_red) 
    red_blur = cv2.medianBlur(mask_red, 7) 
    
    #green 
    lower_hsv_green = np.array([49,79,137])
    upper_hsv_green = np.array([90,255,255])
    mask_green = cv2.inRange(hsv,lowerb=lower_hsv_green,upperb=upper_hsv_green) 
    green_blur = cv2.medianBlur(mask_green, 7) 
    
    lower_hsv_yellow = np.array([15,150,150])
    upper_hsv_yellow = np.array([35,255,255])
    mask_yellow = cv2.inRange(hsv,lowerb=lower_hsv_yellow,upperb=upper_hsv_yellow)
    yellow_blur = cv2.medianBlur(mask_yellow, 7)
    
    #Because the image is a binary image, If the image has a white point, which is 255, then take his maximum max value 255 
    red_color = np.max(red_blur)
    green_color = np.max(green_blur)
    yellow_color = np.max(yellow_blur)
    
    if red_color == 255: 
        print('red') 
        cv2.rectangle(frame,(1020,50),(1060,90),(0,0,255),2 ) #Draw a rectangular frame by coordinates
        cv2.putText(frame, "red", (1020, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255),2) #red text information
    
    elif green_color == 255: 
        print('green') 
        cv2.rectangle(frame,(1020,50),(1060,90),(0,255 ,0),2) 
        cv2.putText(frame, "green", (1020, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0),2)
    
    elif yellow_color == 255: 
        print('yellow') 
        cv2.rectangle(frame,(1020,50),(1060,90),(0,255 ,0),2) 
        cv2.putText(frame, "yellow", (1020, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0),2)

    cv2.imshow('frame',frame) 
    red_blur = cv2.resize(red_blur,(300,200)) 
    green_blur = cv2.resize(green_blur,(300,200))
    yellow_blur = cv2.resize(yellow_blur, (300,200))
    
    #cv2.imshow('red_window',red_blur) 
    #cv2.imshow('green_window',green_blur) 
    #cv2.imshow('yellow_window',yellow_blur)
    
    c = cv2.waitKey(10) 
    if c==27: 
        break
    
cap.release()
cv2.destroyAllWindows() # destroy all opened windows

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2025-01-20 23:09:38

cv2.rectangle 不会裁剪图像,而是返回带有绘制矩形的原始图像。试试这个:

roiColor = frame[565:709, 1022:1411]

cv2.rectangle doesn't crop the image but returns the original image with a drawn rectangle. Try this instead:

roiColor = frame[565:709, 1022:1411]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文