python 图像分割_Python中的图像分割(第2部分)

本文深入探讨了Python中用于图像分割的技术,结合opencv库,涵盖了计算机视觉和机器学习的原理,特别是深度学习在图像分割中的应用。
摘要由CSDN通过智能技术生成

python 图像分割

Stuck behind the paywall? Click here to read the full article with my friend link.

卡在收费墙后面? 单击 此处 以阅读完整的文章以及我的朋友链接。

Welcome back!

欢迎回来!

This is the second part of a three part series on image classification. I recommend you to first go through Part 1 of the series if you haven’t already (link below). I’ve gone through the details of setting up the environment and working with image data from Google Drive in Google Colab there. We’ll be using output from that code here.

这是关于图像分类的三部分系列的第二部分。 我建议您先阅读本系列的第1部分(如果尚未使用)(下面的链接)。 我已经详细介绍了设置环境以及使用Google Colab中的Google云端硬盘中的图像数据的详细信息。 我们将在这里使用该代码的输出。

Image segmentation is the process of “partitioning a digital image into multiple segments”. Since we are just concerned about background removal here, we will just be dividing the images into the foreground and the background.

图像分割是“将数字图像分割成多个片段”的过程。 由于我们在这里只关心背景去除,因此我们将图像分为前景和背景。

This consists of five basic steps:

这包括五个基本步骤:

  1. Convert the image to grayscale.

    将图像转换为灰度。
  2. Apply thresholding to the image.

    对图像应用阈值处理。
  3. Find the image contours (edges).

    找到图像轮廓(边缘)。
  4. Create a mask using the largest contour.

    使用最大轮廓创建蒙版。
  5. Apply the mask on the original image to remove the background.

    在原始图像上应用遮罩以删除背景。

I’ll be explaining and coding each step. Onward!

我将解释和编码每个步骤。 向前!

设置工作区 (Setting Up the Workspace)

If you have gone through Part I and have executed the code till the end, you can skip this section.

如果您已阅读了第一部分并执行了代码直到最后,则可以跳过本节。

For those who haven’t, and are here just to learn image segmentation, I’m assuming that you know how Colab works. In case you don’t please go through Part I.

对于那些还没有来这里学习图像分割的人,我假设您知道Colab的工作原理。 如果您不满意,请阅读第一部分

The data set is available here. This is the result of the code from Part I. Open the link while signed in to your Google account so that it’s available in the “Shared with me” folder of your Google Drive. Then open Google Colab, connect to a runtime, and mount your Google Drive to it:

数据集可在此处获得 。 这是第一部分代码的结果。登录到您的Google帐户后,打开链接,以便可以在Google云端硬盘的“与我共享”文件夹中使用该链接。 然后打开Google Colab ,连接到运行时,然后将Google云端硬盘安装到该运行时:

Image for post

Follow the URL, select the Google account which you used to access the data-set, and grant Colab permissions to your Drive. Paste the authorization code at the text box in the cell output and you’ll get the message Mounted at /gdrive.

跟随该URL,选择用于访问数据集的Google帐户,然后将Colab权限授予您的云端硬盘。 将授权代码粘贴到单元输出的文本框中,您将收到消息Mounted at /gdrive

Then we import all the necessary libraries:

然后,我们导入所有必需的库:

import cv2
import glob
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import random
from tqdm.notebook import tqdmnp.random.seed(1)

Our notebook is now set up!

现在我们的笔记本已经设置好了!

从驱动器读取图像 (Reading Images from Drive)

If you’re using data from the link shared in this article, the path for you will be ‘/gdrive/Shared with me/LeafImages/color/Grape*/*.JPG’.

如果您使用的是本文共享链接中的数据,则您的路径将为'/gdrive/ Shared with me /Lea

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值