ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用GrabCut做分割

2021-03-04 11:04:58  阅读:185  来源: 互联网

标签:src 分割 int GrabCut self fd 使用 np cv


主要完成了界面化设计,代码如下

import cv2 as cv
import numpy as np
import sys
from PyQt5.Qt import *
class MyWediget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("分割烟草")
        self.setWindowIcon(QIcon("2.jpg"))
        self.setFixedSize(800, 430)
        self.setup_ui()
        self.arg = ""
    def setup_ui(self):
        def test():
            fd = QFileDialog(window, "选择一个文件", "../", "All(*.*);;Images(*.png *.jpg);;Python文件(*.py)")
            fd.move(200,0)
            fd.setLabelText(QFileDialog.FileName, "我的文件")
            fd.setLabelText(QFileDialog.Accept, "接受")

            fd.setLabelText(QFileDialog.Reject, "拒绝")
            fd.setFileMode(QFileDialog.ExistingFiles)
            def path(val):
                inputPath.setText(val)
                self.segmentation(val)
            fd.fileSelected.connect(path)
            fd.open()
        inputPath = QLineEdit(self)
        inputPath.resize(300,30)
        btn = QPushButton(self)
        btn.setText("选择文件")
        def click():
            btn.isDefault()
        if len(inputPath.text()) > 0:
            evt = QKeyEvent()
            if evt.modifiers() == Qt.EnterKeyGo:
                btn.pressed.connect(click)
        btn.move(300,0)
        btn.clicked.connect(test)

    def segmentation(self,val):
        windows = QWidget(self)
        windows.resize(400,400)
        windows.move(0,30)

        w2 = QWidget(self)
        w2.move(400,30)
        w2.resize(400,400)

        self.arg = val
        inputPath = QLineEdit(self)
        inputPath.setText(self.arg)

        src = cv.imread(self.arg)
        print(src.shape)
        w = int(src.shape[1])
        h = int(src.shape[0])

        if w>1000 or h>800:
            w = int(w/2)
            h = int(h/2)
        # print(w+"  "+h)
        src = cv.resize(src, (w, h), interpolation=cv.INTER_CUBIC)
        r = cv.selectROI('Draw a rectangle', src, False)  # 返回 (x_min, y_min, w, h)\

        roi = src[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])]

        # 原图mask
        mask = np.zeros(src.shape[:2], dtype=np.uint8)

        # 矩形roi
        rect = (int(r[0]), int(r[1]), int(r[2]), int(r[3]))  # 包括前景的矩形,格式为(x,y,w,h)

        bgdmodel = np.zeros((1, 65), np.float64)  # bg模型的临时数组
        fgdmodel = np.zeros((1, 65), np.float64)  # fg模型的临时数组

        cv.grabCut(src, mask, rect, bgdmodel, fgdmodel, 11, mode=cv.GC_INIT_WITH_RECT)

        # 提取前景和可能的前景区域
        mask2 = np.where((mask == 1) + (mask == 3), 255, 0).astype('uint8')

        print(mask2.shape)

        result = cv.bitwise_and(src, src, mask=mask2)
        cv.imwrite('result.jpg', result)
        cv.imwrite('roi.jpg', roi)

        print("over")
        w2.setStyleSheet("border-image:url(roi.jpg)")
        windows.setStyleSheet("border-image:url(result.jpg)")

        w2.show()
        windows.show()




if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWediget()
    window.show()
    sys.exit(app.exec_())  

第一、选择图像文件

第二、画出矩形框

第三、回车得到分割结果

如果想要生成exe文件,就需要配置.spec文件。请访问我的github。

标签:src,分割,int,GrabCut,self,fd,使用,np,cv
来源: https://www.cnblogs.com/peixu/p/14478982.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有