첫 번째 처리 된 이미지 (예 : Canny Filter)의 출력을 다른 프로세스 필터에 입력하려면 어떻게해야합니까?

베드로

원본 이미지에 필터를 적용하는 대신 첫 번째 처리 된 이미지 (예 : Canny Filter)의 출력을 다른 프로세스 / 필터 (예 : Sobel Filter)에 입력하려면 어떻게해야합니까?

필터를 적용하기 전에

필터 적용 후

다른 프로세스를 적용 할 때마다 원본 이미지 (첫 번째 프로세스 / 필터의 출력이 아님)에서 다시 수행 됩니다 .

암호:-

import sys
import numpy as np
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import cv2

class Ui_MainWindow(QMainWindow):
    global img

    def __init__(self):
        super().__init__()

        self.init_Main_Ui()
        self.init_Menu_Ui()

    def init_Main_Ui(self):
        self.setObjectName("test")
        self.setEnabled(True)
        self.resize(1200, 700)
        self.setMinimumSize(QtCore.QSize(500, 300))
        self.setMaximumSize(QtCore.QSize(500, 300))

        self.image_label = QLabel(self)

        self.setCentralWidget(self.image_label)

        self.show()

    def init_Menu_Ui(self):
        global img
        menu_bar = self.menuBar()
        menu_bar.setNativeMenuBar(False)

        file_menu = menu_bar.addMenu('&File')  # &가 alt+F 메뉴 단축키 지정

        Exit_action = QAction('Exit', self)
        Exit_action.setShortcut('Ctrl+Q')
        Exit_action.triggered.connect(qApp.quit)

        Open_action = QAction('open', self)
        Open_action.setShortcut('Ctrl+O')
        Open_action.triggered.connect(self.read_file)

        file_menu.addAction(Open_action)
        file_menu.addAction(Exit_action)

        self.filter_menu = menu_bar.addMenu("&Filter")
        self.filter_menu.setEnabled(False)

        Sobel_action = QAction('Sobel filter', self)
        Sobel_action.setShortcut('Alt+1')
        Sobel_action.triggered.connect(
            lambda: self.Sobel_filter(img)
        )

        Prewitt_action = QAction('Prewitt filter', self)
        Prewitt_action.setShortcut('Alt+2')
        Prewitt_action.triggered.connect(
            lambda : self.Prewitt_filter(img)
        )

        Gaussian_action = QAction('Gaussian filter', self)
        Gaussian_action.setShortcut('Alt+3')
        Gaussian_action.triggered.connect(
            lambda : self.Gaussian_filter(img)
        )


        Canny_action = QAction('Canny filter', self)
        Canny_action.setShortcut('Alt+4')
        Canny_action.triggered.connect(
            lambda : self.Canny_filter(img)
        )

        LoG_action = QAction('LoG filter', self)
        LoG_action.setShortcut('Alt+5')
        LoG_action.triggered.connect(
            lambda : self.LoG_filter(img)
        )
        
        self.setWindowTitle('Image Processing')
        self.filter_menu.addAction(Sobel_action)
        self.filter_menu.addAction(Prewitt_action)
        self.filter_menu.addAction(Gaussian_action)

    def read_file(self):
        global img
        file_name = QFileDialog.getOpenFileName(self)

        if file_name[0] is not '':
            img0 = cv2.imread(file_name[0])

            img = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB)

            self.reshow_image(img)
            print('aa')
            self.filter_menu.setEnabled(True)
        else:
            print('please put img')

    def save_image(self):
        print("save")

    def Sobel_filter(self, img):
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

        sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
        sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

        sobel = img.copy()
        height = np.size(img, 0)
        width = np.size(img, 1)

        for i in range(width):
            for j in range(height):
                sobel[j, i] = np.minimum(255, np.round(np.sqrt(sobelx[j, i] * sobelx[j, i] + sobely[j, i] * sobely[j, i])))

        sobel = cv2.cvtColor(sobel, cv2.COLOR_GRAY2RGB)
        cv2.imwrite("Sobel Filtered Image.png", sobel)
        self.reshow_image(sobel)


    def Prewitt_filter(self, img):
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

        kernelx = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])
        kernely = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])

        img_prewittx = cv2.filter2D(img, -1, kernelx)
        img_prewitty = cv2.filter2D(img, -1, kernely)

        Prewitt = cv2.cvtColor(img_prewittx + img_prewitty, cv2.COLOR_GRAY2RGB)
        self.reshow_image(Prewitt)


    def Gaussian_filter(self, img):
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        img_smooth = cv2.GaussianBlur(img, (3, 3), 0)
        img_smooth = cv2.cvtColor(img_smooth, cv2.COLOR_GRAY2RGB)
        self.reshow_image(img_smooth)

    def reshow_image(self, cv_img):
        if cv_img is not None:
            self.image_label.resize(cv_img.shape[1], cv_img.shape[0])
            Q_img = QImage(cv_img.data, cv_img.shape[1], cv_img.shape[0], cv_img.shape[1] * 3, QImage.Format_RGB888)
            self.image_label.setPixmap(QPixmap.fromImage(Q_img))
        else:
            print("Image load failed")


    def exit(self):
        cv2.waitKey(0)
        cv2.destroyAllWindows()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_MainWindow()
    sys.exit(app.exec_())
