Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在OpenCV python中将像素值转换为整数?_Python 3.x_Opencv_Image Processing_Cv2 - Fatal编程技术网

Python 3.x 如何在OpenCV python中将像素值转换为整数?

Python 3.x 如何在OpenCV python中将像素值转换为整数?,python-3.x,opencv,image-processing,cv2,Python 3.x,Opencv,Image Processing,Cv2,我试图在图像上实现这个等式。以下是相同的代码: if counter>0: CurrentFrame = frame[x:x+w, y:y+h] (a,b,c) = np.shape(CurrentFrame) if a==0 or b==0 or c==0: continue CurrentFrame = FPC(CurrentFrame) cv2.imshow("FireRegion",Cur

我试图在图像上实现这个等式。以下是相同的代码:

if counter>0:
    CurrentFrame = frame[x:x+w, y:y+h]
    (a,b,c) = np.shape(CurrentFrame)
    if a==0 or b==0 or c==0:
        continue
        
    CurrentFrame = FPC(CurrentFrame)
    cv2.imshow("FireRegion",CurrentFrame.astype(np.uint8))
    subResult = np.subtract(PreviousFrame, CurrentFrame)
    FrameDifference.append(subResult)
    PreviousFrame = CurrentFrame
    
    if counter != 30:
        counter = counter+1
        cv2.imshow("frame",frame)
        continue
    else:
        sum = FrameDifference[0]
        for i in FrameDifference:
            sum = np.add(sum, i)
             
        n = len(FrameDifference)    
        AvgTemporalVariation = sum//n
        cv2.imshow("AvgTemporalVariation",AvgTemporalVariation.astype(np.uint8))
        DFP = np.where((AvgTemporalVariation>100) & (AvgTemporalVariation<200),255,0)
        cv2.imshow("DFP",DFP.astype(np.uint8))
        FrameDifference.clear()
        PreviousFrame = None
        counter = 0

您需要将图像(或处理过程中处理的任何图像)转换为
int
,以克服整数溢出(甚至下溢)。对于某些NumPy数组
a
,您可以这样转换:
a=a.astype(int)
。主要问题是,您希望8位无符号整数的值为510。'a.astype(int)'不起作用。输出仍然相同。请根据a缩短代码,并包括示例图像和所需的输出。你随时都可以。
from collections import deque
import cv2
import numpy as np
import imutils
import time
import argparse

def FPC(img):

Rule1 = img.copy()
Rule2 = img.copy()
Rule3 = img.copy()
Rule4 = img.copy()
Rule5 = 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))
    
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)

return FireRegion    

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
args = vars(ap.parse_args())

if args.get("video", None) is None:
    cap = cv2.VideoCapture(0)
    time.sleep(2.0)
else:
    cap = cv2.VideoCapture(args["video"])    


firstFrame = None
counter = 0
FrameDifference = []
PreviousFrame = None
while True:
    _,frame = cap.read()
    if np.shape(frame) == ():
        cap = cv2.VideoCapture(args["video"])
        continue
    
    if counter>0:
        CurrentFrame = frame[x:x+w, y:y+h]
        (a,b,c) = np.shape(CurrentFrame)
        if a==0 or b==0 or c==0:
            continue
            
        CurrentFrame = FPC(CurrentFrame)
        cv2.imshow("FireRegion",CurrentFrame.astype(np.uint8))
        subResult = np.subtract(PreviousFrame, CurrentFrame)
        FrameDifference.append(subResult)
        PreviousFrame = CurrentFrame
        
        if counter != 30:
            counter = counter+1
            cv2.imshow("frame",frame)
            continue
        else:
            sum = FrameDifference[0]
            for i in FrameDifference:
                sum = np.add(sum, i)
                 
            n = len(FrameDifference)    
            AvgTemporalVariation = sum//n
            cv2.imshow("AvgTemporalVariation",AvgTemporalVariation.astype(np.uint8))
            DFP = np.where((AvgTemporalVariation>100) & (AvgTemporalVariation<200),255,0)
            cv2.imshow("DFP",DFP.astype(np.uint8))
            FrameDifference.clear()
            PreviousFrame = None
            counter = 0
    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray,(21,21),0)
    
    if firstFrame is None:
        firstFrame = gray
        continue
        
    frameDelta = cv2.absdiff(firstFrame, gray)
    thresh = cv2.threshold(frameDelta, 25,255,cv2.THRESH_BINARY)[1]
    thresh = cv2.dilate(thresh, None, iterations = 2)
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    
    if len(cnts) != 0:
        counter = 1
        c = max(cnts, key = cv2.contourArea)
        (x,y,w,h) = cv2.boundingRect(c)
        ROI = frame[x:x+w, y:y+h]
        (a,b,c) = np.shape(ROI)
        if a==0 or b==0 or c==0:
            continue
        
        FireRegion = FPC(ROI)
        
        if PreviousFrame is None:
            PreviousFrame = FireRegion 
            
        
           
     
        cv2.imshow("frame",frame)
        cv2.imshow("FireRegion",FireRegion.astype(np.uint8))
       
    else :
        cv2.imshow("frame",frame)
                     
        
    key = cv2.waitKey(1)
    if key ==27 :
        break