简体   繁体   English

为什么 'cv2.imshow()' function 无法正确显示我的图像?

[英]Why 'cv2.imshow()' function is not displaying my image properly?

Whenever I run my code every image is shown correctly except the one named 'FireRegion'.每当我运行我的代码时,每个图像都会正确显示,除了名为“FireRegion”的图像。 It is being displayed as transparent window or errored window.它显示为透明 window 或错误的 window。 The countNonZero() function works fine though but the image is not being displayed. countNonZero() function 工作正常,但没有显示图像。 It works properly when I don't use the np.where() function and to use that after that I need to use the astype to convert image type to uint8 otherwise the compiler gives an error.当我不使用 np.where() function 并使用它时,它可以正常工作,之后我需要使用 astype 将图像类型转换为 uint8 ,否则编译器会出错。 I think its happening because of the bitwise_and() but its just a guess.我认为它的发生是因为 bitwise_and() 但这只是一个猜测。 Below is my code:下面是我的代码:

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

try this:尝试这个:

    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)

In place of:代替:

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

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

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