쉬운

솔직히 왜 문제가 있는지 이해가 안 돼요.

모든 필터는 이미지를 전역 / 클래스 변수에 할당해야합니다. self.current_img

def Gaussian_filter(self, img):

    # ... code ...

    self.current_img = img_smooth
    self.reshow_image(self.current_img)

def Sobel_filter(self, img):

    # ... code ...

    self.current_img = sobel
    self.reshow_image(self.current_img)


def Prewitt_filter(self, img):

    # ... code ...

    self.current_img = Prewitt
    self.reshow_image(self.current_img)

모든 필터는이 변수의 이미지를 사용해야합니다. self.current_img

    Canny_action.triggered.connect(
        lambda : self.Canny_filter(self.current_img)
    )

    Sobel_action.triggered.connect(
        lambda: self.Sobel_filter(self.current_img)
    )

    Prewitt_action.triggered.connect(
        lambda : self.Prewitt_filter(self.current_img)
    )

    Gaussian_action.triggered.connect(
        lambda : self.Gaussian_filter(self.current_img)
    )

    Canny_action.triggered.connect(
        lambda : self.Canny_filter(self.current_img)
    )

    LoG_action.triggered.connect(
        lambda : self.LoG_filter(self.current_img)
    )

그리고 처음 읽을 때도 마찬가지입니다.

    if file_name[0] != '': # is not '':
        img = cv2.imread(file_name[0])

        self.current_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.original_img = self.current_img.copy()

        self.reshow_image(self.current_img)

I also used self.original_img to keep original image if I would like to start with original image.


EDIT

With class variable self.current_img you could even use it directly in methods - without sending as argument.

Canny_action.triggered.connect(self.Canny_filter)
Sobel_action.triggered.connect(self.Sobel_filter)
Prewitt_action.triggered.connect(self.Prewitt_filter)
Gaussian_action.triggered.connect(self.Gaussian_filter)
Canny_action.triggered.connect(self.Canny_filter)
LoG_action.triggered.connect(self.LoG_filter)

def Gaussian_filter(self):

    img = self.current_img    

    # ... code ...

    self.current_img = img_smooth
    self.reshow_image(self.current_img)

def Sobel_filter(self):

    img = self.current_img    

    # ... code ...

    self.current_img = sobel
    self.reshow_image(self.current_img)


def Prewitt_filter(self):

    img = self.current_img    

    # ... code ...

    self.current_img = Prewitt
    self.reshow_image(self.current_img)

BTW:

def reset_to_original_image(self):

    self.current_img = self.original_img.copy()

    self.reshow_image(self.current_img)

BTW:

you could even create list with history of all images

def Sobel_filter(self):

    self.history.append(self.current_img.copy())
 
    img = self.current_img    

    # ... code ...

    self.current_img = sobel
    self.reshow_image(self.current_img)

EDIT:

Full working code.

Filters work on result from previous filter.

You have also function Reset to original image.

All images are remeber in history and you have function Undo to go back to previous image. You can even undo Reset.

import sys
import numpy as np
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import cv2

