Kiểu dữ liệu của hình ảnh trong python là gì?

Recognise morphometric problems (those dealing with the number, size, or shape of the objects in an image)

Show

As computer systems have become faster and more powerful, and cameras and other imaging systems have become commonplace in many other areas of life, the need has grown for researchers to be able to process and analyse image data. Considering the large volumes of data that can be involved - high-resolution images that take up a lot of disk space/virtual memory, and/or collections of many images that must be processed together - and the time-consuming and error-prone nature of manual processing, it can be advantageous or even necessary for this processing and analysis to be automated as a computer program

This lesson introduces an open source toolkit for processing image data. the Python programming language and the scikit-image (

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32) library. Với thiết kế thử nghiệm cẩn thận, mã Python có thể là một công cụ mạnh mẽ để trả lời nhiều loại câu hỏi khác nhau

Uses of Image Processing in Research

Automated processing can be used to analyse many different properties of an image, including the distribution and change in colours in the image, the number, size, position, orientation, and shape of objects in the image, and even - when combined with machine learning techniques for object recognition - the type of objects in the image

Some examples of image processing methods applied in research include

  • imaging a Black Hole
  • estimating the population of Emperor Penguins
  • the global-scale analysis of marine plankton diversity
  • segmentation of liver and vessels from CT images

With this lesson, we aim to provide a thorough grounding in the fundamental concepts and skills of working with image data in Python. Most of the examples used in this lesson focus on one particular class of image processing technique, morphometrics, but what you will learn can be used to solve a much wider range of problems

Morphometrics

Morphometrics involves counting the number of objects in an image, analyzing the size of the objects, or analyzing the shape of the objects. For example, we might be interested in automatically counting the number of bacterial colonies growing in a Petri dish, as shown in this image

Kiểu dữ liệu của hình ảnh trong python là gì?

Chúng tôi có thể sử dụng xử lý hình ảnh để tìm các khuẩn lạc, đếm chúng và sau đó đánh dấu vị trí của chúng trên ảnh gốc, tạo ra một ảnh như thế này

Kiểu dữ liệu của hình ảnh trong python là gì?

Why write a program to do that?

Note that you can easily manually count the number of bacteria colonies shown in the morphometric example above. Why should we learn how to write a Python program to do a task we could easily perform with our own eyes? There are at least two reasons to learn how to perform tasks like these with Python and skimage

  1. What if there are many more bacteria colonies in the Petri dish? For example, suppose the image looked like this

    Kiểu dữ liệu của hình ảnh trong python là gì?

    Manually counting the colonies in that image would present more of a challenge. Một chương trình Python sử dụng skige có thể đếm số lượng thuộc địa chính xác hơn và nhanh hơn nhiều so với con người có thể

  2. Điều gì sẽ xảy ra nếu bạn có hàng trăm hoặc hàng nghìn hình ảnh cần xem xét? . Một chương trình Python sử dụng dữ liệu lướt có thể di chuyển qua tất cả các hình ảnh trong vài giây;

As you can see, the simple image processing / computer vision techniques you will learn during this workshop can be very valuable tools for scientific research

As we move through this workshop, we will learn image analysis methods useful for many different scientific problems. These will be linked together and applied to a real problem in the final end-of-workshop capstone challenge

Let’s get started, by learning some basics about how images are represented and stored digitally

Key Points

  • Có thể sử dụng các kỹ thuật Python và lướt (scikit-image) đơn giản để giải quyết các vấn đề phân tích hình ảnh chính hãng

  • Morphometric problems involve the number, shape, and / or size of the objects in an image


Khái niệm cơ bản về hình ảnh

Tổng quan

Dạy học. 20 min
Exercises. 5 min

Questions

  • Hình ảnh được thể hiện ở định dạng kỹ thuật số như thế nào?

Objectives

  • Define the terms bit, byte, kilobyte, megabyte, etc

  • Explain how a digital image is composed of pixels

  • Đề nghị sử dụng imageio (resp. skimage) for I/O (resp. image processing) tasks

  • Explain how images are stored in NumPy arrays

  • Explain the left-hand coordinate system used in digital images

  • Explain the RGB additive colour model used in digital images

  • Explain the order of the three colour values in skimage images

  • Giải thích các đặc điểm của định dạng ảnh BMP, JPEG và TIFF

  • Explain the difference between lossy and lossless compression

  • Explain the advantages and disadvantages of compressed image formats

  • Giải thích thông tin nào có thể chứa trong siêu dữ liệu hình ảnh

The images we see on hard copy, view with our electronic devices, or process with our programs are represented and stored in the computer as numeric abstractions, approximations of what we see with our eyes in the real world. Trước khi bắt đầu tìm hiểu cách xử lý hình ảnh bằng các chương trình Python, chúng ta cần dành thời gian để hiểu cách thức hoạt động của các trừu tượng này

Pixels

Điều quan trọng là phải nhận ra rằng hình ảnh được lưu trữ dưới dạng mảng hình chữ nhật gồm hàng trăm, hàng nghìn hoặc hàng triệu “phần tử hình ảnh” rời rạc, còn được gọi là pixel. Mỗi pixel có thể được coi là một điểm vuông duy nhất của ánh sáng màu

Ví dụ: hãy xem xét hình ảnh cây ngô con này, với diện tích hình vuông được chỉ định bởi hộp màu đỏ

Kiểu dữ liệu của hình ảnh trong python là gì?

Now, if we zoomed in close enough to see the pixels in the red box, we would see something like this

Kiểu dữ liệu của hình ảnh trong python là gì?

Lưu ý rằng mỗi ô vuông trong vùng hình ảnh phóng to - mỗi pixel - đều có một màu, nhưng mỗi pixel có thể có màu khác với các pixel lân cận. Nhìn từ xa, những pixel này dường như hòa trộn với nhau để tạo thành hình ảnh mà chúng ta thấy

Làm việc với Pixel

Như đã lưu ý, trong thực tế, các hình ảnh trong thế giới thực thường được tạo thành từ một số lượng lớn các pixel và mỗi pixel này sẽ là một trong số hàng triệu màu tiềm ẩn. Mặc dù chúng ta sẽ sớm xử lý các bức ảnh có độ phức tạp như vậy, nhưng hãy bắt đầu khám phá với 15 pixel trong ma trận 5 X 3 với 2 màu và tiến tới mức độ phức tạp đó

Ma trận, mảng, hình ảnh và pixel

Ma trận là khái niệm toán học - các số được sắp xếp đều nhau trong một hình chữ nhật. Đây có thể là một hình chữ nhật hai chiều, giống như hình dạng của màn hình mà bạn đang xem bây giờ. Hoặc nó có thể là một hình tương đương ba chiều, một hình khối hoặc thậm chí có nhiều chiều hơn, nhưng luôn giữ cách sắp xếp các số cách đều nhau. Trong điện toán, mảng đề cập đến một cấu trúc trong bộ nhớ của máy tính, nơi dữ liệu được lưu trữ trong các phần tử có khoảng cách đều nhau. Điều này rất giống với một ma trận. Mảng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
33 là một loại biến (một ví dụ đơn giản hơn về loại là một số nguyên). Đối với mục đích của chúng tôi, sự khác biệt giữa ma trận và mảng không quan trọng, chúng tôi không thực sự quan tâm cách máy tính sắp xếp dữ liệu của chúng tôi trong bộ nhớ của nó. Điều quan trọng là máy tính lưu trữ các giá trị mô tả các pixel trong ảnh, dưới dạng các mảng. Và thuật ngữ ma trận và mảng có thể được sử dụng thay thế cho nhau

Đầu tiên, nhập khẩu cần thiết

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage

Import Statements in Python

Trong Python, câu lệnh

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
34 được sử dụng để tải chức năng bổ sung vào chương trình. Điều này là cần thiết khi chúng tôi muốn mã của mình thực hiện điều gì đó chuyên biệt hơn, điều không thể dễ dàng đạt được với bộ công cụ cơ bản và cấu trúc dữ liệu hạn chế có sẵn trong môi trường Python mặc định

Additional functionality can be loaded as a single function or object, a module defining several of these, or a library containing many modules. Bạn sẽ gặp một số dạng khác nhau của câu lệnh

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
34

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np

Giải thích thêm

In the example above, form 1 loads the entire

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32 library into the program as an object. Các mô-đun riêng lẻ của thư viện sau đó có sẵn trong đối tượng đó, e. g. , to access the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
37 function used in the drawing episode, you would write
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
38

Form 2 loads only the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
39 module of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32 into the program. When we run the code, the program will take less time and use less memory because we will not load the whole
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32 library. The syntax needed to use the module remains unchanged. để truy cập hàm
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
37, chúng ta sẽ sử dụng lệnh gọi hàm tương tự như đã cho ở dạng 1

To further reduce the time and memory requirements for your program, form 3 can be used to import only a specific function/class from a library/module. Unlike the other forms, when this approach is used, the imported function or class can be called by its name only, without prefixing it with the name of the module/library from which it was loaded, i. e. ,

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
43 thay vì
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
38 sử dụng ví dụ trên. Một mối nguy hiểm của hình thức này là việc nhập như thế này sẽ ghi đè lên bất kỳ đối tượng nào có cùng tên đã được xác định/nhập trước đó trong chương trình, tôi. e. , the example above would replace any existing object called
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
37 with the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
37 function from
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
47

Cuối cùng, từ khóa

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
48 có thể được sử dụng khi nhập, để xác định tên được sử dụng làm tốc ký cho thư viện/mô-đun được nhập. This name is referred to as an alias. Typically, using an alias (such as
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
49 for the NumPy library) saves us a little typing. You may see
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
48 combined with any of the other first three forms of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
34 statement

Which form is used often depends on the size and number of additional tools being loaded into the program

Now that we have our libraries loaded, we will run a Jupyter Magic Command that will ensure our images display in our Jupyter document with pixel information that will help us more efficiently run commands later in the session

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
2

With that taken care of, let’s load our image data from disk using the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
52 function from the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53 module and display it using the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
54 function from the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
55 module.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56 is a Python library for reading and writing image data.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53 is specifying that we want to use version 3 of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56. Phiên bản này có lợi ích là hỗ trợ dữ liệu hình ảnh nD (đa chiều) nguyên bản (nghĩ về tập, phim)

Tại sao không sử dụng import skimage # form 1, load whole skimage library import skimage.draw # form 2, load skimage.draw module only from skimage.draw import disk # form 3, load only the disk function import numpy as np # form 4, load all of numpy into an object called np 59

Thư viện

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32 có chức năng riêng để đọc một hình ảnh, vì vậy bạn có thể hỏi tại sao chúng tôi không sử dụng nó ở đây. Actually,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
59 uses
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62 internally when loading an image into Python. It is certainly something you may use as you see fit in your own code. Trong bài học này, chúng ta sử dụng thư viện
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56 để đọc hoặc ghi (lưu) ảnh, còn
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32 dùng để thực hiện các thao tác trên ảnh. Using
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56 gives us more flexibility, especially when it comes to handling metadata

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
7

Kiểu dữ liệu của hình ảnh trong python là gì?

Bạn có thể đang nghĩ, “Cái đó trông hơi giống số tám, và tôi thấy hai màu nhưng làm sao nó chỉ có 15 pixel được”. The display of the eight you see does use a lot more screen pixels to display our eight so large, but that does not mean there is information for all those screen pixels in the file. All those extra pixels are a consequence of our viewer creating additional pixels through interpolation. It could have just displayed it as a tiny image using only 15 screen pixels if the viewer was designed differently

While many image file formats contain descriptive metadata that can be essential, the bulk of a picture file is just arrays of numeric information that, when interpreted according to a certain rule set, become recognizable as an image to us. Our image of an eight is no exception, and

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53 stored that image data in an array of arrays making a 5 x 3 matrix of 15 pixels. We can demonstrate that by calling on the shape property of our image variable and see the matrix by printing our image variable to the screen

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
9

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
0

Vì vậy, nếu chúng ta có các công cụ cho phép chúng ta thao tác với các dãy số này, chúng ta có thể thao tác với hình ảnh. Thư viện

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
33 có thể đặc biệt hữu ích ở đây, vì vậy hãy thử điều đó bằng cách sử dụng kỹ thuật cắt mảng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
33. Notice that the default behavior of the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
54 function appended row and column numbers that will be helpful to us as we try to address individual or groups of pixels. First let’s load another copy of our eight, and then make it look like a zero

To make it look like a zero, we need to change the number underlying the centremost pixel to be 1. With the help of those row and column headers, at this small scale we can determine the centre pixel is in row labeled 2 and column labeled 1. Using array slicing, we can then address and assign a new value to that position

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
4

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
5

Kiểu dữ liệu của hình ảnh trong python là gì?

Coordinate system

When we process images, we can access, examine, and / or change the colour of any pixel we wish. To do this, we need some convention on how to access pixels individually; a way to give each one a name, or an address of a sort

The most common manner to do this, and the one we will use in our programs, is to assign a modified Cartesian coordinate system to the image. The coordinate system we usually see in mathematics has a horizontal x-axis and a vertical y-axis, like this

Kiểu dữ liệu của hình ảnh trong python là gì?

The modified coordinate system used for our images will have only positive coordinates, the origin will be in the upper left corner instead of the centre, and y coordinate values will get larger as they go down instead of up, like this

Kiểu dữ liệu của hình ảnh trong python là gì?

Đây được gọi là hệ tọa độ bên trái. If you hold your left hand in front of your face and point your thumb at the floor, your extended index finger will correspond to the x-axis while your thumb represents the y-axis

Kiểu dữ liệu của hình ảnh trong python là gì?

Cho đến khi bạn đã làm việc với hình ảnh một thời gian, lỗi phổ biến nhất mà bạn mắc phải với tọa độ là quên rằng tọa độ y lớn hơn khi chúng đi xuống thay vì đi lên như trong một hệ tọa độ Descartes thông thường. Do đó, có thể hữu ích khi nghĩ về việc đếm ngược các hàng (r) cho trục y và trên các cột (c) cho trục x. This can be especially helpful in cases where you need to transpose image viewer data provided in x,y format to y,x format. Do đó, chúng tôi sẽ sử dụng cx và ry khi thích hợp để giúp kết nối hai cách tiếp cận này

Changing Pixel Values (5 min)

Tải một bản sao khác của tám tên là năm, sau đó thay đổi giá trị của pixel để bạn có thứ trông giống như số 5 thay vì số 8. Display the image and print out the matrix as well

Solution

There are many possible solutions, but one method would be . .

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
6

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
7

Kiểu dữ liệu của hình ảnh trong python là gì?

More colours

Up to now, we only had a 2 colour matrix, but we can have more if we use other numbers or fractions. One common way is to use the numbers between 0 and 255 to allow for 256 different colours or 256 different levels of grey. Hãy thử điều đó

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
0

Kiểu dữ liệu của hình ảnh trong python là gì?

We now have 3 colours, but are they the three colours you expected? They all appear to be on a continuum of dark purple on the low end and yellow on the high end. This is a consequence of the default colour map (cmap) in this library. You can think of a colour map as an association or mapping of numbers to a specific colour. Tuy nhiên, mục tiêu ở đây không phải là có một số cho mọi màu có thể, mà là có một chuỗi màu liên tục thể hiện cường độ tương đối. In our specific case here for example, 255 or the highest intensity is mapped to yellow, and 0 or the lowest intensity is mapped to a dark purple. Bản đồ màu tốt nhất cho dữ liệu của bạn sẽ khác nhau và có nhiều tùy chọn được tích hợp sẵn, nhưng lựa chọn mặc định này không tùy ý. A lot of science went into making this the default due to its robustness when it comes to how the human mind interprets relative colour values, grey-scale printability, and colour-blind friendliness (You can read more about this default colour map in a Matplotlib tutorial and an explanatory article by the authors). Thus it is a good place to start, and you should change it only with purpose and forethought. For now, let’s see how you can do that using an alternative map you have likely seen before where it will be even easier to see it as a mapped continuum of intensities. thang độ xám

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
1

Kiểu dữ liệu của hình ảnh trong python là gì?

Ở trên, chúng tôi có chính xác cùng một ma trận dữ liệu cơ bản, nhưng ở thang độ xám. Zero maps to black, 255 maps to white, and 128 maps to medium grey. Ở đây, chúng tôi chỉ có một kênh duy nhất trong dữ liệu và sử dụng bản đồ màu thang độ xám để biểu thị độ chói hoặc cường độ của dữ liệu và tương ứng, kênh này được gọi là kênh độ sáng

Even More Colours

Đây là tất cả tốt và tốt ở quy mô này, nhưng điều gì sẽ xảy ra khi thay vào đó chúng ta có một bức tranh phong cảnh thiên nhiên chứa hàng triệu màu sắc. Có một ánh xạ 1-1 của số thành màu như thế này sẽ không hiệu quả và việc điều chỉnh cũng như xây dựng các công cụ để thực hiện điều đó rất khó khăn. Thay vì số lớn hơn, giải pháp là có nhiều số hơn ở nhiều chiều hơn. Storing the numbers in a multi-dimensional matrix where each colour or property like transparency is associated with its own dimension allows for individual contributions to a pixel to be adjusted independently. This ability to manipulate properties of groups of pixels separately will be key to certain techniques explored in later chapters of this lesson. To get started let’s see an example of how different dimensions of information combine to produce a set of pixels using a 4 X 4 matrix with 3 dimensions for the colours red, green, and blue. Thay vì tải nó từ một tệp, chúng tôi sẽ tạo ví dụ này bằng cách sử dụng numpy

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
2

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
3

Kiểu dữ liệu của hình ảnh trong python là gì?

