Creating Histogram with Video

Oak_Cassia·2022년 8월 1일
0

Video

from PyQt5.QtWidgets import *
import sys
import cv2 as cv
import time
import numpy as np
import matplotlib.pyplot as plt

global masked_img
global img2

class Video(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Capture frame on Video')
        self.setGeometry(200,200,600,120)
        
        videoButton=QPushButton('Open',self)
        captureButton=QPushButton('Capture',self)
        histButton=QPushButton('Hist',self)
        quitButton=QPushButton('Quit',self)
        
        videoButton.setGeometry(10,10,100,30) 
        captureButton.setGeometry(110,10,100,30)
        histButton.setGeometry(310,10,100,30)
        quitButton.setGeometry(410,10,100,30)
        
        videoButton.clicked.connect(self.videoFunction)
        captureButton.clicked.connect(self.captureFunction)
        histButton.clicked.connect(self.histFunction)
        quitButton.clicked.connect(self.quitFunction)
        
    
    def videoFunction(self):
        fname=QFileDialog.getOpenFileName(self,'Open Video','./')
        self.cap=cv.VideoCapture(fname[0])
        if not self.cap.isOpened(): self.close()
        
        while True:
            ret,self.frame=self.cap.read()
            if not ret: break
            time.sleep(1/25)
            cv.imshow('video',self.frame)
            cv.waitKey(1)
        
    
    def captureFunction(self):        
        self.capturedFrame=self.frame
        img2=self.capturedFrame
        if self.capturedFrame is None:
            return
        cv.namedWindow('Captured Frame')
        cv.setMouseCallback('Captured Frame', self.drawFunction)
        cv.imshow('Captured Frame',self.capturedFrame)
        
    
    def histFunction(self):
        mask = np.zeros(self.capturedFrame.shape[:2],np.uint8)
        mask[self.iy:self.iy2,self.ix:self.ix2]=255
        

        color = ('b','g','r')
        for i,col in enumerate(color):
            histr = cv.calcHist([self.capturedFrame],[i],mask,[256],[0,256])
            plt.plot(histr,color = col)
            plt.xlim([0,256])
        plt.title('Tree')
        plt.show()
        print(self.ix)
        print(self.iy)
        print(self.ix2)
        print(self.iy2)
        masked_img=cv.bitwise_and(self.capturedFrame,self.capturedFrame,mask=mask)
        cv.imshow('test1',masked_img)
        
        
        
    def quitFunction(self):
        if self.cap.isOpened():
            self.cap.release()
        cv.destroyAllWindows()
        self.close()
    
    def drawFunction(self,event,x,y,flags,param):
    
        if event==cv.EVENT_LBUTTONDOWN:
            self.ix,self.iy=x,y
        elif event==cv.EVENT_LBUTTONUP:
            cv.rectangle(self.capturedFrame,(self.ix,self.iy),(x,y),(0,0,255),2)
            self.ix2=x
            self.iy2=y
        cv.imshow('Captured Frame',self.capturedFrame)
        
    
    
app=QApplication(sys.argv)
win=Video()
win.show()
app.exec_()
profile
rust로 뭐할까

0개의 댓글