class Ui_MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()

        self.init_Main_Ui()
        self.init_Menu_Ui()

        self.current_img = None  # default value at start
        self.original_img = None # default value at start

        self.history = []

    def init_Main_Ui(self):
        self.setObjectName("test")
        self.setEnabled(True)
        self.resize(1200, 700)
        self.setMinimumSize(QtCore.QSize(500, 300))
        self.setMaximumSize(QtCore.QSize(500, 300))

        self.image_label = QLabel(self)

        self.setCentralWidget(self.image_label)

        self.show()

    def init_Menu_Ui(self):

        menu_bar = self.menuBar()
        menu_bar.setNativeMenuBar(False)

        file_menu = menu_bar.addMenu('&File')  # &가 alt+F 메뉴 단축키 지정

        Exit_action = QAction('Exit', self)
        Exit_action.setShortcut('Ctrl+Q')
        Exit_action.triggered.connect(qApp.quit)

        Open_action = QAction('open', self)
        Open_action.setShortcut('Ctrl+O')
        Open_action.triggered.connect(self.read_file)

        file_menu.addAction(Open_action)
        file_menu.addAction(Exit_action)

        self.filter_menu = menu_bar.addMenu("&Filter")
        self.filter_menu.setEnabled(False)

        Reset_action = QAction('Reset to original image', self)
        Reset_action.setShortcut('Alt+0')
        Reset_action.triggered.connect(self.reset_to_original_image)

        Sobel_action = QAction('Sobel filter', self)
        Sobel_action.setShortcut('Alt+1')
        Sobel_action.triggered.connect(self.Sobel_filter)

        Prewitt_action = QAction('Prewitt filter', self)
        Prewitt_action.setShortcut('Alt+2')
        Prewitt_action.triggered.connect(self.Prewitt_filter)

        Gaussian_action = QAction('Gaussian filter', self)
        Gaussian_action.setShortcut('Alt+3')
        Gaussian_action.triggered.connect(self.Gaussian_filter)

        Canny_action = QAction('Canny filter', self)
        Canny_action.setShortcut('Alt+4')
        Canny_action.triggered.connect(self.Canny_filter)  # filter has to exist

        LoG_action = QAction('LoG filter', self)
        LoG_action.setShortcut('Alt+5')
        LoG_action.triggered.connect(self.LoG_filter)  # filter has to exist

        Undo_action = QAction('Undo filter', self)
        Undo_action.setShortcut('Alt+X')
        Undo_action.triggered.connect(self.Undo_filter)  # filter has to exist
        
        self.setWindowTitle('Image Processing')
        self.filter_menu.addAction(Reset_action)
        self.filter_menu.addAction(Sobel_action)
        self.filter_menu.addAction(Prewitt_action)
        self.filter_menu.addAction(Gaussian_action)

        self.filter_menu.addAction(Undo_action)

    def read_file(self):
        file_name = QFileDialog.getOpenFileName(self)

        if file_name[0] != '': # is not '':
            img = cv2.imread(file_name[0])

            self.original_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            self.current_img = self.original_img.copy()

            self.reshow_image(self.current_img)
            self.filter_menu.setEnabled(True)
        else:
            print('please put img')

    def save_image(self):
        print("save")

    def reset_to_original_image(self):
        self.history.append(self.current_img.copy())
        self.current_img = self.original_img.copy()
        self.reshow_image(self.current_img)

    def Sobel_filter(self):

        self.history.append(self.current_img.copy())

        img = self.current_img

        cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

        sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
        sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

        sobel = img.copy()
        height = np.size(img, 0)
        width = np.size(img, 1)

        for i in range(width):
            for j in range(height):
                sobel[j, i] = np.minimum(255, np.round(np.sqrt(sobelx[j, i] * sobelx[j, i] + sobely[j, i] * sobely[j, i])))

        sobel = cv2.cvtColor(sobel, cv2.COLOR_GRAY2RGB)
        cv2.imwrite("Sobel Filtered Image.png", sobel)

        self.current_img = sobel
        self.reshow_image(self.current_img)

    def Prewitt_filter(self):

        self.history.append(self.current_img.copy())

        img = self.current_img

        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

        kernelx = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])
        kernely = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])

        img_prewittx = cv2.filter2D(img, -1, kernelx)
        img_prewitty = cv2.filter2D(img, -1, kernely)

        Prewitt = cv2.cvtColor(img_prewittx + img_prewitty, cv2.COLOR_GRAY2RGB)

        self.current_img = Prewitt
        self.reshow_image(self.current_img)

    def Gaussian_filter(self):

        self.history.append(self.current_img.copy())

        img = self.current_img

        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        img_smooth = cv2.GaussianBlur(img, (3, 3), 0)
        img_smooth = cv2.cvtColor(img_smooth, cv2.COLOR_GRAY2RGB)

        self.current_img = img_smooth
        self.reshow_image(self.current_img)

    def Canny_filter(self):
        print("TODO: create Canny_filter")
        #self.history.append(self.current_img.copy())
        #img = self.current_img
        # ... code ... 
        #self.current_img = img_smooth
        #self.reshow_image(self.current_img)

    def LoG_filter(self):
        print("TODO: create LoG_filter")
        #self.history.append(self.current_img.copy())
        #img = self.current_img
        # ... code ... 
        #self.current_img = img_smooth
        #self.reshow_image(self.current_img)

    def Undo_filter(self):
        if self.history:
            self.current_img = self.history.pop(-1)
            self.reshow_image(self.current_img)

    def reshow_image(self, cv_img):
        if cv_img is not None:
            self.image_label.resize(cv_img.shape[1], cv_img.shape[0])
            Q_img = QImage(cv_img.data, cv_img.shape[1], cv_img.shape[0], cv_img.shape[1] * 3, QImage.Format_RGB888)
            self.image_label.setPixmap(QPixmap.fromImage(Q_img))
        else:
            print("Image load failed")


    def exit(self):
        cv2.waitKey(0)
        cv2.destroyAllWindows()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui_MainWindow()
    sys.exit(app.exec_())

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관