Trước đây, chúng tôi có một số được ánh xạ tới một màu hoặc cường độ. Bây giờ chúng tôi đang kết hợp hiệu ứng của 3 số để đạt được một giá trị màu duy nhất. Hãy xem một ví dụ về điều đó bằng cách sử dụng hình vuông màu xanh lam ở cuối hàng thứ hai, có chỉ số [1, 3]

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
4

kết quả này. array([ 7, 1, 110]) The integers in order represent Red, Green, and Blue. Nhìn vào 3 giá trị và biết cách chúng ánh xạ, có thể giúp chúng tôi hiểu tại sao nó có màu xanh lam. Nếu chúng ta chia mỗi giá trị cho 255, giá trị lớn nhất, thì chúng ta có thể xác định giá trị đó đang đóng góp bao nhiêu so với tiềm năng tối đa của nó. Thực tế, màu đỏ là 7/255 hoặc 2. 8 percent of its potential, the green is at 1/255 or 0. 4 phần trăm và màu xanh lam là 110/255 hoặc 43. 1 phần trăm tiềm năng của nó. Vì vậy, khi bạn trộn ba cường độ màu đó, màu xanh lam sẽ thắng với biên độ rộng, nhưng màu đỏ và xanh lục vẫn góp phần làm cho màu xanh lam hơi khác một chút so với 0,0,110 của riêng nó

These colours mapped to dimensions of the matrix may be referred to as channels. Có thể hữu ích khi hiển thị độc lập từng kênh này để giúp chúng tôi hiểu điều gì đang xảy ra. Chúng tôi có thể làm điều đó bằng cách nhân biểu diễn mảng hình ảnh của chúng tôi với ma trận 1d có một cho kênh mà chúng tôi muốn giữ và số không cho phần còn lại

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
5

Kiểu dữ liệu của hình ảnh trong python là gì?

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
6

Kiểu dữ liệu của hình ảnh trong python là gì?

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
7

Kiểu dữ liệu của hình ảnh trong python là gì?

Nếu chúng ta nhìn vào hình vuông [1, 3] phía trên trong cả ba hình, chúng ta có thể thấy từng đóng góp màu đó đang hoạt động. Lưu ý rằng có một số hình vuông trong hình màu xanh lam trông thậm chí còn đậm hơn hình vuông [1, 3]. Tuy nhiên, khi cả ba kênh được kết hợp, ánh sáng xanh lam của các ô vuông đó sẽ bị pha loãng bởi cường độ tương đối của màu đỏ và xanh lục được trộn lẫn với chúng

Màu RGB 24-bit

Mô hình màu cuối cùng mà chúng tôi đã sử dụng, được gọi là mô hình RGB (Đỏ, Lục, Lam), là mô hình phổ biến nhất

As we saw, the RGB model is an additive colour model, which means that the primary colours are mixed together to form other colours. Thông thường, lượng màu cơ bản được thêm vào được biểu thị dưới dạng một số nguyên trong phạm vi đóng [0, 255] như trong ví dụ. Do đó, có 256 lượng riêng biệt của mỗi màu cơ bản có thể được thêm vào để tạo ra màu khác. The number of discrete amounts of each colour, 256, corresponds to the number of bits used to hold the colour channel value, which is eight (28=256). Vì chúng ta có ba kênh với 8 bit cho mỗi kênh (8+8+8=24), đây được gọi là độ sâu màu 24 bit

Bất kỳ màu cụ thể nào trong mô hình RGB đều có thể được biểu thị bằng bộ ba số nguyên trong [0, 255], tương ứng đại diện cho các kênh màu đỏ, lục và lam. Một số lớn hơn trong một kênh có nghĩa là có nhiều màu cơ bản hơn

Thinking about RGB colours (5 min)

Giả sử rằng chúng ta biểu diễn các màu dưới dạng bộ ba (r, g, b), trong đó mỗi bộ ba r, g và b là một số nguyên trong [0, 255]. Những màu nào được đại diện bởi mỗi bộ ba này? . )

  1. (255, 0, 0)
  2. (0, 255, 0)
  3. (0, 0, 255)
  4. (255, 255, 255)
  5. (0, 0, 0)
  6. (128, 128, 128)

Solution

  1. (255, 0, 0) đại diện cho màu đỏ, vì kênh màu đỏ được tối đa hóa, trong khi hai kênh còn lại có giá trị tối thiểu
  2. (0, 255, 0) đại diện cho màu xanh lục
  3. (0, 0, 255) đại diện cho màu xanh lam
  4. (255, 255, 255) khó hơn một chút. Khi trộn giá trị lớn nhất của cả 3 kênh màu ta thấy màu trắng
  5. (0, 0, 0) đại diện cho sự vắng mặt của tất cả các màu hoặc màu đen
  6. (128, 128, 128) đại diện cho màu xám trung bình. Lưu ý rằng mô hình màu RGB 24 bit cung cấp ít nhất 254 sắc thái xám, thay vì chỉ 50

Lưu ý rằng mô hình màu RGB có thể chạy ngược lại với trải nghiệm của bạn, đặc biệt nếu bạn đã trộn các màu sơn cơ bản để tạo màu mới. Trong mô hình RGB, thiếu bất kỳ màu nào là màu đen, trong khi số lượng tối đa của mỗi màu cơ bản là màu trắng. Với sơn vật lý, chúng ta có thể bắt đầu với lớp nền màu trắng, sau đó thêm các lượng sơn khác nhau để tạo ra màu đậm hơn

Sau khi hoàn thành thử thách trước, chúng ta có thể xem xét một số ví dụ khác về màu RGB 24-bit một cách trực quan. Hình ảnh trong thử thách tiếp theo hiển thị một số tên màu, giá trị bộ ba RGB 24 bit của chúng và chính màu đó

Bảng màu RGB (tùy chọn, không bao gồm trong thời gian)

Kiểu dữ liệu của hình ảnh trong python là gì?

Chúng tôi thực sự không thể cung cấp một bảng đầy đủ. Để xem tại sao, hãy trả lời câu hỏi này. How many possible colours can be represented with the 24-bit RGB model?

Solution

Có tổng cộng 24 bit trong một màu RGB thuộc loại này và mỗi bit có thể bật hoặc tắt, do đó, có 224 = 16.777.216 màu có thể có với mô hình màu RGB 24 bit bổ sung của chúng tôi

