【问题标题】:Why 'cv2.imshow()' function is not displaying my image properly?为什么 'cv2.imshow()' 函数不能正确显示我的图像?
【发布时间】:2021-08-21 17:34:52
【问题描述】:

每当我运行我的代码时,除了名为“FireRegion”的图像之外,每个图像都会正确显示。它显示为透明窗口或错误窗口。 countNonZero() 函数虽然工作正常,但没有显示图像。当我不使用 np.where() 函数时它可以正常工作,之后我需要使用 astype 将图像类型转换为 uint8 ,否则编译器会出错。我认为它的发生是因为 bitwise_and() 但这只是一个猜测。以下是我的代码:

from playsound import playsound
import cv2 
import numpy as np 
import time

if True:
    img = cv2.imread('WFire.jpg')
    Rule1 = img.copy()
    Rule2 = img.copy()
    Rule3 = img.copy()
    Rule4 = img.copy()
    Rule5 = img.copy()
    RE = img.copy()
    RE2 = img.copy()
    Ymean = img.copy()

    YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
    Y = YCrCb[:,:,0]
    Cr = YCrCb[:,:,1]
    Cb = YCrCb[:,:,2]

    Ym = int(np.mean(Y))
    Crm = int(np.mean(Cr))
    Cbm = int(np.mean(Cb))

    # h = img.shape[0]
    # w = img.shape[1]

    # for x in range(0,h) :
        # for y in range(0,w) :
            # RE[x,y] = 255 if Y[x,y] > Cb[x,y] else 0
            # RE2[x,y] = 255 if Cr[x,y] > Cb[x,y] else 0
            # Rule3[x,y] = 255 if abs(int(Cb[x,y])-int(Cr[x,y]))>=70 else 0
            # RE[x,y] = 255 if Y[x,y]>Ym or Cb[x,y]>Cbm or Cr[x,y]>Crm else 0  
            # RE2[x,y] = 255 if Cb[x,y]<=120 and Cr[x,y]>=150 else 0
            
    Ymean = np.where(True,Ym,0)       
       
    Rule1 = np.where(Y>Cb,255,0)
    Rule2 = np.where(Cr>Cb,255,0)
    Rule3 = np.where(abs(Cb-Cr)>=70,255,0)
    Rule4 = np.where((Y>Ym)|(Cb>Cbm)|(Cr>Crm),255,0) 
    Rule5 = np.where((Cb<=120) & (Cr>=150),255,0)
    
    FireRegion = cv2.bitwise_and(Rule1,Rule2)   
    FireRegion = cv2.bitwise_and(FireRegion,Rule3)   
    FireRegion = cv2.bitwise_and(FireRegion,Rule4)   
    FireRegion = cv2.bitwise_and(FireRegion,Rule5)
    # FireRegionGray = cv2.cvtColor(FireRegion.astype(np.float32),cv2.COLOR_BGR2GRAY)    
    NFP = cv2.countNonZero(FireRegion)
    print(NFP)
    # if NFP>2000 :
        # playsound('duck1.mp3')
    
    cv2.imshow("Y",Y)
    cv2.imshow("Cr",Cr)
    cv2.imshow("Cb",Cb)
    cv2.imshow("Original",img)
    cv2.imshow("Rule1",Rule1.astype(np.uint8))
    cv2.imshow("Rule2",Rule2.astype(np.uint8))
    cv2.imshow("Rule3",Rule3.astype(np.uint8))
    cv2.imshow("Rule4",Rule4.astype(np.uint8))
    cv2.imshow("Rule5",Rule5.astype(np.uint8))
    cv2.imshow("FireRegion",FireRegion.astype(np.uint8))

    
    key = cv2.waitKey(0)
    # if key ==27 :
        # break

【问题讨论】:

  • Rule1 到 Rule5 的类型是什么?你可以试试 pixrl-wise 乘法而不是 bitwise_and 吗?

标签: python python-3.x opencv cv2


【解决方案1】:

试试这个:

    FR_1 = cv2.bitwise_and(Rule1,Rule2)   
    FR_2 = cv2.bitwise_and(FR_1,Rule3)   
    FR_3 = cv2.bitwise_and(FR_2,Rule4)   
    FireRegion = cv2.bitwise_and(FR_3,Rule5)

代替:

    FireRegion = cv2.bitwise_and(Rule1,Rule2)   
    FireRegion = cv2.bitwise_and(FireRegion,Rule3)   
    FireRegion = cv2.bitwise_and(FireRegion,Rule4)   
    FireRegion = cv2.bitwise_and(FireRegion,Rule5)

【讨论】:

    猜你喜欢
    • 2020-08-04
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2020-11-01
    • 1970-01-01
    相关资源
    最近更新 更多