每当我运行我的代码时,除了名为“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发布于 2021-06-04 01:20:01
试试这个:
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)https://stackoverflow.com/questions/67824095
复制相似问题