Mặc dù độ sâu màu 24 bit là phổ biến nhưng vẫn có các tùy chọn khác. Chúng ta có thể có màu 8 bit (3 bit cho màu đỏ và xanh lá cây, nhưng chỉ có 2 cho màu xanh lam, cung cấp 8 × 8 × 4 = 256 màu) hoặc màu 16 bit (4 bit cho màu đỏ, xanh lá cây và xanh lam, cộng thêm 4 bit nữa . Có độ sâu màu với hơn tám bit trên mỗi kênh, nhưng vì mắt người chỉ có thể phân biệt được khoảng 10 triệu màu khác nhau nên những màu này thường không được sử dụng

Nếu bạn đang sử dụng màn hình máy tính xách tay cũ hoặc rẻ tiền hoặc màn hình LCD để xem hình ảnh, nó có thể chỉ hỗ trợ màu 18-bit, có khả năng hiển thị 64 × 64 × 64 = 262.144 màu. Hình ảnh màu 24-bit sẽ được chuyển đổi theo một cách nào đó thành 18-bit và do đó, chất lượng màu bạn nhìn thấy sẽ không khớp với những gì thực sự có trong hình ảnh

Chúng ta có thể kết hợp hệ tọa độ của mình với mô hình màu RGB 24 bit để hiểu khái niệm về những hình ảnh mà chúng ta sẽ làm việc với. Một hình ảnh là một mảng pixel hình chữ nhật, mỗi pixel có tọa độ riêng. Mỗi pixel trong ảnh là một điểm vuông của ánh sáng màu, trong đó màu được chỉ định bởi bộ ba RGB 24 bit. Một hình ảnh như vậy là một ví dụ về đồ họa raster

định dạng hình ảnh

Mặc dù các hình ảnh mà chúng ta sẽ thao tác trong các chương trình của mình được khái niệm hóa dưới dạng các mảng hình chữ nhật của bộ ba RGB, nhưng chúng không nhất thiết phải được tạo, lưu trữ hoặc truyền ở định dạng đó. There are several image formats we might encounter, and we should know the basics of at least of few of them. Một số định dạng chúng tôi có thể gặp và phần mở rộng tệp của chúng được hiển thị trong bảng này

FormatExtension Bitmap độc lập với thiết bị (BMP). bmpJoint Nhóm chuyên gia chụp ảnh (JPEG). jpg hoặc. jpeg Định dạng tệp hình ảnh được gắn thẻ (TIFF). tif hoặc. tiff

BMP

The file format that comes closest to our preceding conceptualisation of images is the Device-Independent Bitmap, or BMP, file format. BMP files store raster graphics images as long sequences of binary-encoded numbers that specify the colour of each pixel in the image. Vì các tệp máy tính là cấu trúc một chiều nên các màu pixel được lưu trữ trên một hàng tại một thời điểm. Nghĩa là, hàng pixel đầu tiên (những pixel có tọa độ y 0) được lưu trữ trước, tiếp theo là hàng thứ hai (những pixel có tọa độ y 1), v.v. Tùy thuộc vào cách nó được tạo, hình ảnh BMP có thể có độ sâu màu 8-bit, 16-bit hoặc 24-bit

24-bit BMP images have a relatively simple file format, can be viewed and loaded across a wide variety of operating systems, and have high quality. Tuy nhiên, hình ảnh BMP không được nén, dẫn đến kích thước tệp rất lớn đối với bất kỳ độ phân giải hình ảnh hữu ích nào

Ý tưởng nén hình ảnh rất quan trọng đối với chúng tôi vì hai lý do. đầu tiên, hình ảnh nén có kích thước tệp nhỏ hơn và do đó dễ dàng lưu trữ và truyền tải hơn; . Vì quá trình nén rất quan trọng đối với chúng tôi, nên chúng tôi nên đi một vòng ngắn và thảo luận về khái niệm này

Nén hình ảnh

Trước khi thảo luận về các định dạng bổ sung, việc làm quen với nén hình ảnh sẽ hữu ích. Hãy đi sâu vào chủ đề đó với một thử thách. Đối với thử thách này, bạn sẽ cần biết về bit/byte và cách chúng được sử dụng để thể hiện dung lượng lưu trữ của máy tính. Nếu bạn đã biết, bạn có thể bỏ qua thử thách bên dưới

Bit và byte

Trước khi nói cụ thể về hình ảnh, trước tiên chúng ta cần hiểu cách các số được lưu trữ trong một máy tính kỹ thuật số hiện đại. Khi chúng tôi nghĩ về một số, chúng tôi làm như vậy bằng cách sử dụng hệ thống số thập phân hoặc giá trị theo vị trí cơ số 10. Ví dụ: một số như 659 là 6 × 102 + 5 × 101 + 9 × 100. Mỗi chữ số trong số được nhân với lũy thừa 10, dựa trên vị trí của nó và có 10 chữ số có thể xuất hiện ở mỗi vị trí (0, 1, 2, 3, 4, 5, 6, 7, 8, 9

In principle, computers could be constructed to represent numbers in exactly the same way. Tuy nhiên, các mạch điện tử bên trong máy tính sẽ dễ xây dựng hơn nhiều nếu chúng ta hạn chế cơ số chỉ còn hai, thay vì 10. (It is easier for circuitry to tell the difference between two voltage levels than it is to differentiate among 10 levels. ) Vì vậy, các giá trị trong máy tính được lưu trữ bằng hệ thống số nhị phân hoặc giá trị theo vị trí cơ số 2

Trong hệ thống này, mỗi ký hiệu trong một số được gọi là một bit thay vì một chữ số và chỉ có hai giá trị cho mỗi bit (0 và 1). We might imagine a four-bit binary number, 1101. Sử dụng cùng một kiểu mở rộng giá trị theo vị trí như chúng ta đã làm ở trên cho 659, chúng ta thấy rằng 1101 = 1 × 23 + 1 × 22 + 0 × 21 + 1 × 20, mà nếu chúng ta làm phép toán thì sẽ là 8 + 4 + 0 +

Bên trong, máy tính có số bit tối thiểu mà chúng hoạt động tại một thời điểm nhất định. tám. Một nhóm tám bit được gọi là một byte. Dung lượng bộ nhớ (RAM) và dung lượng ổ đĩa mà máy tính của chúng ta có được định lượng bằng các thuật ngữ như Megabyte (MB), Gigabyte (GB) và Terabyte (TB). Bảng sau đây cung cấp các định nghĩa chính thức hơn cho các thuật ngữ này

UnitAbbreviationSizeKilobyteKB1024 bytesMegabyteMB1024 KBGigabyteGB1024 MBTerabyteTB1024 GB

Kích thước hình ảnh BMP (tùy chọn, không bao gồm trong thời gian)

Hãy tưởng tượng rằng chúng ta có một hình ảnh khá lớn nhưng rất nhàm chán. hình ảnh 5.000 × 5.000 pixel bao gồm không có gì ngoài các pixel trắng. Nếu chúng tôi sử dụng định dạng hình ảnh không nén, chẳng hạn như BMP, với mô hình màu RGB 24 bit, thì cần bao nhiêu dung lượng lưu trữ cho tệp?

Solution

In such an image, there are 5,000 × 5,000 = 25,000,000 pixels, and 24 bits for each pixel, leading to 25,000,000 × 24 = 600,000,000 bits, or 75,000,000 bytes (71. 5MB). Đó là khá nhiều không gian cho một hình ảnh rất không thú vị

Vì các tệp hình ảnh có thể rất lớn, nên có nhiều sơ đồ nén khác nhau để lưu (xấp xỉ) cùng một thông tin trong khi sử dụng ít dung lượng hơn. These compression techniques can be categorised as lossless or lossy

Nén không mất dữ liệu

Trong nén ảnh không mất dữ liệu, chúng tôi áp dụng một số thuật toán (i. e. , một quy trình được vi tính hóa) vào hình ảnh, dẫn đến một tệp nhỏ hơn đáng kể so với tệp BMP không nén tương đương sẽ là. Then, when we wish to load and view or process the image, our program reads the compressed file, and reverses the compression process, resulting in an image that is identical to the original. Không có gì bị mất trong quá trình này – do đó thuật ngữ “lossless. ”

The general idea of lossless compression is to somehow detect long patterns of bytes in a file that are repeated over and over, and then assign a smaller bit pattern to represent the longer sample. Then, the compressed file is made up of the smaller patterns, rather than the larger ones, thus reducing the number of bytes required to save the file. Tệp nén cũng chứa một bảng các mẫu được thay thế và tệp gốc, vì vậy khi tệp được giải nén, nó có thể được tạo giống hệt với tệp gốc trước khi nén

To provide you with a concrete example, consider the 71. 5 MB white BMP image discussed above. Khi đưa qua tiện ích nén zip trên Microsoft Windows, kết quả. zip file is only 72 KB in size. Đó là, các. zip version of the image is three orders of magnitude smaller than the original, and it can be decompressed into a file that is byte-for-byte the same as the original. Vì bản gốc rất lặp lại - chỉ đơn giản là bộ ba màu giống nhau được lặp lại 25.000.000 lần - nên thuật toán nén có thể giảm đáng kể kích thước của tệp

Nếu bạn làm việc với. zip or . gz, bạn đang xử lý nén không mất dữ liệu

Nén mất dữ liệu

Lossy compression takes the original image and discards some of the detail in it, resulting in a smaller file format. Mục tiêu là chỉ loại bỏ chi tiết mà ai đó đang xem hình ảnh sẽ không nhận thấy. Nhiều lược đồ nén mất dữ liệu có các mức nén có thể điều chỉnh để người tạo hình ảnh có thể chọn lượng chi tiết bị mất. The more detail that is sacrificed, the smaller the image files will be - but of course, the detail and richness of the image will be lower as well

Điều này có thể tốt cho các hình ảnh được hiển thị trên các trang Web hoặc được in ra trên giấy ảnh 4 × 6, nhưng có thể hoặc không tốt cho công việc khoa học. Bạn sẽ phải quyết định xem việc giảm chất lượng hình ảnh và chi tiết có quan trọng đối với công việc của bạn hay không, so với việc tiết kiệm dung lượng do định dạng nén mất dữ liệu mang lại

Điều quan trọng là phải hiểu rằng một khi hình ảnh được lưu ở định dạng nén mất dữ liệu, chi tiết bị mất chỉ là - bị mất. Tôi. e. , không giống như các định dạng không mất dữ liệu, với một hình ảnh được lưu ở định dạng mất dữ liệu, không có cách nào để tái tạo lại hình ảnh gốc theo cách từng byte

JPEG

JPEG images are perhaps the most commonly encountered digital images today. JPEG uses lossy compression, and the degree of compression can be tuned to your liking. It supports 24-bit colour depth, and since the format is so widely used, JPEG images can be viewed and manipulated easily on all computing platforms

Kiểm tra kích thước hình ảnh thực tế (tùy chọn, không bao gồm trong thời gian)

Let us see the effects of image compression on image size with actual images. The following script creates a square white image 5000 X 5000 pixels, and then saves it as a BMP and as a JPEG image

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
8

Kiểm tra kích thước tệp của hai tệp đầu ra,

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
70 và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
71. Kích thước hình ảnh BMP có khớp với dự đoán trước đây của chúng tôi không?

Solution

Tệp BMP,

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
70, có dung lượng 75.000.054 byte, rất phù hợp với dự đoán của chúng tôi. Tệp JPEG,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
71, là 392.503 byte, nhỏ hơn hai bậc so với phiên bản bitmap

So sánh nén không mất dữ liệu và không mất dữ liệu (tùy chọn, không bao gồm trong thời gian)

Chúng ta hãy xem một ví dụ thực tế về nén không mất dữ liệu so với nén mất dữ liệu. Một lần nữa, mở terminal và điều hướng đến thư mục

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
74. The two output images,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
70 and
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
71, should still be in the directory, along with another image,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
77

We can apply lossless compression to any file by using the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
78 command. Nhớ lại rằng tệp
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
70 chứa 75.000.054 byte. Apply lossless compression to this image by executing the following command.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
80. Lệnh này yêu cầu máy tính tạo một tệp nén mới,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
81, từ ảnh bitmap ban đầu. Execute a similar command on the tree JPEG file.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
82

Tạo xong file nén, dùng lệnh

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
83 để hiển thị nội dung thư mục. Làm thế nào lớn là các tập tin nén?

Solution

Here is a partial directory listing, showing the sizes of the relevant files there

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
9

We can see that the regularity of the bitmap image (remember, it is a 5,000 x 5,000 pixel image containing only white pixels) allows the lossless compression scheme to compress the file quite effectively. Mặt khác, việc nén

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
77 không tạo ra một tệp nhỏ hơn nhiều;

Dưới đây là một ví dụ cho thấy cách nén JPEG có thể ảnh hưởng đến chất lượng hình ảnh. Consider this image of several maize seedlings (scaled down here from 11,339 × 11,336 pixels in order to fit the display)

Kiểu dữ liệu của hình ảnh trong python là gì?

Bây giờ, chúng ta hãy phóng to và xem xét một phần nhỏ của nhãn trong bản gốc, đầu tiên ở định dạng không nén

Kiểu dữ liệu của hình ảnh trong python là gì?

Đây là cùng một khu vực của hình ảnh, nhưng ở định dạng JPEG. We used a fairly aggressive compression parameter to make the JPEG, in order to illustrate the problems you might encounter with the format

Kiểu dữ liệu của hình ảnh trong python là gì?

Hình ảnh JPEG rõ ràng có chất lượng kém hơn. Nó có ít biến đổi màu sắc hơn và pixelation đáng chú ý. Sự khác biệt về chất lượng thậm chí còn rõ rệt hơn khi kiểm tra biểu đồ màu cho từng hình ảnh. Biểu đồ cho biết tần suất mỗi giá trị màu xuất hiện trong một hình ảnh. Biểu đồ cho hình ảnh không nén (trái) và nén (phải) được hiển thị bên dưới

Kiểu dữ liệu của hình ảnh trong python là gì?

Chúng ta sẽ học cách tạo các biểu đồ như thế này sau trong hội thảo. Sự khác biệt trong biểu đồ màu thậm chí còn rõ ràng hơn so với chính hình ảnh;

Nếu cài đặt chất lượng cho ảnh JPEG của bạn cao (và do đó tỷ lệ nén tương đối thấp), ảnh có thể đủ chất lượng cho công việc của bạn. Tất cả phụ thuộc vào chất lượng bạn cần và bạn có những hạn chế nào đối với không gian lưu trữ hình ảnh. Một cân nhắc khác có thể là nơi lưu trữ hình ảnh. Ví dụ: nếu hình ảnh của bạn được lưu trữ trên đám mây và do đó phải được tải xuống hệ thống của bạn trước khi sử dụng chúng, bạn có thể muốn sử dụng định dạng hình ảnh nén để tăng tốc thời gian truyền tệp

PNG

Hình ảnh PNG rất phù hợp để lưu trữ sơ đồ. It uses a lossless compression and is hence often used in web applications for non-photographic images. Định dạng này có thể lưu trữ dữ liệu RGB và độ chói đơn giản (kênh đơn, không có màu liên quan), trong số các dữ liệu khác. Dữ liệu hình ảnh được lưu trữ theo hàng và sau đó, trên mỗi hàng, một bộ lọc đơn giản, chẳng hạn như lấy sự khác biệt của các pixel liền kề, có thể được áp dụng để tăng khả năng nén của dữ liệu. Dữ liệu được lọc sau đó được nén trong bước tiếp theo và ghi ra đĩa

TIFF

Hình ảnh TIFF phổ biến với các nhà xuất bản, nhà thiết kế đồ họa và nhiếp ảnh gia. TIFF images can be uncompressed, or compressed using either lossless or lossy compression schemes, depending on the settings used, and so TIFF images seem to have the benefits of both the BMP and JPEG formats. Nhược điểm chính của hình ảnh TIFF (ngoài kích thước của hình ảnh ở phiên bản không nén của định dạng) là chúng không thể đọc được bằng phần mềm xử lý và xem hình ảnh

Metadata

Hình ảnh JPEG và TIFF hỗ trợ đưa siêu dữ liệu vào hình ảnh. Metadata is textual information that is contained within an image file. Metadata holds information about the image itself, such as when the image was captured, where it was captured, what type of camera was used and with what settings, etc. We normally don’t see this metadata when we view an image, but we can view it independently if we wish to (see Accessing Metadata, below). Điều quan trọng cần lưu ý ở giai đoạn này là bạn không thể dựa vào siêu dữ liệu của hình ảnh được bảo toàn hoàn toàn khi bạn sử dụng phần mềm để xử lý hình ảnh đó. Thư viện trình đọc/ghi hình ảnh mà chúng ta sử dụng trong suốt bài học này,

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53, bao gồm siêu dữ liệu khi lưu hình ảnh mới nhưng có thể không giữ được một số trường siêu dữ liệu. Trong mọi trường hợp, hãy nhớ. nếu siêu dữ liệu quan trọng đối với bạn, hãy đề phòng để luôn bảo toàn các tệp gốc

Truy cập siêu dữ liệu

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53 cung cấp một cách để hiển thị hoặc khám phá siêu dữ liệu được liên kết với một hình ảnh. Siêu dữ liệu được cung cấp độc lập với dữ liệu pixel

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
20

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
21

Có phần mềm khác có thể giúp bạn xử lý siêu dữ liệu, e. g. , Fiji và ImageMagick. Bạn có thể muốn khám phá các tùy chọn này nếu bạn cần làm việc với siêu dữ liệu hình ảnh của mình

Tổng hợp các định dạng ảnh sử dụng trong bài học này

The following table summarises the characteristics of the BMP, JPEG, and TIFF image formats

Định dạng NénSiêu dữ liệuƯu điểmNhược điểmBMPNoneKhông cóCó thể xem phổ biến,Kích thước tệp lớn   chất lượng cao JPEGLossyCó Có thể xem phổ biến,Chi tiết có thể bị mất   kích thước tệp nhỏ hơn PNGLosslessCó Có thể xem phổ biến, tiêu chuẩn mở, kích thước tệp nhỏ hơnSiêu dữ liệu ít linh hoạt hơn TIFF, chỉ RGB

Key Points

  • Hình ảnh kỹ thuật số được biểu diễn dưới dạng mảng hình chữ nhật pixel vuông

  • Hình ảnh kỹ thuật số sử dụng hệ tọa độ bên trái, với gốc tọa độ ở góc trên bên trái, trục x chạy sang phải và trục y chạy xuống. Một số người học có thể thích suy nghĩ theo cách đếm ngược các hàng cho trục y và các cột cho trục x. Vì vậy, chúng tôi sẽ cố gắng cho phép cả hai cách tiếp cận trong phần trình bày bài học của chúng tôi.

  • Thông thường, hình ảnh kỹ thuật số sử dụng mô hình RGB bổ sung, với tám bit cho các kênh màu đỏ, lục và lam

  • hình ảnh lướt qua được lưu trữ dưới dạng mảng NumPy đa chiều

  • Trong các hình ảnh lướt qua, kênh màu đỏ được chỉ định trước, sau đó là màu xanh lá cây, sau đó là màu xanh lam, tôi. e. , RGB

  • Lossless compression retains all the details in an image, but lossy compression results in loss of some of the original image detail

  • Hình ảnh BMP không được nén, có nghĩa là chúng có chất lượng cao nhưng kích thước tệp của chúng cũng lớn

  • Hình ảnh JPEG sử dụng phương pháp nén giảm chất lượng, nghĩa là kích thước tệp của chúng nhỏ hơn nhưng chất lượng hình ảnh có thể bị ảnh hưởng

  • Hình ảnh TIFF có thể được giải nén hoặc nén bằng phương pháp nén mất dữ liệu hoặc không mất dữ liệu

  • Depending on the camera or sensor, various useful pieces of information may be stored in an image file, in the image metadata


Làm việc với Skipage

Tổng quan

Dạy học. 70 min
Exercises. 50 phút

Questions

  • How can the skimage Python computer vision library be used to work with images?

Objectives

  • Read and save images with imageio

  • Hiển thị hình ảnh với matplotlib

  • Thay đổi kích thước hình ảnh với Skiage

  • Thực hiện ngưỡng hình ảnh đơn giản với các hoạt động mảng NumPy

  • Extract sub-images using array slicing

Chúng tôi đã đề cập nhiều đến cách hình ảnh được thể hiện trong phần mềm máy tính. Trong tập này chúng ta sẽ tìm hiểu thêm một số phương pháp để truy cập và thay đổi hình ảnh kỹ thuật số

Đọc, hiển thị và lưu hình ảnh

Imageio cung cấp các chức năng trực quan để đọc và ghi (lưu) hình ảnh. Tất cả các định dạng hình ảnh phổ biến, chẳng hạn như BMP, PNG, JPEG và TIFF đều được hỗ trợ, cùng với một số định dạng bí truyền khác. Kiểm tra tài liệu Định dạng được hỗ trợ để biết danh sách tất cả các định dạng. Matplotlib cung cấp một bộ sưu tập lớn các tiện ích vẽ đồ thị

Hãy để chúng tôi kiểm tra một chương trình Python đơn giản để tải, hiển thị và lưu hình ảnh sang một định dạng khác. Here are the first few lines

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
22

Đầu tiên, chúng tôi nhập mô-đun

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
89 của imageio (
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53) dưới dạng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
91 để chúng tôi có thể đọc và ghi hình ảnh. Sau đó, chúng tôi sử dụng chức năng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62 để đọc một hình ảnh JPEG có tên ghế. jpg. Imageio đọc hình ảnh, chuyển đổi nó từ JPEG thành một mảng NumPy và trả về mảng đó;

Tiếp theo, chúng tôi sẽ làm một cái gì đó với hình ảnh

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
23

Once we have the image in the program, we first call

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
94 so that we will have a fresh figure with a set of axis independent from our previous calls. Tiếp theo, chúng tôi gọi
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
95 để hiển thị hình ảnh

Now, we will save the image in another format

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
24

Câu lệnh cuối cùng trong chương trình,

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
96, ghi hình ảnh vào tệp có tên
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
97 trong thư mục
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
74. The
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
99 function automatically determines the type of the file, based on the file extension we provide. Trong trường hợp này, tiện ích mở rộng
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
200 khiến hình ảnh được lưu dưới dạng TIFF

Siêu dữ liệu, đã xem lại

Remember, as mentioned in the previous section, images saved with

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
99 will not retain all metadata associated with the original image that was loaded into Python. Nếu siêu dữ liệu hình ảnh quan trọng đối với bạn, hãy đảm bảo luôn giữ một bản sao không thay đổi của hình ảnh gốc

Tiện ích mở rộng không phải lúc nào cũng chỉ định loại tệp

Hàm

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
202 tự động sử dụng loại tệp mà chúng tôi chỉ định trong phần mở rộng của tham số tên tệp. Lưu ý rằng điều này không phải lúc nào cũng đúng. Ví dụ: nếu chúng tôi đang chỉnh sửa tài liệu trong Microsoft Word và chúng tôi lưu tài liệu dưới dạng
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
203 thay vì
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
204, tệp sẽ không được lưu dưới dạng tài liệu PDF

Đối số được đặt tên so với vị trí

Khi chúng ta gọi các hàm trong Python, có hai cách chúng ta có thể chỉ định các đối số cần thiết. Chúng tôi có thể chỉ định các đối số theo vị trí, tôi. e. , theo thứ tự các tham số xuất hiện trong định nghĩa hàm hoặc chúng ta có thể sử dụng các đối số được đặt tên

Ví dụ: định nghĩa hàm

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
202 chỉ định hai tham số, tài nguyên để lưu ảnh vào (e. g. , a file name, an http address) and the image to write to disk. Vì vậy, chúng tôi có thể lưu hình ảnh chiếc ghế trong mã mẫu ở trên bằng cách sử dụng các đối số vị trí như thế này

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
206

Since the function expects the first argument to be the file name, there is no confusion about what

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
207 means. The same goes for the second argument

Phong cách chúng ta sẽ sử dụng trong hội thảo này là đặt tên cho từng đối số, như thế này

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
96

This style will make it easier for you to learn how to use the variety of functions we will cover in this workshop

Thay đổi kích thước hình ảnh (10 phút)

Thêm

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
209 và
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
210 vào danh sách nhập khẩu của bạn. Sử dụng hình ảnh
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
211 nằm trong thư mục dữ liệu, viết một tập lệnh Python để đọc hình ảnh của bạn vào một biến có tên là
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
93. Sau đó, thay đổi kích thước hình ảnh thành 10 phần trăm kích thước hiện tại của nó bằng cách sử dụng các dòng mã này

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
25

Vì nó được sử dụng ở đây, các tham số của hàm

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
213 là hình ảnh cần biến đổi,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
93, kích thước mà chúng tôi muốn hình ảnh mới có,
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
215

Lưu ý rằng các giá trị pixel trong hình ảnh mới là giá trị gần đúng của các giá trị ban đầu và không được nhầm lẫn với dữ liệu thực, được quan sát. Điều này là do

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32 nội suy các giá trị pixel khi giảm hoặc tăng kích thước của hình ảnh.
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
217 có một số tham số tùy chọn cho phép người dùng kiểm soát phép nội suy này. Bạn có thể tìm thêm chi tiết trong tài liệu hình ảnh scikit

Các tệp hình ảnh trên đĩa thường được lưu trữ dưới dạng số nguyên để tiết kiệm dung lượng, nhưng các phép biến đổi và các phép toán khác thường dẫn đến chuyển đổi thành số dấu phẩy động. Sử dụng phương pháp

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
218 chuyển đổi nó trở lại thành số nguyên trước khi chúng tôi lưu nó trở lại đĩa. Nếu chúng tôi không chuyển đổi nó trước khi lưu,
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
202 có thể không nhận ra nó là dữ liệu hình ảnh

Next, write the resized image out to a new file named

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
220 in your data directory. Cuối cùng, sử dụng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
95 với mỗi biến hình ảnh của bạn để hiển thị cả hai hình ảnh trong sổ ghi chép của bạn. Đừng quên sử dụng
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
222 để bạn không ghi đè lên hình ảnh đầu tiên bằng hình ảnh thứ hai. Hình ảnh có thể xuất hiện cùng kích thước trong jupyter, nhưng bạn có thể thấy sự khác biệt về kích thước bằng cách so sánh tỷ lệ cho từng hình ảnh. Bạn cũng có thể thấy sự khác biệt về kích thước lưu trữ tệp trên đĩa bằng cách di con trỏ chuột lên tệp gốc và tệp mới trong trình duyệt tệp jupyter, sử dụng
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
223 trong trình bao của bạn hoặc trình duyệt tệp OS nếu nó được định cấu hình để hiển thị kích thước tệp

Solution

Here is what your Python script might look like

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
26

Tập lệnh thay đổi kích thước hình ảnh

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
224 theo hệ số 10 ở cả hai chiều, lưu kết quả vào tệp
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
225 và hiển thị ảnh gốc và đã thay đổi kích thước để so sánh

Thao tác pixel

In the Image Basics episode, we individually manipulated the colours of pixels by changing the numbers stored in the image’s NumPy array. Let’s apply the principles learned there along with some new principles to a real world example

Giả sử chúng ta quan tâm đến hình ảnh cụm rễ ngô này. We want to be able to focus our program’s attention on the roots themselves, while ignoring the black background

Kiểu dữ liệu của hình ảnh trong python là gì?

Vì hình ảnh được lưu trữ dưới dạng một mảng số, chúng ta có thể chỉ cần xem qua mảng để tìm các giá trị màu pixel nhỏ hơn một số giá trị ngưỡng. This process is called thresholding, and we will see more powerful methods to perform the thresholding task in the Thresholding episode. Tuy nhiên, ở đây, chúng ta sẽ xem xét một phương pháp NumPy đơn giản và tao nhã để tạo ngưỡng. Hãy để chúng tôi phát triển một chương trình chỉ giữ các giá trị màu pixel trong một hình ảnh có giá trị lớn hơn hoặc bằng 128. This will keep the pixels that are brighter than half of “full brightness”, i. e. , pixels that do not belong to the black background. Chúng tôi sẽ bắt đầu bằng cách đọc hình ảnh và hiển thị nó

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
27

Now we can threshold the image and display the result

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
28

Lệnh NumPy để bỏ qua tất cả các pixel cường độ thấp là

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
226. Mọi giá trị màu pixel trong toàn bộ mảng 3 chiều có giá trị nhỏ hơn 128 được đặt thành 0. Trong trường hợp này, kết quả là một hình ảnh trong đó chi tiết nền bên ngoài đã bị loại bỏ

Kiểu dữ liệu của hình ảnh trong python là gì?

Chuyển đổi hình ảnh màu sang thang độ xám

Thường dễ dàng hơn khi làm việc với ảnh thang độ xám có một kênh thay vì ảnh màu có ba kênh. Skimage cung cấp chức năng

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
227 để đạt được điều này. This function adds up the three colour channels in a way that matches human colour perception, see the skimage documentation for details. Nó trả về một hình ảnh thang độ xám với các giá trị dấu phẩy động trong phạm vi từ 0 đến 1. Chúng ta có thể sử dụng hàm
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
218 để chuyển đổi nó trở lại kiểu dữ liệu ban đầu và phạm vi dữ liệu trở lại 0 đến 255. Lưu ý rằng thường tốt hơn nếu sử dụng các giá trị hình ảnh được biểu thị bằng các giá trị dấu phẩy động, bởi vì sử dụng số dấu phẩy động sẽ ổn định hơn về mặt số học

Màu sắc và """ * Python libraries for learning and performing image processing. * """ import numpy as np import matplotlib.pyplot as plt import ipympl import imageio.v3 as iio import skimage 229

The Carpentries generally prefers UK English spelling, which is why we use “colour” in the explanatory text of this lesson. However,

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32 contains many modules and functions that include the US English spelling,
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
229. The exact spelling matters here, e. g. you will encounter an error if you try to run
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
232. To account for this, we will use the US English spelling,
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
229, in example Python code throughout the lesson. You will encounter a similar approach with “centre” and
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
234

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
29

We can also load colour images as grayscale directly by passing the argument

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
235 to
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
70

Keeping only low intensity pixels (10 min)

A little earlier, we showed how we could use Python and skimage to turn on only the high intensity pixels from an image, while turning all the low intensity pixels off. Now, you can practice doing the opposite - keeping all the low intensity pixels while changing the high intensity ones

The file

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
237 is an RGB image of a sudoku puzzle

Kiểu dữ liệu của hình ảnh trong python là gì?

Your task is to turn all of the white pixels in the image to a light gray colour, say with the intensity of each formerly white pixel set to 0. 75. The results should look like this

Kiểu dữ liệu của hình ảnh trong python là gì?

Gợi ý. this is an instance where it is helpful to convert the image from RGB to grayscale

Solution

Đầu tiên, tải tệp hình ảnh vào và chuyển đổi nó thành thang độ xám

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
71

Then, change all high intensity pixel values (> 0. 75) đến 0. 75

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
72

Cuối cùng, hiển thị hình ảnh sửa đổi

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
73

Vẽ sơ đồ hình ảnh kênh đơn (cmap, vmin, vmax)

Compared to a colour image, a grayscale image contains only a single intensity value per pixel. Khi chúng tôi vẽ một hình ảnh như vậy bằng

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
238, matplotlib sử dụng bản đồ màu để gán cho mỗi giá trị cường độ một màu. Bản đồ màu mặc định được gọi là “viridis” và ánh xạ các giá trị thấp thành màu tím và giá trị cao thành màu vàng. Thay vào đó, chúng tôi có thể hướng dẫn matplotlib ánh xạ các giá trị thấp thành màu đen và giá trị cao thành màu trắng, bằng cách gọi
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
238 với
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
240. Tài liệu chứa thông tin tổng quan về các bản đồ màu được xác định trước

Hơn nữa, theo mặc định, matplotlib xác định các giá trị tối thiểu và tối đa của bản đồ màu từ hình ảnh. Điều đó có nghĩa là, trong một hình ảnh, trong đó giá trị tối thiểu là 0. 25 and the maximum is 0. 75, các giá trị đó sẽ được ánh xạ thành đen và trắng tương ứng (chứ không phải xám đậm và xám nhạt như bạn mong đợi). Nếu có các giá trị tối thiểu và tối đa được xác định, bạn có thể chỉ định chúng qua

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
241 và
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
242 để có được đầu ra mong muốn

Nếu bạn quên điều này, nó có thể dẫn đến kết quả không mong muốn. Try removing the

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
242 parameter from the sudoku challenge solution and see what happens

Truy cập thông qua cắt lát

Như đã lưu ý trong bài học trước, hình ảnh lướt qua được lưu trữ dưới dạng mảng NumPy, vì vậy chúng ta có thể sử dụng tính năng cắt mảng để chọn các vùng hình chữ nhật của hình ảnh. Sau đó, chúng ta có thể lưu lựa chọn thành một hình ảnh mới, thay đổi các pixel trong hình ảnh, v.v. Điều quan trọng cần nhớ là các tọa độ được chỉ định theo thứ tự (ry, cx) và các giá trị màu được chỉ định theo thứ tự (r, g, b) khi thực hiện các thao tác này

Hãy xem xét hình ảnh bảng trắng này và giả sử rằng chúng ta muốn tạo một hình ảnh con chỉ có phần ghi “lẻ + chẵn = lẻ”, cùng với hộp màu đỏ được vẽ xung quanh các từ

Kiểu dữ liệu của hình ảnh trong python là gì?

Sử dụng cùng một kỹ thuật hiển thị mà chúng tôi đã sử dụng trong suốt khóa học này, chúng tôi có thể xác định tọa độ của các góc của khu vực mà chúng tôi muốn trích xuất bằng cách di chuột gần các điểm ưa thích và ghi lại tọa độ. Nếu chúng ta làm như vậy, chúng ta có thể giải quyết trên một khu vực hình chữ nhật có tọa độ phía trên bên trái là (135, 60) và tọa độ phía dưới bên phải là (480, 150), như thể hiện trong phiên bản hình ảnh bảng trắng này

Kiểu dữ liệu của hình ảnh trong python là gì?

Lưu ý rằng tọa độ trong hình trước được chỉ định theo thứ tự (cx, ry). Bây giờ nếu toàn bộ hình ảnh bảng trắng của chúng tôi được lưu trữ dưới dạng hình ảnh lướt qua có tên là

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
93, chúng tôi có thể tạo một hình ảnh mới của vùng đã chọn bằng một câu lệnh như thế này

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
245

Our array slicing specifies the range of y-coordinates or rows first,

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
246, and then the range of x-coordinates or columns,
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
247. Lưu ý rằng chúng tôi vượt quá giá trị tối đa trong mỗi thứ nguyên để toàn bộ khu vực mong muốn được chọn. The third part of the slice,
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
248, indicates that we want all three colour channels in our new image

A script to create the subimage would start by loading the image

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
74

Sau đó, chúng tôi sử dụng cắt mảng để tạo một hình ảnh mới với khu vực đã chọn của chúng tôi và sau đó hiển thị hình ảnh mới

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
75

We can also change the values in an image, as shown next

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
76

Đầu tiên, chúng tôi lấy mẫu màu của một pixel tại một vị trí cụ thể của hình ảnh, lưu nó vào một biến có tên là

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
229, biến này tạo ra một mảng NumPy 1 × 1 × 3 với các giá trị màu xanh dương, xanh lá cây và đỏ cho pixel nằm ở ( . Sau đó, với lệnh
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
250, chúng tôi sửa đổi hình ảnh trong khu vực được chỉ định. Từ góc độ NumPy, điều này thay đổi tất cả các giá trị pixel trong phạm vi đó thành mảng được lưu trong biến
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
229. In this case, the command “erases” that area of the whiteboard, replacing the words with a beige colour, as shown in the final image produced by the program

Kiểu dữ liệu của hình ảnh trong python là gì?

Practicing with slices (10 min - optional, not included in timing)

Sử dụng các kỹ thuật bạn vừa học, viết một tập lệnh tạo, hiển thị và lưu một hình ảnh phụ chỉ chứa cây và rễ của nó từ “data/maize-root-cluster. jpg”

Solution

Đây là chương trình Python đã hoàn thành để chỉ chọn cây và rễ trong ảnh

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
77

Key Points

  • Images are read from disk with the

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    62 function

  • Chúng tôi tạo một cửa sổ tự động chia tỷ lệ hình ảnh được hiển thị bằng matplotlib và gọi

    """
     * Python libraries for learning and performing image processing.
     *
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import ipympl
    import imageio.v3 as iio
    import skimage
    
    253 trên đối tượng hình toàn cầu

  • Colour images can be transformed to grayscale using

    """
     * Python libraries for learning and performing image processing.
     *
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import ipympl
    import imageio.v3 as iio
    import skimage
    
    227 or, in many cases, be read as grayscale directly by passing the argument
    """
     * Python libraries for learning and performing image processing.
     *
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import ipympl
    import imageio.v3 as iio
    import skimage
    
    235 to
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    62

  • We can resize images with the

    """
     * Python libraries for learning and performing image processing.
     *
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import ipympl
    import imageio.v3 as iio
    import skimage
    
    213 function

  • Các lệnh mảng NumPy, chẳng hạn như

    """
     * Python libraries for learning and performing image processing.
     *
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import ipympl
    import imageio.v3 as iio
    import skimage
    
    226, có thể được sử dụng để thao tác các pixel của hình ảnh

  • Cắt mảng có thể được sử dụng để trích xuất các hình ảnh phụ hoặc sửa đổi các vùng của hình ảnh, v.v. g. ,

    """
     * Python libraries for learning and performing image processing.
     *
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import ipympl
    import imageio.v3 as iio
    import skimage
    
    259

  • Siêu dữ liệu không được giữ lại khi hình ảnh được tải dưới dạng hình ảnh lướt qua


Hoạt động Vẽ và Bitwise

Tổng quan

Teaching. 45 phút
Bài tập. 45 phút

Questions

  • Làm cách nào chúng ta có thể vẽ trên các hình ảnh lướt qua và sử dụng các thao tác bitwise và mặt nạ để chọn các phần nhất định của hình ảnh?

Objectives

  • Tạo một hình ảnh Skipage trống, màu đen

  • Vẽ hình chữ nhật và các hình dạng khác trên hình ảnh lướt qua

  • Giải thích cách sử dụng hình dạng màu trắng trên nền đen làm mặt nạ để chọn các phần cụ thể của hình ảnh

  • Sử dụng các thao tác bitwise để áp dụng mặt nạ cho hình ảnh

The next series of episodes covers a basic toolkit of skimage operators. Với những công cụ này, chúng tôi sẽ có thể tạo các chương trình để thực hiện các phân tích hình ảnh đơn giản dựa trên những thay đổi về màu sắc hoặc hình dạng

Drawing on images

Often we wish to select only a portion of an image to analyze, and ignore the rest. Tạo một hình ảnh con hình chữ nhật bằng cách cắt, như chúng ta đã làm trong phần Biểu diễn hình ảnh trong phần lướt qua là một tùy chọn cho các trường hợp đơn giản. Một tùy chọn khác là tạo một hình ảnh đặc biệt khác, có cùng kích thước với hình gốc, với các điểm ảnh màu trắng cho biết khu vực cần lưu và các điểm ảnh màu đen ở mọi nơi khác. Một hình ảnh như vậy được gọi là mặt nạ. In preparing a mask, we sometimes need to be able to draw a shape - a circle or a rectangle, say - on a black image. Skipage cung cấp các công cụ để làm điều đó

Xem xét hình ảnh này của cây ngô

Kiểu dữ liệu của hình ảnh trong python là gì?

Now, suppose we want to analyze only the area of the image containing the roots themselves; we do not care to look at the kernels, or anything else about the plants. Further, we wish to exclude the frame of the container holding the seedlings as well. Di chuột qua hình ảnh, có thể cho chúng tôi biết tọa độ phía trên bên trái của vùng phụ mà chúng tôi quan tâm là (44, 357), trong khi tọa độ phía dưới bên phải là (720, 740). Các tọa độ này được hiển thị theo thứ tự (x, y)

Một chương trình Python tạo mặt nạ để chỉ chọn vùng đó của hình ảnh sẽ bắt đầu bằng một đoạn mã quen thuộc để mở và hiển thị hình ảnh gốc

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
78

Như trước đây, trước tiên chúng tôi nhập mô-đun con

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
89 của
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56 (
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53). Chúng tôi cũng nhập thư viện NumPy mà chúng tôi cần để tạo hình ảnh mặt nạ ban đầu. Sau đó, chúng tôi nhập mô-đun con
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
39 của
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32. Chúng tôi tải và hiển thị hình ảnh ban đầu giống như cách chúng tôi đã làm trước đây

NumPy cho phép lập chỉ mục các hình ảnh/mảng với các mảng “boolean” có cùng kích thước. Indexing with a boolean array is also called mask indexing. The “pixels” in such a mask array can only take two values.

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265 hoặc
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
266. When indexing an image with such a mask, only pixel values at positions where the mask is
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265 are accessed. Nhưng trước tiên, chúng ta cần tạo một mảng mặt nạ có cùng kích thước với hình ảnh. May mắn thay, thư viện NumPy cung cấp một hàm để tạo một mảng như vậy. Phần tiếp theo của mã cho thấy làm thế nào

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
79

Đối số đầu tiên của hàm

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
268 là hình dạng của ảnh gốc, do đó mặt nạ của chúng ta sẽ có cùng kích thước với ảnh gốc. Notice, that we have only used the first two indices of our shape. We omitted the channel dimension. Lập chỉ mục với mặt nạ như vậy sẽ thay đổi đồng thời tất cả các giá trị kênh. Đối số thứ hai,
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
269, chỉ ra rằng các phần tử trong mảng phải là boolean - i. e. , values are either
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265 or
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
266. Thus, even though we use
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
272 to create the mask, its pixel values are in fact not
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
273 but
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265. You could check this, e. g. , by
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
275

Next, we draw a filled, rectangle on the mask

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
90

Here is what our constructed mask looks like.

Kiểu dữ liệu của hình ảnh trong python là gì?

The parameters of the

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
276 function
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
277 and
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
278, are the coordinates of the upper-left (
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
279) and lower-right (
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
280) corners of a rectangle in (ry, cx) order. The function returns the rectangle as row (
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
281) and column (
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
282) coordinate arrays

Check the documentation

When using an skimage function for the first time - or the fifth time - it is wise to check how the function is used, via the skimage documentation or other usage examples on programming-related sites such as Stack Overflow. Basic information about skimage functions can be found interactively in Python, via commands like

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
283 or
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
284. Take notes in your lab notebook. And, it is always wise to run some test code to verify that the functions your program uses are behaving in the manner you intend

Variable naming conventions

You may have wondered why we called the return values of the rectangle function

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
281 and
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
282?. You may have guessed that
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
287 is short for
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
288 and
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
289 is short for
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
290. However, the rectangle function returns mutiple rows and columns; thus we used a convention of doubling the letter
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
287 to
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
281 (and
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
289 to
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
282) to indicate that those are multiple values. In fact it may have even been clearer to name those variables
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
295 and
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
296; however this would have been also much longer. Whatever you decide to do, try to stick to some already existing conventions, such that it is easier for other people to understand your code

Other drawing operations (15 min)

There are other functions for drawing on images, in addition to the

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
297 function. We can draw circles, lines, text, and other shapes as well. These drawing functions may be useful later on, to help annotate images that our programs produce. Practice some of these functions here

Circles can be drawn with the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
38 function, which takes two parameters. the (ry, cx) point of the centre of the circle, and the radius of the circle. There is an optional
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
299 parameter that can be supplied to this function. It will limit the output coordinates for cases where the circle dimensions exceed the ones of the image

Lines can be drawn with the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
700 function, which takes four parameters. the (ry, cx) coordinate of one end of the line, and the (ry, cx) coordinate of the other end of the line

Other drawing functions supported by skimage can be found in the skimage reference pages

First let’s make an empty, black image with a size of 800x600 pixels

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
91

Now your task is to draw some other coloured shapes and lines on the image, perhaps something like this

Kiểu dữ liệu của hình ảnh trong python là gì?

Solution

Drawing a circle

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
92

Drawing a line

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
93

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
94

We could expand this solution, if we wanted, to draw rectangles, circles and lines at random positions within our black canvas. To do this, we could use the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
701 python module, and the function
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
702, which can produce random numbers within a certain range

Let’s draw 15 randomly placed circles

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
95

We could expand this even further to also randomly choose whether to plot a rectangle, a circle, or a square. Again, we do this with the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
701 module, now using the function
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
704 that returns a random number between 0. 0 and 1. 0

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
96

Image modification

All that remains is the task of modifying the image using our mask in such a way that the areas with

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265 pixels in the mask are not shown in the image any more

How does a mask work? (optional, not included in timing)

Now, consider the mask image we created above. The values of the mask that corresponds to the portion of the image we are interested in are all

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
266, while the values of the mask that corresponds to the portion of the image we want to remove are all
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265

How do we change the original image using the mask?

Solution

When indexing the image using the mask, we access only those pixels at positions where the mask is

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265. So, when indexing with the mask, one can set those values to 0, and effectively remove them from the image

Now we can write a Python program to use a mask to retain only the portions of our maize roots image that actually contains the seedling roots. We load the original image and create the mask in the same way as before

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
97

Then, we use numpy indexing to remove the portions of the image, where the mask is

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
98

Then, we display the masked image

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
99

The resulting masked image should look like this

Kiểu dữ liệu của hình ảnh trong python là gì?

Masking an image of your own (optional, not included in timing)

Now, it is your turn to practice. Using your mobile phone, tablet, webcam, or digital camera, take an image of an object with a simple overall geometric shape (think rectangular or circular). Copy that image to your computer, write some code to make a mask, and apply it to select the part of the image containing your object. For example, here is an image of a remote control

Kiểu dữ liệu của hình ảnh trong python là gì?

And, here is the end result of a program masking out everything but the remote

Kiểu dữ liệu của hình ảnh trong python là gì?

Solution

Here is a Python program to produce the cropped remote control image shown above. Of course, your program should be tailored to your image

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
00

Masking a 96-well plate image (30 min)

Consider this image of a 96-well plate that has been scanned on a flatbed scanner

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
01

Kiểu dữ liệu của hình ảnh trong python là gì?

Suppose that we are interested in the colours of the solutions in each of the wells. We do not care about the colour of the rest of the image, i. e. , the plastic that makes up the well plate itself

Your task is to write some code that will produce a mask that will mask out everything except for the wells. To help with this, you should use the text file

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
710 that contains the (cx, ry) coordinates of the centre of each of the 96 wells in this image. You may assume that each of the wells has a radius of 16 pixels

Your program should produce output that looks like this

Kiểu dữ liệu của hình ảnh trong python là gì?

Solution

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
02

Masking a 96-well plate image, take two (optional, not included in timing)

If you spent some time looking at the contents of the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
710 file from the previous challenge, you may have noticed that the centres of each well in the image are very regular. Assuming that the images are scanned in such a way that the wells are always in the same place, and that the image is perfectly oriented (i. e. , it does not slant one way or another), we could produce our well plate mask without having to read in the coordinates of the centres of each well. Assume that the centre of the upper left well in the image is at location cx = 91 and ry = 108, and that there are 70 pixels between each centre in the cx dimension and 72 pixels between each centre in the ry dimension. Each well still has a radius of 16 pixels. Write a Python program that produces the same output image as in the previous challenge, but without having to read in the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
712 file. Hint. use nested for loops

Solution

Here is a Python program that is able to create the masked image without having to read in the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
712 file

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
03

Key Points

  • We can use the NumPy

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    714 function to create a blank, black image

  • We can draw on skimage images with functions such as

    """
     * Python libraries for learning and performing image processing.
     *
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import ipympl
    import imageio.v3 as iio
    import skimage
    
    297,
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    38,
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    700, and more

  • The drawing functions return indices to pixels that can be set directly


Creating Histograms

Tổng quan

Teaching. 40 min
Exercises. 40 min

Questions

  • How can we create grayscale and colour histograms to understand the distribution of colour values in an image?

Objectives

  • Explain what a histogram is

  • Load an image in grayscale format

  • Create and display grayscale and colour histograms for entire images

  • Create and display grayscale and colour histograms for certain areas of images, via masks

In this episode, we will learn how to use skimage functions to create and display histograms for images

Introduction to Histograms

As it pertains to images, a histogram is a graphical representation showing how frequently various colour values occur in the image. We saw in the Image Basics episode that we could use a histogram to visualise the differences in uncompressed and compressed image formats. If your project involves detecting colour changes between images, histograms will prove to be very useful, and histograms are also quite handy as a preparatory step before performing thresholding

Grayscale Histograms

We will start with grayscale images, and then move on to colour images. We will use this image of a plant seedling as an example.

Kiểu dữ liệu của hình ảnh trong python là gì?

Here we load the image in grayscale instead of full colour, and display it

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
04

Kiểu dữ liệu của hình ảnh trong python là gì?

Again, we use the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62 function to load our image. The first argument to
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62 is the filename of the image. The second argument
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
235 defines the type and depth of a pixel in the image (e. g. , an 8-bit pixel has a range of 0-255). This argument is forwarded to the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
721 backend, for which mode “L” means 8-bit pixels and single-channel (i. e. , grayscale).
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
721 is a Python imaging library; which backend is used by
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62 may be specified (to use
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
721, you would pass this argument.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
725); if unspecified,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62 determines the backend to use based on the image type

Then, we convert the grayscale image of integer dtype, with 0-255 range, into a floating-point one with 0-1 range, by calling the function

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
727. We will keep working with images in the value range 0 to 1 in this lesson

We now use the function

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
728 to compute the histogram of our image which, after all, is a NumPy array

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
05

The parameter

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
729 determines the number of “bins” to use for the histogram. We pass in
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
730 because we want to see the pixel count for each of the 256 possible values in the grayscale image

The parameter

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
731 is the range of values each of the pixels in the image can have. Here, we pass 0 and 1, which is the value range of our input image after transforming it to grayscale

The first output of the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
728 function is a one-dimensional NumPy array, with 256 rows and one column, representing the number of pixels with the intensity value corresponding to the index. I. e. , the first number in the array is the number of pixels found with intensity value 0, and the final number in the array is the number of pixels found with intensity value 255. The second output of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
728 is an array with the bin edges and one column and 257 rows (one more than the histogram itself). There are no gaps between the bins, which means that the end of the first bin, is the start of the second and so on. For the last bin, the array also has to contain the stop, so it has one more element, than the histogram

Next, we turn our attention to displaying the histogram, by taking advantage of the plotting facilities of the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
734 library

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
06

Chúng tôi tạo biểu đồ bằng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
735, sau đó gắn nhãn hình và các trục tọa độ bằng các hàm
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
736,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
737 và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
738. The last step in the preparation of the figure is to set the limits on the values on the x-axis with the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
739 function call

Variable-length argument lists

Note that we cannot used named parameters for the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
740 or
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
741 functions. This is because these functions are defined to take an arbitrary number of unnamed arguments. The designers wrote the functions this way because they are very versatile, and creating named parameters for all of the possible ways to use them would be complicated

Finally, we create the histogram plot itself with

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
742. We use the left bin edges as x-positions for the histogram values by indexing the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
743 array to ignore the last value (the right edge of the last bin). When we run the program on this image of a plant seedling, it produces this histogram

Kiểu dữ liệu của hình ảnh trong python là gì?

Histograms in matplotlib

Matplotlib provides a dedicated function to compute and display histograms.

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
744. We will not use it in this lesson in order to understand how to calculate histograms in more detail. In practice, it is a good idea to use this function, because it visualises histograms more appropriately than
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
741. Here, you could use it by calling
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
746 instead of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
747 and
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
741 (
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
749 is a numpy function that converts our two-dimensional image into a one-dimensional array)

Using a mask for a histogram (15 min)

Looking at the histogram above, you will notice that there is a large number of very dark pixels, as indicated in the chart by the spike around the grayscale value 0. 12. That is not so surprising, since the original image is mostly black background. What if we want to focus more closely on the leaf of the seedling? That is where a mask enters the picture

First, hover over the plant seedling image with your mouse to determine the (x, y) coordinates of a bounding box around the leaf of the seedling. Then, using techniques from the Drawing and Bitwise Operations episode, create a mask with a white rectangle covering that bounding box

After you have created the mask, apply it to the input image before passing it to the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
728 function

Solution

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
07

Your histogram of the masked area should look something like this

Kiểu dữ liệu của hình ảnh trong python là gì?

Colour Histograms

We can also create histograms for full colour images, in addition to grayscale histograms. We have seen colour histograms before, in the Image Basics episode. A program to create colour histograms starts in a familiar way

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
08

We read the original image, now in full colour, and display it

Next, we create the histogram, by calling the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
728 function three times, once for each of the channels. We obtain the individual channels, by slicing the image along the last axis. For example, we can obtain the red colour channel by calling
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
752

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
09

We will draw the histogram line for each channel in a different colour, and so we create a tuple of the colours to use for the three lines with the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
753

line of code. Then, we limit the range of the x-axis with the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
740 function call

Next, we use the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
755 control structure to iterate through the three channels, plotting an appropriately-coloured histogram line for each. This may be new Python syntax for you, so we will take a moment to discuss what is happening in the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
755 statement

The Python built-in

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
757 function takes a list and returns an iterator of tuples, where the first element of the tuple is the index and the second element is the element of the list

Iterators, tuples, and import skimage # form 1, load whole skimage library import skimage.draw # form 2, load skimage.draw module only from skimage.draw import disk # form 3, load only the disk function import numpy as np # form 4, load all of numpy into an object called np 757

In Python, an iterator, or an iterable object, is something that can be iterated over with the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
755 control structure. A tuple is a sequence of objects, just like a list. However, a tuple cannot be changed, and a tuple is indicated by parentheses instead of square brackets. The
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
757 function takes an iterable object, and returns an iterator of tuples consisting of the 0-based index and the corresponding object

For example, consider this small Python program

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
40

Executing this program would produce the following output

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
41

In our colour histogram program, we are using a tuple,

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
761, as the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
755 variable. The first time through the loop, the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
763 variable takes the value
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
764, referring to the position of the red colour channel, and the
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
229 variable contains the string
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
766. The second time through the loop the values are the green channels index
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
273 and
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
768, and the third time they are the blue channel index
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
769 and
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
770

Inside the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
755 loop, our code looks much like it did for the grayscale example. We calculate the histogram for the current channel with the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
772

function call, and then add a histogram line of the correct colour to the plot with the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
773

function call. Note the use of our loop variables,

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
763 and
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
289

Finally we label our axes and display the histogram, shown here

Kiểu dữ liệu của hình ảnh trong python là gì?

Colour histogram with a mask (25 min)

We can also apply a mask to the images we apply the colour histogram process to, in the same way we did for grayscale histograms. Consider this image of a well plate, where various chemical sensors have been applied to water and various concentrations of hydrochloric acid and sodium hydroxide

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
42

Kiểu dữ liệu của hình ảnh trong python là gì?

Suppose we are interested in the colour histogram of one of the sensors in the well plate image, specifically, the seventh well from the left in the topmost row, which shows Erythrosin B reacting with water

Hover over the image with your mouse to find the centre of that well and the radius (in pixels) of the well. Then create a circular mask to select only the desired well. Then, use that mask to apply the colour histogram operation to that well

Your masked image should look like this

Kiểu dữ liệu của hình ảnh trong python là gì?

And, the program should produce a colour histogram that looks like this

Kiểu dữ liệu của hình ảnh trong python là gì?

Solution

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
43

Key Points

  • In many cases, we can load images in grayscale by passing the

    """
     * Python libraries for learning and performing image processing.
     *
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import ipympl
    import imageio.v3 as iio
    import skimage
    
    235 argument to the
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    62 function

  • We can create histograms of images with the

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    728 function

  • We can separate the RGB channels of an image using slicing operations

  • We can display histograms using the

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    779
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    780,
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    781,
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    782,
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    783,
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    784,
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    785, and
    """
     * Python libraries for learning and performing image processing.
     *
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import ipympl
    import imageio.v3 as iio
    import skimage
    
    253 functions


Blurring Images

Tổng quan

Teaching. 35 min
Exercises. 25 min

Questions

  • How can we apply a low-pass blurring filter to an image?

Objectives

  • Explain why applying a low-pass blurring filter to an image is beneficial

  • Apply a Gaussian blur filter to an image using skimage

In this episode, we will learn how to use skimage functions to blur images

When processing an image, we are often interested in identifying objects represented within it so that we can perform some further analysis of these objects e. g. by counting them, measuring their sizes, etc. An important concept associated with the identification of objects in an image is that of edges. the lines that represent a transition from one group of similar pixels in the image to another different group. One example of an edge is the pixels that represent the boundaries of an object in an image, where the background of the image ends and the object begins

When we blur an image, we make the colour transition from one side of an edge in the image to another smooth rather than sudden. The effect is to average out rapid changes in pixel intensity. A blur is a very common operation we need to perform before other tasks such as thresholding. There are several different blurring functions in the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
787 module, so we will focus on just one here, the Gaussian blur

Filters

In the day-to-day, macroscopic world, we have physical filters which separate out objects by size. A filter with small holes allows only small objects through, leaving larger objects behind. This is a good analogy for image filters. A high-pass filter will retain the smaller details in an image, filtering out the larger ones. A low-pass filter retains the larger features, analogous to what’s left behind by a physical filter mesh. High- and low-pass, here, refer to high and low spatial frequencies in the image. Details associated with high spatial frequencies are small, a lot of these features would fit across an image. Features associated with low spatial frequencies are large - maybe a couple of big features per image

Blurring

Blurring is to make something less clear or distinct. This could be interpreted quite broadly in the context of image analysis - anything that reduces or distorts the detail of an image might apply. Applying a low pass filter, which removes detail occurring at high spatial frequencies, is perceived as a blurring effect. A Gaussian blur is a filter that makes use of a Gaussian kernel

Kernels

A kernel can be used to implement a filter on an image. A kernel, in this context, is a small matrix which is combined with the image using a mathematical technique. convolution. Different sizes, shapes and contents of kernel produce different effects. The kernel can be thought of as a little image in itself, and will favour features of a similar size and shape in the main image. On convolution with an image, a big, blobby kernel will retain big, blobby, low spatial frequency features

Gaussian blur

Consider this image of a cat, in particular the area of the image outlined by the white square

Kiểu dữ liệu của hình ảnh trong python là gì?

Now, zoom in on the area of the cat’s eye, as shown in the left-hand image below. When we apply a filter, we consider each pixel in the image, one at a time. In this example, the pixel we are currently working on is highlighted in red, as shown in the right-hand image

Kiểu dữ liệu của hình ảnh trong python là gì?

When we apply a filter, we consider rectangular groups of pixels surrounding each pixel in the image, in turn. The kernel is another group of pixels (a separate matrix / small image), of the same dimensions as the rectangular group of pixels in the image, that moves along with the pixel being worked on by the filter. The width and height of the kernel must be an odd number, so that the pixel being worked on is always in its centre. In the example shown above, the kernel is square, with a dimension of seven pixels

To apply the kernel to the current pixel, an average of the the colour values of the pixels surrounding it is calculated, weighted by the values in the kernel. In a Gaussian blur, the pixels nearest the centre of the kernel are given more weight than those far away from the centre. The rate at which this weight diminishes is determined by a Gaussian function, hence the name Gaussian blur

A Gaussian function maps random variables into a normal distribution or “Bell Curve”.

Kiểu dữ liệu của hình ảnh trong python là gì?

https. //en. wikipedia. org/wiki/Gaussian_function#/media/File. Normal_Distribution_PDF. svg

The shape of the function is described by a mean value μ, and a variance value σ². The mean determines the central point of the bell curve on the x axis, and the variance describes the spread of the curve

In fact, when using Gaussian functions in Gaussian blurring, we use a 2D Gaussian function to account for X and Y dimensions, but the same rules apply. The mean μ is always 0, and represents the middle of the 2D kernel. Increasing values of σ² in either dimension increases the amount of blurring in that dimension

Kiểu dữ liệu của hình ảnh trong python là gì?

https. //commons. wikimedia. org/wiki/File. Gaussian_2D. png

The averaging is done on a channel-by-channel basis, and the average channel values become the new value for the pixel in the filtered image. Larger kernels have more values factored into the average, and this implies that a larger kernel will blur the image more than a smaller kernel

To get an idea of how this works, consider this plot of the two-dimensional Gaussian function

Kiểu dữ liệu của hình ảnh trong python là gì?

Imagine that plot laid over the kernel for the Gaussian blur filter. The height of the plot corresponds to the weight given to the underlying pixel in the kernel. I. e. , the pixels close to the centre become more important to the filtered pixel colour than the pixels close to the outer limits of the kernel. The shape of the Gaussian function is controlled via its standard deviation, or sigma. A large sigma value results in a flatter shape, while a smaller sigma value results in a more pronounced peak. The mathematics involved in the Gaussian blur filter are not quite that simple, but this explanation gives you the basic idea

To illustrate the blur process, consider the blue channel colour values from the seven-by-seven region of the cat image above

Kiểu dữ liệu của hình ảnh trong python là gì?

The filter is going to determine the new blue channel value for the centre pixel – the one that currently has the value 86. The filter calculates a weighted average of all the blue channel values in the kernel giving higher weight to the pixels near the centre of the kernel

Kiểu dữ liệu của hình ảnh trong python là gì?

This weighted average, the sum of the multiplications, becomes the new value for the centre pixel (3, 3). The same process would be used to determine the green and red channel values, and then the kernel would be moved over to apply the filter to the next pixel in the image

Image edges

Something different needs to happen for pixels near the outer limits of the image, since the kernel for the filter may be partially off the image. For example, what happens when the filter is applied to the upper-left pixel of the image? Here are the blue channel pixel values for the upper-left pixel of the cat image, again assuming a seven-by-seven kernel

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
44

The upper-left pixel is the one with value 4. Since the pixel is at the upper-left corner, there are no pixels underneath much of the kernel; here, this is represented by x’s. So, what does the filter do in that situation?

The default mode is to fill in the nearest pixel value from the image. For each of the missing x’s the image value closest to the x is used. If we fill in a few of the missing pixels, you will see how this works

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
45

Another strategy to fill those missing values is to reflect the pixels that are in the image to fill in for the pixels that are missing from the kernel

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
46

A similar process would be used to fill in all of the other missing pixels from the kernel. Other border modes are available; you can learn more about them in the skimage documentation

This animation shows how the blur kernel moves along in the original image in order to calculate the colour channel values for the blurred image

Kiểu dữ liệu của hình ảnh trong python là gì?

skimage has built-in functions to perform blurring for us, so we do not have to perform all of these mathematical operations ourselves. Let’s work through an example of blurring an image with the skimage Gaussian blur function

First, we load the image, and display it

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
47

Kiểu dữ liệu của hình ảnh trong python là gì?

Next, we apply the gaussian blur

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
48

The first two parameters to

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
788 are the image to blur,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
93, and a tuple defining the sigma to use in ry- and cx-direction,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
790. The third parameter
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
791 gives the radius of the kernel in terms of sigmas. A Gaussian function is defined from -infinity to +infinity, but our kernel (which must have a finite, smaller size) can only approximate the real function. Therefore, we must choose a certain distance from the centre of the function where we stop this approximation, and set the final size of our kernel. In the above example, we set
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
791 to 3. 5, which means the kernel size will be 2 * sigma * 3. 5. For example, for a
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
793 of 1. 0 the resulting kernel size would be 7, while for a
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
793 of 2. 0 the kernel size would be 14. The default value for
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
791 in scikit-image is 4. 0

The last parameter to

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
788 tells skimage to interpret our image, that has three dimensions, as a multichannel colour image

Finally, we display the blurred image

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
49

Kiểu dữ liệu của hình ảnh trong python là gì?

Experimenting with sigma values (10 min)

The size and shape of the kernel used to blur an image can have a significant effect on the result of the blurring and any downstream analysis carried out on the blurred image. The next two exercises ask you to experiment with the sigma values of the kernel, which is a good way to develop your understanding of how the choice of kernel can influence the result of blurring

First, try running the code above with a range of smaller and larger sigma values. Generally speaking, what effect does the sigma value have on the blurred image?

Solution

Generally speaking, the larger the sigma value, the more blurry the result. A larger sigma will tend to get rid of more noise in the image, which will help for other operations we will cover soon, such as thresholding. However, a larger sigma also tends to eliminate some of the detail from the image. So, we must strike a balance with the sigma value used for blur filters

Experimenting with kernel shape (10 min - optional, not included in timing)

Now, what is the effect of applying an asymmetric kernel to blurring an image? Try running the code above with different sigmas in the ry and cx direction. For example, a sigma of 1. 0 in the ry direction, and 6. 0 in the cx direction

Solution

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
50

Kiểu dữ liệu của hình ảnh trong python là gì?

These unequal sigma values produce a kernel that is rectangular instead of square. The result is an image that is much more blurred in the x direction than the y direction. For most use cases, a uniform blurring effect is desirable and this kind of asymmetric blurring should be avoided. However, it can be helpful in specific circumstances e. g. when noise is present in your image in a particular pattern or orientation, such as vertical lines, or when you want to remove uniform noise without blurring edges present in the image in a particular orientation

Other methods of blurring

The Gaussian blur is a way to apply a low-pass filter in skimage. It is often used to remove Gaussian (i. e. , random) noise from the image. For other kinds of noise, e. g. “salt and pepper” or “static” noise, a median filter is typically used. See the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
787 documentation for a list of available filters

Key Points

  • Applying a low-pass blurring filter smooths edges and removes noise from an image

  • Blurring is often used as a first step before we perform thresholding or edge detection

  • The Gaussian blur can be applied to an image with the

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    788 function

  • Larger sigma values may remove more noise, but they will also remove detail from an image


Thresholding

Tổng quan

Teaching. 60 min
Exercises. 50 min

Questions

  • How can we use thresholding to produce a binary image?

Objectives

  • Explain what thresholding is and how it can be used

  • Use histograms to determine appropriate threshold values to use for the thresholding process

  • Apply simple, fixed-level binary thresholding to an image

  • Explain the difference between using the operator

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    799 or the operator
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    900 to threshold an image represented by a numpy array

  • Describe the shape of a binary image produced by thresholding via

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    799 or
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    900

  • Explain when Otsu’s method for automatic thresholding is appropriate

  • Apply automatic thresholding to an image using Otsu’s method

  • Use the

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    903 function to count the number of non-zero pixels in an image

In this episode, we will learn how to use skimage functions to apply thresholding to an image. Thresholding is a type of image segmentation, where we change the pixels of an image to make the image easier to analyze. Trong ngưỡng, chúng tôi chuyển đổi một hình ảnh từ màu sắc hoặc thang độ xám thành một hình ảnh nhị phân, tôi. e. , one that is simply black and white. Most frequently, we use thresholding as a way to select areas of interest of an image, while ignoring the parts we are not concerned with. We have already done some simple thresholding, in the “Manipulating pixels” section of the Image Representation in skimage episode. In that case, we used a simple NumPy array manipulation to separate the pixels belonging to the root system of a plant from the black background. In this episode, we will learn how to use skimage functions to perform thresholding. Sau đó, chúng tôi sẽ sử dụng các mặt nạ được trả về bởi các chức năng này để chọn các phần của hình ảnh mà chúng tôi quan tâm

Simple thresholding

Consider the image

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
904 with a series of crudely cut shapes set against a white background

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
51

Kiểu dữ liệu của hình ảnh trong python là gì?

Now suppose we want to select only the shapes from the image. In other words, we want to leave the pixels belonging to the shapes “on,” while turning the rest of the pixels “off,” by setting their colour channel values to zeros. The skimage library has several different methods of thresholding. We will start with the simplest version, which involves an important step of human input. Cụ thể, trong ngưỡng đơn giản, mức cố định này, chúng tôi phải cung cấp giá trị ngưỡng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905

The process works like this. First, we will load the original image, convert it to grayscale, and de-noise it as in the Blurring Images episode

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
52

Kiểu dữ liệu của hình ảnh trong python là gì?

Next, we would like to apply the threshold

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905 such that pixels with grayscale values on one side of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905 will be turned “on”, while pixels with grayscale values on the other side will be turned “off”. How might we do that? Remember that grayscale images contain pixel values in the range from 0 to 1, so we are looking for a threshold
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905 in the closed range [0. 0, 1. 0]. We see in the image that the geometric shapes are “darker” than the white background but there is also some light gray noise on the background. One way to determine a “good” value for
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905 is to look at the grayscale histogram of the image and try to identify what grayscale ranges correspond to the shapes in the image or the background

The histogram for the shapes image shown above can be produced as in the Creating Histograms episode

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53

Kiểu dữ liệu của hình ảnh trong python là gì?

Vì hình ảnh có nền trắng nên hầu hết các pixel trong hình ảnh đều có màu trắng. This corresponds nicely to what we see in the histogram. there is a peak near the value of 1. 0. If we want to select the shapes and not the background, we want to turn off the white background pixels, while leaving the pixels for the shapes turned on. So, we should choose a value of

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905 somewhere before the large peak and turn pixels above that value “off”. Hãy để chúng tôi chọn
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
911

Để áp dụng ngưỡng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905, chúng ta có thể sử dụng toán tử so sánh numpy để tạo mặt nạ. Here, we want to turn “on” all pixels which have values smaller than the threshold, so we use the less operator
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
900 to compare the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
914 to the threshold
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905. Toán tử trả về một mặt nạ mà chúng ta chụp trong biến
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
916. Nó chỉ có một kênh và mỗi giá trị của nó là 0 hoặc 1. The binary mask created by the thresholding operation can be shown with
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
238, where the
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
266 entries are shown as black pixels (0-valued) and the
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265 entries are shown as white pixels (1-valued)

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
54

Kiểu dữ liệu của hình ảnh trong python là gì?

You can see that the areas where the shapes were in the original area are now white, while the rest of the mask image is black

What makes a good threshold?

As is often the case, the answer to this question is “it depends”. In the example above, we could have just switched off all the white background pixels by choosing

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
920, but this would leave us with some background noise in the mask image. Mặt khác, nếu chúng ta chọn giá trị quá thấp cho ngưỡng, chúng ta có thể mất một số hình dạng quá sáng. You can experiment with the threshold by re-running the above code lines with different values for
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905. In practice, it is a matter of domain knowledge and experience to interpret the peaks in the histogram so to determine an appropriate threshold. The process often involves trial and error, which is a drawback of the simple thresholding method. Dưới đây chúng tôi sẽ giới thiệu ngưỡng tự động, sử dụng định nghĩa toán học, định lượng cho một ngưỡng tốt cho phép chúng tôi tự động xác định giá trị của
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905. It is worth noting that the principle for simple and automatic thresholding can also be used for images with pixel ranges other than [0. 0, 1. 0]. For example, we could perform thresholding on pixel intensity values in the range [0, 255] as we have already seen in the Image Representation in skimage episode

We can now apply the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
916 to the original coloured image as we have learned in the Drawing and Bitwise Operations episode. Những gì chúng ta còn lại chỉ là những hình dạng màu từ bản gốc

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
55

Kiểu dữ liệu của hình ảnh trong python là gì?

More practice with simple thresholding (15 min)

Now, it is your turn to practice. Suppose we want to use simple thresholding to select only the coloured shapes (in this particular case we consider grayish to be a colour, too) from the image

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
924

Kiểu dữ liệu của hình ảnh trong python là gì?

Đầu tiên, vẽ biểu đồ thang độ xám như trong phần Tạo biểu đồ và kiểm tra sự phân bố của các giá trị thang độ xám trong ảnh. What do you think would be a good value for the threshold

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905?

Solution

The histogram for the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
924 image can be shown with

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56

Kiểu dữ liệu của hình ảnh trong python là gì?

We can see a large spike around 0. 3, and a smaller spike around 0. 7. Tăng đột biến gần 0. 3 đại diện cho nền tối hơn, vì vậy có vẻ như giá trị gần với

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
927 sẽ là một lựa chọn tốt

Tiếp theo, tạo mặt nạ để bật các pixel trên ngưỡng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905 và tắt các pixel dưới ngưỡng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905. Lưu ý rằng không giống như hình ảnh có nền trắng mà chúng tôi đã sử dụng ở trên, ở đây, đỉnh của màu nền ở mức xám thấp hơn so với hình dạng. Do đó, hãy thay đổi toán tử so sánh less
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
900 thành
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
799 lớn hơn để tạo mặt nạ phù hợp. Sau đó, áp dụng mặt nạ cho hình ảnh và xem hình ảnh ngưỡng. Nếu mọi thứ hoạt động bình thường, đầu ra của bạn sẽ chỉ hiển thị các hình có màu trên nền đen

Solution

Dưới đây là các lệnh để tạo và xem mặt nạ nhị phân

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
57

Kiểu dữ liệu của hình ảnh trong python là gì?

Và đây là các lệnh để áp dụng mặt nạ và xem hình ảnh ngưỡng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
58

Kiểu dữ liệu của hình ảnh trong python là gì?

Ngưỡng tự động

Nhược điểm của kỹ thuật tạo ngưỡng đơn giản là chúng ta phải đưa ra dự đoán có cơ sở về ngưỡng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905 bằng cách kiểm tra biểu đồ. Ngoài ra còn có các phương pháp ngưỡng tự động có thể tự động xác định ngưỡng cho chúng tôi. Một phương pháp như vậy là phương pháp của Otsu. Nó đặc biệt hữu ích cho các tình huống trong đó biểu đồ thang độ xám của ảnh có hai đỉnh tương ứng với nền và đối tượng quan tâm

Denoising an image before thresholding

Trong thực tế, thường cần phải khử nhiễu hình ảnh trước khi tạo ngưỡng, điều này có thể được thực hiện bằng một trong các phương pháp từ tập Hình ảnh mờ

Hãy xem xét hình ảnh

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
933 của hệ thống rễ ngô mà chúng ta đã thấy trước đây trong phần Biểu diễn bằng hình ảnh trong phần lướt qua

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
59

Kiểu dữ liệu của hình ảnh trong python là gì?

Chúng tôi sử dụng độ mờ Gaussian với sigma là 1. 0 to denoise the root image. Chúng ta hãy xem biểu đồ thang độ xám của hình ảnh được khử nhiễu

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
60

Kiểu dữ liệu của hình ảnh trong python là gì?

Biểu đồ có một đỉnh đáng kể xung quanh 0. 2, và một đỉnh thứ hai, nhỏ hơn rất gần 1. 0. Do đó, hình ảnh này là một ứng cử viên sáng giá để tạo ngưỡng bằng phương pháp của Otsu. Các chi tiết toán học về cách thức hoạt động của nó rất phức tạp (hãy xem tài liệu lướt qua nếu bạn quan tâm), nhưng kết quả là phương pháp của Otsu tìm thấy một giá trị ngưỡng giữa hai đỉnh của biểu đồ thang độ xám

Hàm

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
934 có thể được sử dụng để xác định ngưỡng tự động thông qua phương pháp của Otsu. Sau đó, các toán tử so sánh numpy có thể được sử dụng để áp dụng nó như trước đây. Dưới đây là các lệnh Python để xác định ngưỡng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905 bằng phương pháp của Otsu

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
61

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62

Đối với hình ảnh gốc này và độ mờ Gaussian với sigma đã chọn là 1. 0, giá trị ngưỡng tính toán là 0. 42. No we can create a binary mask with the comparison operator

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
799. Như chúng ta đã thấy trước đây, các điểm ảnh trên giá trị ngưỡng sẽ được bật, những điểm ảnh dưới ngưỡng sẽ bị tắt

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
63

Kiểu dữ liệu của hình ảnh trong python là gì?

Cuối cùng, chúng tôi sử dụng mặt nạ để chọn tiền cảnh

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
64

Kiểu dữ liệu của hình ảnh trong python là gì?

Application. đo khối lượng rễ

Bây giờ chúng ta hãy chuyển sang một ứng dụng trong đó chúng ta có thể áp dụng phân ngưỡng và các kỹ thuật khác mà chúng ta đã học được cho đến thời điểm này. Consider these four maize root system images, which you can find in the files

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
937,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
938,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
939, and
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
940

Kiểu dữ liệu của hình ảnh trong python là gì?

Giả sử chúng ta quan tâm đến số lượng vật liệu thực vật trong mỗi hình ảnh và đặc biệt là số lượng đó thay đổi như thế nào từ hình ảnh này sang hình ảnh khác. Có lẽ những hình ảnh đại diện cho sự phát triển của cây theo thời gian, hoặc có lẽ những hình ảnh cho thấy bốn giống ngô khác nhau ở cùng một giai đoạn phát triển của chúng. Câu hỏi mà chúng tôi muốn trả lời là "khối lượng gốc trong mỗi hình ảnh là bao nhiêu?"

Trước tiên, chúng tôi sẽ xây dựng một chương trình Python để đo giá trị này cho một hình ảnh. Chiến lược của chúng tôi sẽ là thế này

  1. Đọc hình ảnh, chuyển đổi nó thành thang độ xám khi nó được đọc. Đối với ứng dụng này, chúng tôi không cần hình ảnh màu
  2. Làm mờ hình ảnh
  3. Use Otsu’s method of thresholding to create a binary image, where the pixels that were part of the maize plant are white, and everything else is black
  4. Lưu hình ảnh nhị phân để có thể kiểm tra sau
  5. Count the white pixels in the binary image, and divide by the number of pixels in the image. Tỷ lệ này sẽ là thước đo khối lượng rễ của cây trong ảnh
  6. Xuất tên của hình ảnh được xử lý và tỷ lệ khối lượng gốc

Mục đích của chúng tôi là thực hiện các bước này và tạo ra kết quả số - thước đo khối lượng rễ trong ảnh - mà không cần sự can thiệp của con người. Implementing the steps within a Python function will enable us to call this function for different images

Here is a Python function that implements this root-mass-measuring strategy. Vì chức năng nhằm tạo ra đầu ra số mà không có sự tương tác của con người, nên nó không hiển thị bất kỳ hình ảnh nào. Hầu như tất cả các lệnh đều quen thuộc và trên thực tế, nó có vẻ đơn giản hơn mã mà chúng tôi đã làm việc cho đến nay, bởi vì chúng tôi không hiển thị bất kỳ hình ảnh nào

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
65

Chức năng bắt đầu bằng việc đọc ảnh gốc từ tệp

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
941. Chúng tôi sử dụng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62 với đối số tùy chọn
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
235 để tự động chuyển đổi nó thành thang độ xám. Tiếp theo, hình ảnh thang độ xám được làm mờ bằng bộ lọc Gaussian với giá trị
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
793 được truyền cho hàm. Sau đó, chúng tôi xác định ngưỡng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
905 bằng phương pháp của Otsu và tạo mặt nạ nhị phân giống như chúng tôi đã làm trong phần trước. Đến thời điểm này, mọi thứ chắc đã quen thuộc

Phần cuối cùng của chức năng xác định tỷ lệ khối lượng gốc trong hình ảnh. Nhớ lại rằng trong

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
916, mỗi pixel có giá trị bằng 0 (đen/nền) hoặc một (trắng/nền trước). Chúng tôi muốn đếm số pixel trắng, có thể được thực hiện bằng cách gọi hàm numpy
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
947. Sau đó, chúng tôi xác định chiều rộng và chiều cao của hình ảnh bằng cách sử dụng các phần tử của
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
948 (nghĩa là kích thước của mảng numpy lưu trữ hình ảnh). Cuối cùng, tỷ lệ mật độ được tính bằng cách chia số pixel trắng cho tổng số pixel
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
949 trong ảnh. Hàm trả về mật độ gốc của hình ảnh

Chúng ta có thể gọi hàm này với bất kỳ tên tệp nào và cung cấp giá trị sigma để làm mờ. Nếu không có giá trị sigma nào được cung cấp, giá trị mặc định 1. 0 sẽ được sử dụng. Ví dụ: đối với tệp

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
937 và giá trị sigma là 1. 5, chúng ta sẽ gọi hàm như thế này

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
66

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
67

Bây giờ chúng ta có thể sử dụng chức năng để xử lý chuỗi bốn hình ảnh được hiển thị ở trên. Trong một tình huống khoa học trong thế giới thực, có thể có hàng chục, hàng trăm hoặc thậm chí hàng nghìn hình ảnh cần xử lý. Để tiết kiệm cho chúng tôi sự tẻ nhạt khi gọi hàm cho từng hình ảnh bằng tay, chúng tôi có thể viết một vòng lặp xử lý tất cả các tệp tự động. Khối mã sau đây giả định rằng các tệp được đặt trong cùng một thư mục và tất cả các tên tệp đều bắt đầu bằng tiền tố dùng thử và kết thúc bằng tiền tố. hậu tố jpg

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
68

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
69

Ignoring more of the images – brainstorming (10 min)

Let us take a closer look at the binary masks produced by the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
951 function

Kiểu dữ liệu của hình ảnh trong python là gì?

You may have noticed in the section on automatic thresholding that the thresholded image does include regions of the image aside of the plant root. the numbered labels and the white circles in each image are preserved during the thresholding, because their grayscale values are above the threshold. Do đó, tỷ lệ khối lượng rễ được tính toán của chúng tôi bao gồm các điểm ảnh màu trắng của nhãn và vòng tròn màu trắng không phải là một phần của rễ cây. Those extra pixels affect how accurate the root mass calculation is

Làm cách nào chúng tôi có thể xóa nhãn và vòng tròn trước khi tính toán tỷ lệ để kết quả của chúng tôi chính xác hơn?

Solution

One approach we might take is to try to completely mask out a region from each image, particularly, the area containing the white circle and the numbered label. If we had coordinates for a rectangular area on the image that contained the circle and the label, we could mask the area out easily by using techniques we learned in the Drawing and Bitwise Operations episode

However, a closer inspection of the binary images raises some issues with that approach. Since the roots are not always constrained to a certain area in the image, and since the circles and labels are in different locations each time, we would have difficulties coming up with a single rectangle that would work for every image. We could create a different masking rectangle for each image, but that is not a practicable approach if we have hundreds or thousands of images to process

Another approach we could take is to apply two thresholding steps to the image. Look at the graylevel histogram of the file

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
937 shown above again. Notice the peak near 1. 0? Recall that a grayscale value of 1. 0 corresponds to white pixels. the peak corresponds to the white label and circle. So, we could use simple binary thresholding to mask the white circle and label from the image, and then we could use Otsu’s method to select the pixels in the plant portion of the image

Note that most of this extra work in processing the image could have been avoided during the experimental design stage, with some careful consideration of how the resulting images would be used. For example, all of the following measures could have made the images easier to process, by helping us predict and/or detect where the label is in the image and subsequently mask it from further processing

  • Using labels with a consistent size and shape
  • Placing all the labels in the same position, relative to the sample
  • Using a non-white label, with non-black writing

Ignoring more of the images – implementation (30 min - optional, not included in timing)

Implement an enhanced version of the function

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
951 that applies simple binary thresholding to remove the white circle and label from the image before applying Otsu’s method

Solution

We can apply a simple binary thresholding with a threshold

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
954 to remove the label and circle from the image. We use the binary mask to set the pixels in the blurred image to zero (black)

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
70

The output of the improved program does illustrate that the white circles and labels were skewing our root mass ratios

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
71

Here are the binary images produced by the additional thresholding. Note that we have not completely removed the offending white pixels. Outlines still remain. Tuy nhiên, chúng tôi đã giảm số lượng pixel không liên quan, điều này sẽ làm cho đầu ra chính xác hơn

Kiểu dữ liệu của hình ảnh trong python là gì?

Thresholding a bacteria colony image (15 min)

In the images directory

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
74, you will find an image named
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
956

Kiểu dữ liệu của hình ảnh trong python là gì?

This is one of the images you will be working with in the morphometric challenge at the end of the workshop

  1. Plot and inspect the grayscale histogram of the image to determine a good threshold value for the image
  2. Tạo một mặt nạ nhị phân để các điểm ảnh trong quần thể vi khuẩn “bật” trong khi “tắt” các điểm ảnh còn lại trong hình ảnh

Solution

Here is the code to create the grayscale histogram

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
72

Kiểu dữ liệu của hình ảnh trong python là gì?

Đỉnh gần một tương ứng với nền hình ảnh màu trắng và đỉnh rộng hơn xung quanh 0. 5 corresponds to the yellow/brown culture medium in the dish. Đỉnh nhỏ gần bằng 0 là những gì chúng ta đang theo đuổi. các khuẩn lạc vi khuẩn tối. Do đó, một lựa chọn hợp lý có thể là để các pixel bên dưới

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
957 trên

Here is the code to create and show the binarized image using the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
900 operator with a threshold
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
957

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
73

Kiểu dữ liệu của hình ảnh trong python là gì?

When you experiment with the threshold a bit, you can see that in particular the size of the bacteria colony near the edge of the dish in the top right is affected by the choice of the threshold

Key Points

  • Ngưỡng tạo ra một hình ảnh nhị phân, trong đó tất cả các pixel có cường độ trên (hoặc dưới) giá trị ngưỡng được bật, trong khi tất cả các pixel khác bị tắt

  • Các hình ảnh nhị phân được tạo bởi ngưỡng được giữ trong các mảng NumPy hai chiều, vì chúng chỉ có một kênh giá trị màu. Chúng là boolean, do đó chúng chứa các giá trị 0 (tắt) và 1 (bật)

  • Ngưỡng có thể được sử dụng để tạo mặt nạ chỉ chọn các phần thú vị của hình ảnh hoặc là bước đầu tiên trước khi phát hiện cạnh hoặc tìm đường viền


Phân tích thành phần được kết nối

Tổng quan

Dạy học. 70 phút
Bài tập. 55 min

Questions

  • Cách trích xuất các đối tượng riêng biệt từ một hình ảnh và mô tả các đối tượng này một cách định lượng

Objectives

  • Understand the term object in the context of images

  • Tìm hiểu về kết nối pixel

  • Learn how Connected Component Analysis (CCA) works

  • Use CCA to produce an image that highlights every object in a different colour

  • Characterise each object with numbers that describe its appearance

Các đối tượng

Trong phần Ngưỡng, chúng ta đã đề cập đến việc chia một hình ảnh thành các pixel nền trước và nền sau. Trong hình ảnh ví dụ về hình dạng, chúng tôi coi các hình dạng có màu là đối tượng tiền cảnh trên nền trắng

Kiểu dữ liệu của hình ảnh trong python là gì?

In thresholding we went from the original image to this version

Kiểu dữ liệu của hình ảnh trong python là gì?

Here, we created a mask that only highlights the parts of the image that we find interesting, the objects. Tất cả các đối tượng có giá trị pixel là ________ 2265 trong khi pixel nền là ________ 2266

Bằng cách nhìn vào hình ảnh mặt nạ, người ta có thể đếm các đối tượng có trong hình ảnh (7). Nhưng làm cách nào chúng tôi thực sự làm được điều đó, làm cách nào chúng tôi quyết định được cụm pixel nào tạo thành một đối tượng?

Vùng lân cận pixel

Để quyết định pixel nào thuộc cùng một đối tượng, người ta có thể khai thác vùng lân cận của chúng. pixel trực tiếp cạnh nhau và thuộc về lớp tiền cảnh có thể được coi là thuộc về cùng một đối tượng

Hãy thảo luận chi tiết hơn về khái niệm vùng lân cận pixel. Xem xét mặt nạ “hình ảnh” sau đây với 8 hàng và 8 cột. Với mục đích minh họa, chữ số

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
764 được sử dụng để biểu thị pixel nền và chữ cái
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
963 được sử dụng để biểu thị pixel đối tượng nền trước)

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
74

Các pixel được tổ chức trong một lưới hình chữ nhật. Để hiểu các vùng lân cận pixel, chúng tôi sẽ giới thiệu khái niệm "nhảy" giữa các pixel. Các bước nhảy tuân theo hai quy tắc. Nguyên tắc đầu tiên là chỉ được phép nhảy một lần dọc theo cột hoặc hàng. Nhảy chéo không được phép. Vì vậy, từ một pixel trung tâm, được biểu thị bằng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
964, chỉ các pixel được biểu thị bằng
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
273 mới có thể truy cập được

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
75

Các pixel trên đường chéo (từ

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
964) không thể truy cập được bằng một lần nhảy, được biểu thị bằng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
967. Các pixel có thể truy cập bằng một bước nhảy duy nhất tạo thành vùng lân cận 1 bước nhảy

Quy tắc thứ hai quy định rằng trong một chuỗi nhảy, người ta chỉ được nhảy theo hướng hàng và cột một lần -> chúng phải trực giao. Một ví dụ về chuỗi các bước nhảy trực giao được hiển thị bên dưới. Bắt đầu từ

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
964, bước nhảy đầu tiên đi dọc theo hàng bên phải. Bước nhảy thứ hai sau đó đi dọc theo hướng cột lên. Sau đó, trình tự không thể tiếp tục vì một bước nhảy đã được thực hiện theo cả hướng hàng và cột

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
76

Tất cả các pixel có thể truy cập bằng một hoặc hai bước nhảy từ vùng lân cận 2 bước nhảy. Lưới bên dưới minh họa các pixel có thể truy cập từ pixel trung tâm

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
964 bằng một lần nhảy, được đánh dấu bằng
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
273 và các pixel có thể truy cập bằng 2 lần nhảy với một
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
769

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
77

We want to revisit our example image mask from above and apply the two different neighborhood rules. Với một kết nối nhảy duy nhất cho mỗi pixel, chúng tôi nhận được hai đối tượng kết quả, được đánh dấu trong ảnh bằng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
972 và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
973

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
78

Trong phiên bản 1 lần nhảy, chỉ những pixel có hàng xóm trực tiếp dọc theo hàng hoặc cột mới được coi là đã kết nối. Các kết nối chéo không được bao gồm trong vùng lân cận 1 bước nhảy. Tuy nhiên, với hai bước nhảy, chúng tôi chỉ nhận được một đối tượng duy nhất

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
972 vì các pixel cũng được coi là được kết nối dọc theo các đường chéo

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
79

Đếm đối tượng (tùy chọn, không bao gồm trong thời gian)

Có bao nhiêu đối tượng có 1 bước nhảy trực giao, bao nhiêu đối tượng có 2 bước nhảy trực giao?

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
00

1 lần nhảy

a) 1 b) 5 c) 2

Solution

b) 5

2 lần nhảy

a) 2 b) 3 c) 5

Solution

a) 2

Bước nhảy và vùng lân cận

Chúng tôi vừa giới thiệu cách bạn có thể tiếp cận các pixel lân cận khác nhau bằng cách thực hiện một hoặc nhiều bước nhảy trực giao. Chúng tôi đã sử dụng thuật ngữ lân cận 1 bước nhảy và 2 bước nhảy. Ngoài ra còn có một cách khác để đề cập đến những khu phố này. khu phố 4 và 8. With a single jump you can reach four pixels from a given starting pixel. Do đó, lân cận 1 bước nhảy tương ứng với lân cận 4. Khi cho phép hai bước nhảy trực giao, có thể đạt tới 8 pixel, do đó, vùng lân cận 2 bước nhảy tương ứng với vùng lân cận 8

Phân tích thành phần được kết nối

Để tìm các đối tượng trong một hình ảnh, chúng tôi muốn sử dụng một thao tác được gọi là Phân tích thành phần được kết nối (CCA). Thao tác này lấy một hình ảnh nhị phân làm đầu vào. Thông thường, giá trị

"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
266 trong hình ảnh này được liên kết với pixel nền và giá trị
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265 biểu thị pixel tiền cảnh hoặc đối tượng. Một hình ảnh như vậy có thể được tạo ra, e. g. , với ngưỡng. Đưa ra một hình ảnh ngưỡng, phân tích thành phần được kết nối tạo ra một hình ảnh được dán nhãn mới với các giá trị pixel nguyên. Các pixel có cùng giá trị, thuộc về cùng một đối tượng. Skimage cung cấp phân tích thành phần được kết nối trong chức năng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
977. Hãy để chúng tôi thêm chức năng này vào các bước đã quen thuộc để tạo ngưỡng cho một hình ảnh. Ở đây chúng tôi định nghĩa một hàm Python có thể tái sử dụng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
978

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
01

Lưu ý nhập mới của

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
979 để sử dụng chức năng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
980 thực hiện CCA. Bốn dòng mã đầu tiên quen thuộc từ tập Thresholding

Sau đó, chúng tôi gọi hàm

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
980. Hàm này có một đối số vị trí trong đó chúng tôi vượt qua
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
916, i. e. , hình ảnh nhị phân để làm việc trên. Với đối số tùy chọn
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
983, chúng tôi chỉ định vùng lân cận theo đơn vị bước nhảy trực giao. Ví dụ: bằng cách đặt
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
984, chúng tôi sẽ xem xét vùng lân cận 2 bước nhảy được giới thiệu ở trên. Hàm trả về một
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985 trong đó mỗi pixel có một giá trị duy nhất tương ứng với đối tượng mà nó thuộc về. Ngoài ra, chúng tôi chuyển tham số tùy chọn
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
986 để trả về chỉ số nhãn tối đa là
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
987

Tham số tùy chọn và giá trị trả về

Tham số tùy chọn

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
988 thay đổi kiểu dữ liệu được trả về bởi hàm
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
980. Số lượng nhãn chỉ được trả về nếu
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
988 là True. Mặt khác, hàm chỉ trả về hình ảnh được dán nhãn. Điều này có nghĩa là chúng ta phải chú ý khi gán giá trị trả về cho một biến. Nếu chúng ta bỏ qua tham số tùy chọn
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
988 hoặc vượt qua
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
992, chúng ta có thể gọi hàm là

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
02

Nếu chúng ta vượt qua

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
986, hàm sẽ trả về một bộ và chúng ta có thể gán nó là

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
03

Nếu chúng ta sử dụng phép gán giống như trong trường hợp đầu tiên, biến

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985 sẽ trở thành một bộ, trong đó
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
995 là hình ảnh và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
996 là số lượng nhãn. Điều này có thể gây nhầm lẫn nếu chúng ta cho rằng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985 chỉ chứa hình ảnh và chuyển nó đến các chức năng khác. Nếu bạn nhận được một
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
998 hoặc tương tự, hãy kiểm tra xem bạn đã chỉ định các giá trị trả về nhất quán với các tham số tùy chọn chưa

Chúng ta có thể gọi hàm trên

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
978 và hiển thị hình ảnh được gắn nhãn như vậy

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
04

ánh xạ màu

Tại đây, bạn có thể nhận được cảnh báo

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
000 hoặc chỉ thấy một hình ảnh toàn màu đen (Lưu ý. hành vi này có thể thay đổi trong các phiên bản tương lai hoặc không xảy ra với trình xem ảnh khác)

Có chuyện gì? . Bạn có thể thấy rằng một số pixel có giá trị khác với

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
764, vì vậy chúng không thực sự có màu đen thuần khiết. Hãy tìm hiểu thêm bằng cách kiểm tra
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985. Các thuộc tính có thể thú vị trong ngữ cảnh này là
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
003, giá trị tối thiểu và tối đa. Chúng ta có thể in chúng với các dòng sau

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
05

Kiểm tra đầu ra có thể cho chúng ta manh mối tại sao hình ảnh có màu đen

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
06

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
003 của
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985 là
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
006. Điều này có nghĩa là các giá trị trong phạm vi hình ảnh này từ
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
007 đến
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
008. Đó là những con số thực sự lớn. Từ không gian có sẵn này, chúng tôi chỉ sử dụng phạm vi từ
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
764 đến
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
010. Khi hiển thị hình ảnh này trong trình xem, nó sẽ nén phạm vi hoàn chỉnh thành 256 giá trị màu xám. Do đó, phạm vi số của chúng tôi không tạo ra bất kỳ thay đổi rõ ràng nào

May mắn thay, thư viện đọc lướt có các công cụ để đối phó với tình huống này

Ta có thể dùng hàm

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
011 để convert màu trong ảnh (nhớ lại ta đã dùng hàm
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
227 để convert sang grayscale). Với
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
011, tất cả các đối tượng được tô màu theo danh sách màu có thể tùy chỉnh. Chúng ta có thể sử dụng các lệnh sau để chuyển đổi và hiển thị hình ảnh

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
07

Kiểu dữ liệu của hình ảnh trong python là gì?

Có bao nhiêu đối tượng trong hình ảnh đó (15 phút)

Bây giờ, đến lượt bạn thực hành. Sử dụng hàm

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
978, tìm hai cách để in ra số lượng đối tượng được tìm thấy trong hình ảnh

Bạn muốn nhận được bao nhiêu đối tượng?

Làm thế nào để thay đổi các giá trị

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
793 và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
016 ảnh hưởng đến kết quả?

Solution

Như bạn có thể đoán, giá trị trả về

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
987 đã chứa số lượng hình ảnh được tìm thấy. Vì vậy, nó có thể được in đơn giản với

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
08

Nhưng cũng có một cách để có được số lượng đối tượng được tìm thấy từ chính hình ảnh được gắn nhãn. Nhớ lại rằng tất cả các pixel thuộc về một đối tượng được gán cùng một giá trị số nguyên. Thuật toán thành phần được kết nối tạo ra các số liên tiếp. Nền nhận giá trị

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
764, đối tượng đầu tiên nhận giá trị
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
273, đối tượng thứ hai nhận giá trị
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
769, v.v. Điều này có nghĩa là bằng cách tìm đối tượng có giá trị lớn nhất, chúng ta cũng biết có bao nhiêu đối tượng trong ảnh. Do đó, chúng ta có thể sử dụng hàm
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
021 từ Numpy để tìm giá trị lớn nhất bằng với số đối tượng được tìm thấy

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
09

Gọi hàm với

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
022 và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
023, cả hai phương thức sẽ in

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
10

Giảm ngưỡng sẽ dẫn đến ít đối tượng hơn. Ngưỡng được đặt càng cao, càng nhiều đối tượng được tìm thấy. Ngày càng có nhiều tiếng ồn xung quanh được chọn làm đối tượng. Các sigma lớn hơn tạo ra các mặt nạ nhị phân với ít nhiễu hơn và do đó số lượng đối tượng ít hơn. Đặt sigma quá cao có nguy cơ hợp nhất các đối tượng

Bạn có thể thắc mắc tại sao phân tích thành phần được kết nối với

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
022 và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
023 tìm thấy 11 đối tượng, trong khi chúng tôi mong đợi chỉ có 7 đối tượng. Bốn đối tượng bổ sung ở đâu?

Kiểu dữ liệu của hình ảnh trong python là gì?

Đối với chúng tôi, rõ ràng những điểm nhỏ này là đồ tạo tác chứ không phải đối tượng mà chúng tôi quan tâm. Nhưng làm thế nào chúng ta có thể nói với máy tính? . Trong một số trường hợp, tiếng ồn xung quanh được coi là một đối tượng. Và với các tham số khác, một số đối tượng nền trước bị vỡ hoặc biến mất hoàn toàn. Do đó, chúng ta cần các tiêu chí khác để mô tả các thuộc tính mong muốn của các đối tượng được tìm thấy.

Hình thái học - Mô tả đặc điểm đối tượng bằng các con số

Hình thái học liên quan đến phân tích định lượng của các đối tượng và xem xét các thuộc tính như kích thước và hình dạng. Đối với ví dụ về hình ảnh có hình dạng, trực giác của chúng ta cho chúng ta biết rằng các đối tượng phải có kích thước hoặc diện tích nhất định. Vì vậy, chúng ta có thể sử dụng diện tích tối thiểu làm tiêu chí khi phát hiện một đối tượng. Để áp dụng một tiêu chí như vậy, chúng ta cần một cách để tính diện tích của các đối tượng được tìm thấy bởi các thành phần được kết nối. Nhớ lại cách chúng ta xác định khối lượng gốc trong phần Ngưỡng bằng cách đếm các pixel trong mặt nạ nhị phân. Nhưng ở đây chúng tôi muốn tính diện tích của một số đối tượng trong hình ảnh được dán nhãn. Thư viện skipage cung cấp hàm

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
028 để đo các thuộc tính của vùng được dán nhãn. Nó trả về một danh sách
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
029 mô tả từng vùng được kết nối trong ảnh. Các thuộc tính có thể được truy cập bằng cách sử dụng các thuộc tính của kiểu dữ liệu
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
029. Ở đây chúng ta sẽ sử dụng các thuộc tính
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
031 và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
032. Bạn có thể khám phá tài liệu về lướt web để tìm hiểu về các thuộc tính khác có sẵn

Chúng ta có thể nhận được một danh sách các khu vực của các đối tượng được dán nhãn như sau

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
11

Điều này sẽ tạo ra đầu ra

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
12

Plot a histogram of the object area distribution (10 min)

Tương tự như cách chúng tôi xác định ngưỡng “tốt” trong phần Ngưỡng, việc kiểm tra biểu đồ của thuộc tính đối tượng thường rất hữu ích. Ví dụ: chúng tôi muốn xem xét sự phân bố của các khu vực đối tượng

  1. Tạo và kiểm tra biểu đồ của các khu vực đối tượng thu được bằng
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    028
  2. Biểu đồ cho bạn biết gì về các đối tượng?

Solution

Biểu đồ có thể được vẽ với

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
13

Kiểu dữ liệu của hình ảnh trong python là gì?

Biểu đồ hiển thị số lượng đối tượng (trục tung) có diện tích nằm trong một phạm vi nhất định (trục hoành). Chiều cao của các thanh trong biểu đồ biểu thị mức độ phổ biến của các đối tượng có diện tích nhất định. Toàn bộ biểu đồ cho chúng ta biết về sự phân bố kích thước đối tượng trong ảnh. Thường có thể xác định khoảng cách giữa các nhóm thanh (hoặc đỉnh nếu chúng ta vẽ biểu đồ dưới dạng một đường cong liên tục) cho chúng ta biết về một số nhóm nhất định trong ảnh

Trong ví dụ này, chúng ta có thể thấy rằng có bốn đối tượng nhỏ chứa ít hơn 50000 pixel. Sau đó, có một nhóm bốn đối tượng (1+1+2) trong phạm vi từ 200000 đến 400000 và ba đối tượng có kích thước khoảng 500000. Đối với số lượng đối tượng của chúng tôi, chúng tôi có thể muốn coi các đối tượng nhỏ là hiện vật, tôi. e, chúng tôi muốn bỏ qua thanh ngoài cùng bên trái của biểu đồ. Chúng ta có thể sử dụng ngưỡng 50000 làm diện tích tối thiểu để tính. Trên thực tế, danh sách

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
034 đã cho chúng ta biết rằng có ít hơn 200 pixel trong các đối tượng này. Do đó, thật hợp lý khi yêu cầu diện tích tối thiểu ít nhất 200 pixel cho một đối tượng được phát hiện. Trong thực tế, việc tìm ra ngưỡng “đúng” có thể phức tạp và thường liên quan đến phỏng đoán dựa trên kiến ​​thức về lĩnh vực

Lọc các đối tượng theo khu vực (10 phút)

Bây giờ chúng tôi muốn sử dụng tiêu chí diện tích tối thiểu để có được số lượng đối tượng trong ảnh chính xác hơn

  1. Tìm cách tính số đồ vật bằng cách chỉ đếm những đồ vật ở trên một diện tích nhất định

Solution

Một cách để chỉ đếm các đối tượng phía trên một khu vực nhất định là trước tiên tạo danh sách các đối tượng đó, sau đó lấy độ dài của danh sách đó làm số lượng đối tượng. Điều này có thể được thực hiện như sau

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
14

Một tùy chọn khác là sử dụng mảng Numpy để tạo danh sách các đối tượng lớn. Đầu tiên chúng ta tạo một mảng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
034 chứa các vùng đối tượng và một mảng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
036 chứa các nhãn đối tượng. Nhãn của các đối tượng cũng được trả về bởi
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
028. Chúng ta đã thấy rằng chúng ta có thể tạo các mảng boolean bằng cách sử dụng các toán tử so sánh. Ở đây chúng ta có thể sử dụng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
038 để tạo ra một mảng có cùng kích thước với
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
036. Sau đó, nó có thể được sử dụng để chọn nhãn của các đối tượng có diện tích lớn hơn
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
040 bằng cách lập chỉ mục

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
15

Ưu điểm của việc sử dụng mảng Numpy là các vòng lặp

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
755 và câu lệnh
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
042 trong Python có thể chậm và trong thực tế, cách tiếp cận đầu tiên có thể không khả thi nếu hình ảnh chứa một số lượng lớn đối tượng. Trong trường hợp đó, các hàm mảng Numpy trở nên rất hữu ích vì chúng nhanh hơn nhiều

Trong ví dụ này, chúng ta cũng có thể sử dụng hàm

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
947 mà chúng ta đã thấy trước đó cùng với toán tử
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
799 để đếm các đối tượng có diện tích trên
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
040

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
16

Đối với cả ba phương án, đầu ra giống nhau và đưa ra số lượng dự kiến ​​là 7 đối tượng

Sử dụng các chức năng từ Numpy và các gói Python khác

Các chức năng từ các gói Python như Numpy thường hiệu quả hơn và yêu cầu ít mã hơn để viết. Bạn nên duyệt qua các trang tham khảo của

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
33 và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32 để tìm một hàm khả dụng có thể giải quyết một nhiệm vụ nhất định

Loại bỏ các vật thể nhỏ (20 phút)

Chúng tôi cũng có thể muốn loại trừ (che dấu) các đối tượng nhỏ khi vẽ hình ảnh được dán nhãn

  1. Nâng cao chức năng
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    978 để nó tự động loại bỏ các đối tượng bên dưới một khu vực nhất định được chuyển đến chức năng dưới dạng tham số tùy chọn

Solution

Để loại bỏ các đối tượng nhỏ khỏi hình ảnh được dán nhãn, chúng tôi thay đổi giá trị của tất cả các pixel thuộc về các đối tượng nhỏ thành nhãn nền 0. Một cách để làm điều này là lặp qua tất cả các đối tượng và đặt các pixel khớp với nhãn của đối tượng thành 0

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
17

Ở đây, các hàm Numpy cũng có thể được sử dụng để loại bỏ các vòng lặp

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
755 và các câu lệnh
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
042. Giống như trên, chúng ta có thể tạo một mảng các nhãn đối tượng nhỏ bằng phép so sánh
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
051. Chúng ta có thể sử dụng một hàm Numpy khác,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
052, để đặt pixel của tất cả các đối tượng nhỏ thành 0.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
052 nhận hai mảng và trả về một mảng boolean với các giá trị
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265 nếu mục nhập của mảng đầu tiên được tìm thấy trong mảng thứ hai và
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
266 nếu không. Sau đó, mảng này có thể được sử dụng để lập chỉ mục cho
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985 và đặt các mục thuộc về các đối tượng nhỏ thành
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
764

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
18

Một cách thanh lịch hơn nữa để xóa các đối tượng nhỏ khỏi hình ảnh là tận dụng mô-đun

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
058. Nó cung cấp một chức năng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
059 thực hiện chính xác những gì chúng tôi đang tìm kiếm. Nó có thể được áp dụng cho một hình ảnh nhị phân và trả về một mặt nạ trong đó tất cả các đối tượng nhỏ hơn
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
040 đều bị loại trừ, tôi. e, giá trị pixel của chúng được đặt thành
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
266. Sau đó, chúng tôi có thể áp dụng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
980 cho hình ảnh được che dấu

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
19

Sử dụng các tính năng của

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32, chúng tôi có thể triển khai
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
064 như sau

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
20

Bây giờ chúng ta có thể gọi hàm với một

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
040 đã chọn và hiển thị hình ảnh được gắn nhãn kết quả

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
21

Kiểu dữ liệu của hình ảnh trong python là gì?

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
22

Chú ý các đối tượng nhỏ đã “ra đi” và ta thu được đúng số lượng 7 đối tượng trong ảnh

Tô màu đối tượng theo khu vực (tùy chọn, không bao gồm trong thời gian)

Cuối cùng, chúng tôi muốn hiển thị hình ảnh với các đối tượng được tô màu theo độ lớn của khu vực của chúng. Trong thực tế, điều này có thể được sử dụng với các thuộc tính khác để đưa ra các dấu hiệu trực quan về các thuộc tính của đối tượng

Solution

Chúng ta đã biết cách lấy diện tích của các đối tượng từ

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
066. Chúng ta chỉ cần chèn một giá trị vùng 0 cho nền (để tô màu nó giống như một đối tượng có kích thước bằng 0). Nền cũng được gắn nhãn
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
764 trong
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985, vì vậy chúng tôi chèn giá trị vùng 0 vào trước phần tử đầu tiên của
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
034 với
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
070. Sau đó, chúng tôi có thể tạo một
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
071 nơi chúng tôi chỉ định từng giá trị pixel cho khu vực bằng cách lập chỉ mục cho
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
034 với các giá trị nhãn trong
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
23

Kiểu dữ liệu của hình ảnh trong python là gì?

Bạn có thể nhận thấy rằng trong giải pháp, chúng tôi đã sử dụng

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985 để lập chỉ mục cho mảng
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
034. Đây là một ví dụ về lập chỉ mục nâng cao trong Numpy. Kết quả là một mảng có hình dạng giống như
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
985 có giá trị pixel được chọn từ
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
034 theo nhãn đối tượng. Do đó các đối tượng sẽ được tô màu theo khu vực khi kết quả được hiển thị. Lưu ý rằng lập chỉ mục nâng cao với một mảng số nguyên hoạt động hơi khác so với lập chỉ mục với mảng Boolean mà chúng tôi đã sử dụng để tạo mặt nạ. Trong khi lập chỉ mục mảng Boolean chỉ trả về các mục tương ứng với các giá trị
"""
 * Python libraries for learning and performing image processing.
 *
"""
import numpy as np
import matplotlib.pyplot as plt
import ipympl
import imageio.v3 as iio
import skimage
265 của chỉ mục, lập chỉ mục mảng số nguyên trả về một mảng có cùng hình dạng với chỉ mục. Bạn có thể đọc thêm về lập chỉ mục nâng cao trong tài liệu Numpy

Key Points

  • Chúng ta có thể sử dụng

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    980 để tìm và gắn nhãn các đối tượng được kết nối trong một hình ảnh

  • Chúng ta có thể sử dụng

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    028 để đo thuộc tính của các đối tượng được dán nhãn

  • Chúng ta có thể sử dụng

    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw import disk  # form 3, load only the disk function
    import numpy as np             # form 4, load all of numpy into an object called np
    
    059 để che các đối tượng nhỏ và xóa các tạo tác khỏi hình ảnh

  • Chúng tôi có thể hiển thị hình ảnh được dán nhãn để xem các đối tượng được tô màu theo nhãn


Thử thách đỉnh cao

Tổng quan

Dạy học. 10 phút
Bài tập. 40 phút

Questions

  • Làm thế nào chúng ta có thể tự động đếm các khuẩn lạc vi khuẩn bằng phân tích hình ảnh?

Objectives

  • Tập hợp mọi thứ bạn đã học cho đến nay để đếm số khuẩn lạc trong 3 hình ảnh

Trong tập này, chúng tôi sẽ đưa ra thử thách cuối cùng để bạn thử sức, dựa trên tất cả các kỹ năng bạn đã đạt được cho đến nay. Thử thách này sẽ liên quan đến hình dạng của đối tượng trong ảnh (hình thái học)

hình thái học. Đếm khuẩn lạc vi khuẩn

Như đã đề cập trong phần giới thiệu hội thảo, thách thức hình thái học của bạn là xác định có bao nhiêu khuẩn lạc trong mỗi hình ảnh này

Kiểu dữ liệu của hình ảnh trong python là gì?

Kiểu dữ liệu của hình ảnh trong python là gì?

Kiểu dữ liệu của hình ảnh trong python là gì?

Các tệp hình ảnh có thể được tìm thấy tại

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
082,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
083 và
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
084

Hình thái học cho các khuẩn lạc vi khuẩn

Viết chương trình Python sử dụng tính năng đọc lướt để đếm số lượng khuẩn lạc trong mỗi hình ảnh và đối với mỗi hình ảnh, tạo ra một hình ảnh mới làm nổi bật các khuẩn lạc. The image should look similar to this one

Kiểu dữ liệu của hình ảnh trong python là gì?

Ngoài ra, in ra số lượng khuẩn lạc cho mỗi hình ảnh

Sử dụng những gì bạn đã học về biểu đồ, ngưỡng và phân tích thành phần được kết nối. Cố gắng đưa mã của bạn vào một chức năng có thể sử dụng lại để có thể dễ dàng áp dụng mã này cho bất kỳ tệp hình ảnh nào

Solution

Đầu tiên, chúng ta hãy thực hiện quy trình cho một hình ảnh

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
24

Kiểu dữ liệu của hình ảnh trong python là gì?

Tiếp theo, chúng ta cần ngưỡng hình ảnh để tạo mặt nạ chỉ bao phủ các khuẩn lạc tối màu. Điều này dễ dàng hơn khi sử dụng hình ảnh thang độ xám, vì vậy chúng tôi chuyển đổi nó ở đây

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
25

Kiểu dữ liệu của hình ảnh trong python là gì?

Tiếp theo, chúng tôi làm mờ hình ảnh và tạo biểu đồ

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
26

Kiểu dữ liệu của hình ảnh trong python là gì?

Trong biểu đồ này, chúng ta thấy ba đỉnh - đỉnh bên trái (i. e. điểm tối nhất) là khuẩn lạc của chúng tôi, đỉnh trung tâm là môi trường nuôi cấy màu vàng/nâu trong đĩa và bên phải (i. e. pixel sáng nhất) là nền hình ảnh màu trắng. Do đó ta chọn ngưỡng chọn đỉnh nhỏ bên trái

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
27

Kiểu dữ liệu của hình ảnh trong python là gì?

Mặt nạ này cho chúng ta biết vị trí của các khuẩn lạc trong hình ảnh - nhưng làm cách nào chúng ta có thể đếm được có bao nhiêu khuẩn lạc?

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
28

Cuối cùng, chúng tôi tạo hình ảnh tóm tắt của các khuẩn lạc màu trên hình ảnh thang độ xám

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
29

Kiểu dữ liệu của hình ảnh trong python là gì?

Bây giờ chúng tôi đã hoàn thành nhiệm vụ cho một hình ảnh, chúng tôi cần lặp lại điều này cho hai hình ảnh còn lại. Đây là một điểm tốt để thu thập các dòng trên thành một chức năng có thể sử dụng lại

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
30

Bây giờ chúng ta có thể dễ dàng thực hiện phân tích này trên tất cả các hình ảnh thông qua vòng lặp for

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
31

Kiểu dữ liệu của hình ảnh trong python là gì?
Kiểu dữ liệu của hình ảnh trong python là gì?
Kiểu dữ liệu của hình ảnh trong python là gì?

Bạn sẽ nhận thấy rằng đối với những hình ảnh có nhiều khuẩn lạc hơn, kết quả không hoàn hảo. Ví dụ, một số khuẩn lạc nhỏ bị thiếu và có thể có một số đốm đen nhỏ được dán nhãn không chính xác là khuẩn lạc. Ví dụ: bạn có thể mở rộng giải pháp này để sử dụng ngưỡng được xác định tự động cho từng hình ảnh, ngưỡng này có thể phù hợp hơn với từng hình ảnh. Ngoài ra, bạn có thể lọc ra các khuẩn lạc dưới một kích thước nhất định (như chúng ta đã làm trong phần Phân tích thành phần được kết nối). Bạn cũng sẽ thấy rằng một số thuộc địa cảm ứng được hợp nhất thành một thuộc địa lớn. Điều này có thể được khắc phục bằng các phương pháp phân đoạn phức tạp hơn (nằm ngoài phạm vi của bài học này) như lưu vực sông

Key Points

  • Sử dụng phân tích ngưỡng, phân tích thành phần được kết nối và các công cụ khác, chúng tôi có thể tự động phân đoạn hình ảnh của các khuẩn lạc vi khuẩn

    Kiểu dữ liệu nào là một hình ảnh?

    Hình ảnh là một loại dữ liệu tổng hợp . Tệp hình ảnh thường là tệp định dạng nhị phân.

    JPG là loại dữ liệu gì?

    JPEG (Hình ảnh của Nhóm chuyên gia nhiếp ảnh chung) . Thông số kỹ thuật JFIF (Định dạng trao đổi tệp JPEG) mô tả định dạng của các tệp mà chúng tôi coi là hình ảnh "JPEG". a data format for compressed photos, rather than a file type. The JFIF (JPEG File Interchange Format) specification describes the format of the files we think of as "JPEG" images.

    Hình ảnh trong Python là gì?

    Mô-đun Hình ảnh cung cấp một lớp có cùng tên được sử dụng để thể hiện hình ảnh PIL . Mô-đun này cũng cung cấp một số chức năng xuất xưởng, bao gồm chức năng tải hình ảnh từ tệp và tạo hình ảnh mới.

    Những loại hình ảnh nào được hỗ trợ trong Python?

    Định dạng lưu duy nhất được hỗ trợ là PNG , trừ khi PIL được cài đặt, trong trường hợp đó, bất kỳ định dạng nào nó hỗ trợ đều có thể được viết.