Làm cách nào để lấy các thuộc tính của hình ảnh trong python?

Nhận biết các vấn đề về hình thái học [những vấn đề liên quan đến số lượng, kích thước hoặc hình dạng của các đối tượng trong một hình ảnh]

Khi các hệ thống máy tính trở nên nhanh hơn và mạnh hơn, máy ảnh và các hệ thống hình ảnh khác đã trở nên phổ biến trong nhiều lĩnh vực khác của cuộc sống, nhu cầu ngày càng tăng đối với các nhà nghiên cứu để có thể xử lý và phân tích dữ liệu hình ảnh. Xem xét khối lượng lớn dữ liệu có thể liên quan - hình ảnh có độ phân giải cao chiếm nhiều dung lượng ổ đĩa/bộ nhớ ảo và/hoặc bộ sưu tập nhiều hình ảnh phải được xử lý cùng nhau - và tính chất tốn thời gian và dễ bị lỗi

Bài học này giới thiệu bộ công cụ mã nguồn mở để xử lý dữ liệu ảnh. ngôn ngữ lập trình Python và thư viện 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]. 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

Sử dụng xử lý hình ảnh trong nghiên cứu

Xử lý tự động có thể được sử dụng để phân tích nhiều thuộc tính khác nhau của hình ảnh, bao gồm sự phân bố và thay đổi màu sắc trong hình ảnh, số lượng, kích thước, vị trí, hướng và hình dạng của các đối tượng trong hình ảnh và thậm chí - khi kết hợp với máy học

Một số ví dụ về phương pháp xử lý ảnh được áp dụng trong nghiên cứu bao gồm

  • chụp ảnh một hố đen
  • ước tính dân số của chim cánh cụt hoàng đế
  • phân tích quy mô toàn cầu về đa dạng sinh vật phù du biển
  • phân đoạn gan và mạch máu từ hình ảnh CT

Với bài học này, chúng tôi mong muốn cung cấp nền tảng cơ bản về các khái niệm cơ bản và kỹ năng làm việc với dữ liệu hình ảnh trong Python. Hầu hết các ví dụ được sử dụng trong bài học này tập trung vào một loại kỹ thuật xử lý hình ảnh cụ thể, morphometrics, nhưng những gì bạn sẽ học có thể được sử dụng để giải quyết nhiều vấn đề khác nhau.

hình thái học

Hình thái học liên quan đến việc đếm số lượng đối tượng trong một hình ảnh, phân tích kích thước của đối tượng hoặc phân tích hình dạng của đối tượng. Ví dụ: chúng tôi có thể quan tâm đến việc tự động đếm số lượng khuẩn lạc vi khuẩn phát triển trong đĩa Petri, như trong hình này

Chúng tôi có thể sử dụng xử lý ả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

Tại sao phải viết một chương trình để làm điều đó?

Lưu ý rằng bạn có thể dễ dàng đếm thủ công số lượng khuẩn lạc được hiển thị trong ví dụ đo hình thái ở trên. Tại sao chúng ta nên học cách viết chương trình Python để thực hiện một tác vụ mà chúng ta có thể dễ dàng thực hiện bằng chính đôi mắt của mình?

  1. Điều gì sẽ xảy ra nếu có nhiều khuẩn lạc hơn trong đĩa Petri?

    Đếm thủ công các thuộc địa trong hình ảnh đó sẽ gây ra nhiều thách thức hơn. 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;

Như bạn có thể thấy, các kỹ thuật xử lý hình ảnh/thị giác máy tính đơn giản mà bạn sẽ học trong hội thảo này có thể là những công cụ rất có giá trị cho nghiên cứu khoa học

Khi chúng ta chuyển qua hội thảo này, chúng ta sẽ tìm hiểu các phương pháp phân tích hình ảnh hữu ích cho nhiều vấn đề khoa học khác nhau. Những điều này sẽ được liên kết với nhau và áp dụng cho một vấn đề thực tế trong thử thách capstone cuối cùng của hội thảo

Hãy bắt đầu bằng cách tìm hiểu một số điều cơ bản về cách hình ảnh được thể hiện và lưu trữ kỹ thuật số

Những điểm chính

  • 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

  • Các vấn đề về hình học liên quan đến số lượng, hình dạng và/hoặc kích thước của các đối tượng trong một hình ảnh

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

Tổng quan

Dạy học. 20 phút
Bài tập. 5 phút

câu hỏi

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

mục tiêu

  • Xác định các thuật ngữ bit, byte, kilobyte, megabyte, v.v.

  • Giải thích cách hình ảnh kỹ thuật số bao gồm các pixel

  • Đề nghị sử dụng imageio [resp. đọc lướt] cho I/O [tương ứng. nhiệm vụ xử lý ảnh]

  • Giải thích cách hình ảnh được lưu trữ trong mảng NumPy

  • Giải thích hệ tọa độ bên trái được sử dụng trong hình ảnh kỹ thuật số

  • Giải thích mô hình màu bổ sung RGB được sử dụng trong hình ảnh kỹ thuật số

  • Giải thích thứ tự của ba giá trị màu trong hình ảnh lướt qua

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

  • Giải thích sự khác biệt giữa nén mất dữ liệu và nén không mất dữ liệu

  • Giải thích những ưu điểm và nhược điểm của các định dạng hình ảnh nén

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

Hình ảnh chúng ta nhìn thấy trên bản in, xem bằng thiết bị điện tử hoặc xử lý bằng chương trình của chúng ta được biểu diễn và lưu trữ trong máy tính dưới dạng trừu tượng số, xấp xỉ những gì chúng ta nhìn thấy bằng mắt trong thế giới thực. 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

Điểm ảnh

Đ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 đỏ

Bây giờ, nếu chúng ta phóng to đủ gần để xem các pixel trong hộp màu đỏ, chúng ta sẽ thấy một cái gì đó như thế này

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

Nhập câu lệnh trong 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

Chức năng bổ sung có thể được tải dưới dạng một chức năng hoặc đối tượng, một mô-đun xác định một số trong số này hoặc một thư viện chứa nhiều mô-đun. 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

Trong ví dụ trên, dạng 1 nạp toàn bộ 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 vào chương trình dưới dạng đối tượng. Các mô-đun riêng lẻ của thư viện sau đó có sẵn trong đối tượng đó, e. g. , để truy cập hàm ________ 037 được sử dụng trong tập vẽ, bạn sẽ viết ________ 038

Biểu mẫu 2 chỉ tải 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
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 vào chương trình. Khi chúng tôi chạy mã, chương trình sẽ mất ít thời gian hơn và sử dụng ít bộ nhớ hơn vì chúng tôi sẽ không tải toàn bộ 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ú pháp cần thiết để sử dụng mô-đun không thay đổi. để 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

Để tiếp tục giảm yêu cầu về thời gian và bộ nhớ cho chương trình của bạn, biểu mẫu 3 có thể được sử dụng để chỉ nhập một chức năng/lớp cụ thể từ thư viện/mô-đun. Không giống như các dạng khác, khi cách tiếp cận này được sử dụng, hàm hoặc lớp đã nhập chỉ có thể được gọi theo tên của nó mà không cần thêm tiền tố vào tên của mô-đun/thư viện mà từ đó nó được tả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. , ví dụ trên sẽ thay thế bất kỳ đối tượng hiện có nào được gọi 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
37 bằ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
37 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
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. Tên này được gọi là bí danh. Thông thường, sử dụng bí danh [chẳng hạn 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
49 cho thư viện NumPy] giúp chúng tôi tiết kiệm một chút thời gian nhập. Bạn có thể 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
48 kết hợp với bất kỳ dạng nào trong ba dạng đầu tiên khác của tuyên bố
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Hình thức nào được sử dụng thường xuyên phụ thuộc vào kích thước và số lượng công cụ bổ sung được tải vào chương trình

Bây giờ chúng tôi đã tải các thư viện của mình, chúng tôi sẽ chạy Lệnh ma thuật Jupyter để đảm bảo hình ảnh của chúng tôi hiển thị trong tài liệu Jupyter của chúng tôi với thông tin pixel sẽ giúp chúng tôi chạy các lệnh hiệu quả hơn sau này trong phiê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
2

Với điều đó đã được giải quyết, hãy tải dữ liệu hình ảnh của chúng ta từ đĩa bằng cách 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
52 từ 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
53 và hiển thị nó bằng cách 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
54 từ 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
55.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 là một thư viện Python để đọc và ghi dữ liệu 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
53 đang xác định rằng chúng tôi muốn sử dụng phiên bản 3 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. 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. Trên thự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
59 sử dụng nội bộ
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 khi tải hình ảnh vào Python. Đó chắc chắn là thứ bạn có thể sử dụng khi bạn thấy phù hợp với mã của riêng mình. In this lesson, 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
56 library to read or write [save] images, while
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 is dedicated to performing operations on the images. 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
56 giúp chúng tôi linh hoạt hơn, đặc biệt là khi xử lý 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
7

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”. Màn hình của tám pixel mà bạn thấy sử dụng nhiều pixel màn hình hơn để hiển thị tám pixel lớn như vậy của chúng tôi, nhưng điều đó không có nghĩa là có thông tin cho tất cả các pixel màn hình đó trong tệp. Tất cả những pixel thừa đó là hệ quả của việc người xem của chúng tôi tạo thêm pixel thông qua phép nội suy. Nó có thể chỉ hiển thị nó dưới dạng một hình ảnh nhỏ chỉ sử dụng 15 pixel màn hình nếu trình xem được thiết kế khác

Mặc dù nhiều định dạng tệp hình ảnh chứa siêu dữ liệu mô tả có thể cần thiết, nhưng phần lớn tệp ảnh chỉ là các mảng thông tin số mà khi được giải thích theo một bộ quy tắc nhất định, chúng ta có thể nhận ra đó là một hình ảnh. Hình ảnh số tám của chúng tôi cũng không ngoại lệ 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
53 đã lưu trữ dữ liệu hình ảnh đó trong một mảng các mảng tạo thành ma trận 5 x 3 gồm 15 pixel. Chúng tôi có thể chứng minh điều đó bằng cách gọi thuộc tính hình dạng của biến hình ảnh của chúng tôi và xem ma trận bằng cách in biến hình ảnh của chúng tôi ra màn hì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
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

Thus if we have tools that will allow us to manipulate these arrays of numbers, we can manipulate the image. 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. Lưu ý rằng hành vi mặc định của 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
54 đã thêm số hàng và cột sẽ hữu ích cho chúng tôi khi chúng tôi cố gắng giải quyết các pixel riêng lẻ hoặc nhóm. Trước tiên, hãy tải một bản sao khác trong số tám của chúng tôi, sau đó làm cho nó trông giống như một số không

Để làm cho nó giống số 0, chúng ta cần thay đổi số bên dưới pixel ở giữa thành 1. Với sự trợ giúp của các tiêu đề hàng và cột đó, ở quy mô nhỏ này, chúng tôi có thể xác định pixel trung tâm ở hàng có nhãn 2 và cột có nhãn 1. Sử dụng kỹ thuật cắt mảng, sau đó chúng ta có thể xử lý và gán giá trị mới cho vị 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
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

Coordinate system

Khi chúng tôi xử lý hình ảnh, chúng tôi có thể truy cập, kiểm tra và/hoặc thay đổi màu của bất kỳ pixel nào mà chúng tôi muốn. 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

Cách phổ biến nhất để làm điều này và cách chúng ta sẽ sử dụng trong các chương trình của mình là gán một hệ tọa độ Descartes đã sửa đổi cho hình ảnh. Hệ tọa độ mà chúng ta thường thấy trong toán học có trục x nằm ngang và trục y thẳng đứng, như thế này

Hệ tọa độ đã sửa đổi được sử dụng cho hình ảnh của chúng ta sẽ chỉ có tọa độ dương, gốc tọa độ sẽ ở góc trên bên trái thay vì ở giữa và các giá trị tọa độ y sẽ lớn hơn khi chúng đi xuống thay vì đi lên, như thế này

Đâ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

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. Điều này có thể đặc biệt hữu ích trong trường hợp bạn cần chuyển đổi dữ liệu trình xem hình ảnh được cung cấp ở định dạng x,y sang định dạng y,x. Thus, we will use cx and ry where appropriate to help bridge these two approaches

Thay đổi giá trị pixel [5 phút]

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. Hiển thị hình ảnh và in ra ma trận

Dung dịch

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

Nhiều màu sắc hơn

Cho đến nay, chúng tôi chỉ có một ma trận 2 màu, nhưng chúng tôi có thể có nhiều hơn nếu chúng tôi sử dụng các số hoặc phân số khác. Một cách phổ biến là sử dụng các số từ 0 đến 255 để cho phép 256 màu khác nhau hoặc 256 cấp độ xám khác nhau. Let’s try that out

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Chúng tôi hiện có 3 màu, nhưng đó có phải là ba màu mà bạn mong đợi không? . This is a consequence of the default colour map [cmap] in this library. Bạn có thể coi bản đồ màu là sự liên kết hoặc ánh xạ các số thành một màu cụ thể. 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. Trong trường hợp cụ thể của chúng tôi ở đây chẳng hạn, 255 hoặc cường độ cao nhất được ánh xạ thành màu vàng và 0 hoặc cường độ thấp nhất được ánh xạ thành màu tím đậm. The best colour map for your data will vary and there are many options built in, but this default selection was not arbitrary. Rất nhiều nhà khoa học đã biến điều này thành mặc định do tính mạnh mẽ của nó khi nói đến cách tâm trí con người diễn giải các giá trị màu tương đối, khả năng in ở thang màu xám và tính thân thiện với người mù màu [Bạn có thể đọc thêm về bản đồ màu mặc định này trong Matplotlib . Vì vậy, đây là một nơi tốt để bắt đầu và bạn chỉ nên thay đổi nó khi có mục đích và suy nghĩ trước. 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

Ở 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. Không bản đồ thành màu đen, 255 bản đồ thành màu trắng và 128 bản đồ thành màu xám trung bình. Ở đâ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

Thậm chí nhiều màu sắc hơn

This is all well and good at this scale, but what happens when we instead have a picture of a natural landscape that contains millions of colours. 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. Lưu trữ các số trong ma trận nhiều chiều trong đó mỗi màu hoặc thuộc tính như độ trong suốt được liên kết với kích thước riêng của nó cho phép các đóng góp riêng lẻ cho một pixel được điều chỉnh độc lập. Khả năng thao tác các thuộc tính của các nhóm pixel một cách riêng biệt sẽ là chìa khóa cho một số kỹ thuật được khám phá trong các chương sau của bài học này. Để bắt đầu, hãy xem một ví dụ về cách các kích thước thông tin khác nhau kết hợp để tạo ra một tập hợp pixel bằng cách sử dụng ma trận 4 X 4 với 3 kích thước cho các màu đỏ, lục và lam. Rather than loading it from a file, we will generate this example using 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

Trước đây, chúng tôi có một số được ánh xạ tới một màu hoặc cường độ. Now we are combining the effect of 3 numbers to arrive at a single colour value. 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

This outputs. mảng[[ 7, 1, 110]] Các số nguyên theo thứ tự đại diện cho Đỏ, Lục và Lam. 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 percent of its potential. 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. It may be helpful to display each of these channels independently, to help us understand what is happening. 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

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

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

This last colour model we used, known as the RGB [Red, Green, Blue] model, is the most common

Như chúng ta đã thấy, mô hình RGB là mô hình màu bổ sung, có nghĩa là các màu cơ bản được trộn với nhau để tạo thành các màu khác. 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. Số lượng riêng biệt của mỗi màu, 256, tương ứng với số bit được sử dụng để giữ giá trị kênh màu, là tám [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. A larger number in a channel means that more of that primary colour is present

Suy nghĩ về màu RGB [5 phút]

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]

Dung dịch

  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]

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. Có bao nhiêu màu có thể được biểu thị bằng mô hình RGB 24 bit?

Dung dịch

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

Although 24-bit colour depth is common, there are other options. 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 . There are colour depths with more than eight bits per channel, but as the human eye can only discern approximately 10 million different colours, these are not often used

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. 24-bit colour images will be converted in some manner to 18-bit, and thus the colour quality you see will not match what is actually in the image

We can combine our coordinate system with the 24-bit RGB colour model to gain a conceptual understanding of the images we will be working with. An image is a rectangular array of pixels, each with its own coordinate. 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

Although the images we will manipulate in our programs are conceptualised as rectangular arrays of RGB triplets, they are not necessarily created, stored, or transmitted in that format. Có một số định dạng hình ảnh mà chúng ta có thể gặp phải và chúng ta nên biết những điều cơ bản của ít nhất một vài trong số chúng. 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

FormatExtensionDevice-Independent Bitmap [BMP]. bmpJoint Photographic Experts Group [JPEG]. jpg or . jpeg Định dạng tệp hình ảnh được gắn thẻ [TIFF]. tif hoặc. tiff

BMP

Định dạng tệp gần nhất với khái niệm hình ảnh trước đây của chúng tôi là Bitmap độc lập với thiết bị, hoặc BMP, định dạng tệp. Các tệp BMP lưu trữ hình ảnh đồ họa raster dưới dạng chuỗi dài các số được mã hóa nhị phân chỉ định màu của từng pixel trong ảnh. Since computer files are one-dimensional structures, the pixel colours are stored one row at a time. That is, the first row of pixels [those with y-coordinate 0] are stored first, followed by the second row [those with y-coordinate 1], and so on. 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

Hình ảnh BMP 24 bit có định dạng tệp tương đối đơn giản, có thể được xem và tải trên nhiều hệ điều hành và có chất lượng cao. However, BMP images are not compressed, resulting in very large file sizes for any useful image resolutions

Ý tưởng nén hình ảnh rất quan trọng đối với chúng tôi vì hai lý do. first, compressed images have smaller file sizes, and are therefore easier to store and transmit; and second, compressed images may not have as much detail as their uncompressed counterparts, and so our programs may not be able to detect some important aspect if we are working with compressed images. 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

Image compression

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. For example, a number like 659 is 6 × 102 + 5 × 101 + 9 × 100. Each digit in the number is multiplied by a power of 10, based on where it occurs, and there are 10 digits that can occur in each position [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. [Mạch điện dễ dàng phân biệt sự khác biệt giữa hai mức điện áp hơn là phân biệt giữa 10 mức. ] 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

In this system, each symbol in a number is called a bit instead of a digit, and there are only two values for each bit [0 and 1]. Chúng ta có thể tưởng tượng một số nhị phân bốn bit, 1101. Using the same kind of place-value expansion as we did above for 659, we see that 1101 = 1 × 23 + 1 × 22 + 0 × 21 + 1 × 20, which if we do the math is 8 + 4 + 0 + 1, or 13 in decimal

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. The amount of memory [RAM] and drive space our computers have is quantified by terms like Megabytes [MB], Gigabytes [GB], and Terabytes [TB]. The following table provides more formal definitions for these terms

UnitAbbreviationSizeKilobyteKB1024 bytesMegabyteMB1024 KBGigabyteGB1024 MBTerabyteTB1024 GB

BMP image size [optional, not included in timing]

Imagine that we have a fairly large, but very boring image. a 5,000 × 5,000 pixel image composed of nothing but white pixels. 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?

Dung dịch

Trong một hình ảnh như vậy, có 5.000 × 5.000 = 25.000.000 pixel và 24 bit cho mỗi pixel, dẫn đến 25.000.000 × 24 = 600.000.000 bit hoặc 75.000.000 byte [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

Lossless compression

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. , a computerised procedure] to the image, resulting in a file that is significantly smaller than the uncompressed BMP file equivalent would be. Sau đó, khi chúng tôi muốn tải và xem hoặc xử lý hình ảnh, chương trình của chúng tôi sẽ đọc tệp nén và đảo ngược quá trình nén, dẫn đến hình ảnh giống hệt với bản gốc. Nothing is lost in the process – hence the term “lossless. ”

Ý tưởng chung của nén không mất dữ liệu là bằng cách nào đó phát hiện các mẫu byte dài trong một tệp được lặp đi lặp lại, sau đó gán một mẫu bit nhỏ hơn để biểu thị mẫu dài hơn. 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. The compressed file also contains a table of the substituted patterns and the originals, so when the file is decompressed it can be made identical to the original before compression

Để cung cấp cho bạn một ví dụ cụ thể, hãy xem xét 71. 5 MB white BMP image discussed above. When put through the zip compression utility on Microsoft Windows, the resulting . zip file is only 72 KB in size. That is, the . phiên bản zip của hình ảnh nhỏ hơn ba bậc so với bản gốc và nó có thể được giải nén thành một tệp có kích thước từng byte giống như bản gốc. 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. nén hoặc. gz, bạn đang xử lý nén không mất dữ liệu

Nén mất dữ liệu

Nén mất dữ liệu lấy hình ảnh gốc và loại bỏ một số chi tiết trong đó, dẫn đến định dạng tệp nhỏ hơn. The goal is to only throw away detail that someone viewing the image would not notice. Many lossy compression schemes have adjustable levels of compression, so that the image creator can choose the amount of detail that is lost. Càng nhiều chi tiết bị hy sinh, các tệp hình ảnh sẽ càng nhỏ - nhưng tất nhiên, độ chi tiết và độ phong phú của hình ảnh cũng sẽ thấp hơn

Đ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

Examining actual image sizes [optional, not included in timing]

Hãy cùng chúng tôi xem ảnh hưởng của nén ảnh đến dung lượng ảnh với ảnh thực tế. Tập lệnh sau tạo một hình ảnh vuông màu trắng 5000 X 5000 pixel, sau đó lưu nó dưới dạng BMP và dưới dạng ảnh 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
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?

Dung dịch

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. Hai hình ảnh đầ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, vẫn phải nằm trong thư mục, cùng với một hình ảnh 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
77

Chúng tôi có thể áp dụng nén không mất dữ liệu cho bất kỳ tệp nào bằng cách sử 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
78. 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. Áp dụng nén không mất dữ liệu cho hình ảnh này bằng cách thực hiện lệ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
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. Thực hiện một lệnh tương tự trên tệp JPEG câ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
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?

Dung dịch

Đây là danh sách một phần thư mục, hiển thị kích thước của các tệp có liên quan ở đó

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Chúng ta có thể thấy rằng tính đều đặn của hình ảnh bitmap [hãy nhớ rằng đó là hình ảnh 5.000 x 5.000 pixel chỉ chứa các pixel trắng] cho phép lược đồ nén không mất dữ liệu nén tệp khá hiệu quả. 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. Xem xét hình ảnh này của một số cây ngô [được thu nhỏ ở đây từ 11.339 × 11.336 pixel để vừa với màn hình]

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

Đây là cùng một khu vực của hình ảnh, nhưng ở định dạng JPEG. Chúng tôi đã sử dụng một tham số nén khá tích cực để tạo JPEG, nhằm minh họa các sự cố mà bạn có thể gặp phải với định dạng

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

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ơ đồ. Nó sử dụng nén không mất dữ liệu và do đó thường được sử dụng trong các ứng dụng web cho hình ảnh không phải ảnh. Đị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. Hình ảnh TIFF có thể được giải nén hoặc nén bằng cách sử dụng sơ đồ nén không mất dữ liệu hoặc không mất dữ liệu, tùy thuộc vào cài đặt được sử dụng và do đó, hình ảnh TIFF dường như có lợi ích của cả định dạng BMP và JPEG. 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. Siêu dữ liệu là thông tin dạng văn bản được chứa trong tệp hình ảnh. Siêu dữ liệu chứa thông tin về chính hình ảnh, chẳng hạn như thời điểm chụp ảnh, nơi chụp ảnh, loại máy ảnh được sử dụng và cài đặt nào, v.v. Chúng tôi thường không nhìn thấy siêu dữ liệu này khi xem một hình ảnh, nhưng chúng tôi có thể xem nó một cách độc lập nếu muốn [xem phần Truy cập siêu dữ liệu bên dưới]. Đ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

Bảng sau đây tóm tắt các đặc điểm của định dạng ảnh BMP, JPEG và TIFF

Đị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 kém linh hoạt hơn TIFF, chỉ RGB

Những điểm chính

  • 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

  • Nén không mất dữ liệu giữ lại tất cả các chi tiết trong ảnh, nhưng nén có mất dữ liệu dẫn đến mất một số chi tiết của ảnh gốc

  • 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

  • Tùy thuộc vào máy ảnh hoặc cảm biến, các mẩu thông tin hữu ích khác nhau có thể được lưu trữ trong tệp hình ảnh, trong siêu dữ liệu hình ảnh

Làm việc với Skipage

Tổng quan

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

câu hỏi

  • Làm thế nào thư viện thị giác máy tính Skimage Python có thể được sử dụng để làm việc với hình ảnh?

mục tiêu

  • Đọc và lưu hình ảnh với imageio

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

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

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

  • Trích xuất các hình ảnh phụ bằng cách sử dụng mảng cắt

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. Đây là vài dòng đầu tiê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
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

Khi chúng tôi có hình ảnh trong chương trình, trước tiên 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
94 để chúng tôi sẽ có một hình mới với một bộ trục độc lập với các cuộc gọi trước đó của chúng tôi. 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

Bây giờ, chúng tôi sẽ lưu hình ảnh ở định dạng khá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
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. 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
99 tự động xác định loại tệp, dựa trên phần mở rộng tệp mà chúng tôi cung cấp. 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

Hãy nhớ rằng, như đã đề cập trong phần trước, hình ảnh được lư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
99 sẽ không giữ lại tất cả siêu dữ liệu được liên kết với hình ảnh gốc đã được tải vào 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. , tên tệp, địa chỉ http] và hình ảnh để ghi vào đĩa. 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

Vì hàm dự kiến ​​đối số đầu tiên là tên tệp, nên không có sự nhầm lẫn về ý nghĩa của

"""
 * 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. Điều tương tự cũng xảy ra với đối số thứ hai

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

Phong cách này sẽ giúp bạn dễ dàng học cách sử dụng nhiều chức năng mà chúng tôi sẽ giới thiệu trong hội thảo này

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

Tiếp theo, ghi hình ảnh đã thay đổi kích thước vào một tệp mới có tê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
220 trong thư mục dữ liệu của bạn. 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ệ của từng ả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

Dung dịch

Đây là giao diện của tập lệnh Python của bạ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
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

Trong tập Thông tin cơ bản về hình ảnh, chúng tôi đã thao tác riêng màu sắc của pixel bằng cách thay đổi các số được lưu trữ trong mảng NumPy của hình ảnh. Hãy áp dụng các nguyên tắc đã học ở đó cùng với một số nguyên tắc mới vào ví dụ thực tế

Giả sử chúng ta quan tâm đến hình ảnh cụm rễ ngô này. Chúng tôi muốn có thể tập trung sự chú ý của chương trình vào gốc rễ, trong khi bỏ qua nền đen

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. Quá trình này được gọi là ngưỡng và chúng ta sẽ thấy các phương pháp mạnh mẽ hơn để thực hiện tác vụ ngưỡng trong phần Ngưỡng. 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. Điều này sẽ giữ cho các pixel sáng hơn một nửa "độ sáng đầy đủ", tôi. e. , pixel không thuộc nền đen. 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

Bây giờ chúng ta có thể ngưỡng hình ảnh và hiển thị kết quả

"""
 * 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ỏ

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. Chức năng này thêm ba kênh màu theo cách phù hợp với nhận thức về màu sắc của con người, xem tài liệu về lướt để biết chi tiết. 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 thường thích đánh vần tiếng Anh của Vương quốc Anh hơn, đó là lý do tại sao chúng tôi sử dụng “color” trong phần giải thích của bài học này. Tuy nhiê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 chứa nhiều mô-đun và chức năng bao gồm cách đánh vần tiếng Anh 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
229. Vấn đề chính tả chính xác ở đây, e. g. bạn sẽ gặp lỗi nếu cố chạ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
232. Để giải thích điều này, chúng ta sẽ sử dụng cách đánh vần tiếng Anh 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
229, ví dụ như mã Python trong suốt bài học. Bạn sẽ gặp cách tiếp cận tương tự với “trung tâm” 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
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

Chúng tôi cũng có thể tải trực tiếp hình ảnh màu dưới dạng thang độ xám bằng cách chuyển đối số

"""
 * 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 đế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
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

Chỉ giữ lại các pixel cường độ thấp [10 phút]

Trước đó một chút, chúng tôi đã chỉ ra cách chúng tôi có thể sử dụng Python và Skipage để chỉ bật các pixel có cường độ cao từ một hình ảnh, đồng thời tắt tất cả các pixel có cường độ thấp. Bây giờ, bạn có thể thực hành làm ngược lại - giữ tất cả các điểm ảnh có cường độ thấp trong khi thay đổi các điểm ảnh có cường độ cao

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
237 là hình ảnh RGB của câu đố sudoku

Nhiệm vụ của bạn là biến tất cả các pixel trắng trong ảnh thành màu xám nhạt, chẳng hạn với cường độ của từng pixel trắng trước đây được đặt thành 0. 75. Kết quả sẽ giống như thế này

Gợi ý. đây là một trường hợp hữu ích khi chuyển đổi hình ảnh từ RGB sang thang độ xám

Dung dịch

Đầ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]

So với ảnh màu, ảnh thang độ xám chỉ chứa một giá trị cường độ duy nhất trên mỗi 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. The default colour map is called “viridis” and maps low values to purple and high values to yellow. 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. The documentation contains an overview of pre-defined colour maps

Furthermore, matplotlib determines the minimum and maximum values of the colour map dynamically from the image, by default. That means, that in an image, where the minimum is 0. 25 and the maximum is 0. 75, those values will be mapped to black and white respectively [and not dark gray and light gray as you might expect]. If there are defined minimum and maximum vales, you can specify them via

"""
 * 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 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
242 to get the desired output

If you forget about this, it can lead to unexpected results. 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

Access via slicing

As noted in the previous lesson skimage images are stored as NumPy arrays, so we can use array slicing to select rectangular areas of an image. Then, we can save the selection as a new image, change the pixels in the image, and so on. It is important to remember that coordinates are specified in [ry, cx] order and that colour values are specified in [r, g, b] order when doing these manipulations

Consider this image of a whiteboard, and suppose that we want to create a sub-image with just the portion that says “odd + even = odd,” along with the red box that is drawn around the words

Using the same display technique we have used throughout this course, we can determine the coordinates of the corners of the area we wish to extract by hovering the mouse near the points of interest and noting the coordinates. If we do that, we might settle on a rectangular area with an upper-left coordinate of [135, 60] and a lower-right coordinate of [480, 150], as shown in this version of the whiteboard picture

Note that the coordinates in the preceding image are specified in [cx, ry] order. Now if our entire whiteboard image is stored as an skimage 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
93, we can create a new image of the selected region with a statement like this

"""
 * 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. Note we go one beyond the maximum value in each dimension, so that the entire desired area is selected. 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

Then we use array slicing to create a new image with our selected area and then display the new 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
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 ở [ . Then, 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
250 command, we modify the image in the specified area. From a NumPy perspective, this changes all the pixel values within that range to array saved in 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. 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

Practicing with slices [10 min - optional, not included in timing]

Using the techniques you just learned, write a script that creates, displays, and saves a sub-image containing only the plant and its roots from “data/maize-root-cluster. jpg”

Dung dịch

Here is the completed Python program to select only the plant and roots in 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
77

Những điểm chính

  • 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

  • We create a window that automatically scales the displayed image with matplotlib and calling

    """
     * 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 on the global figure object

  • 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

  • NumPy array commands, 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
    
    226, can be used to manipulate the pixels of an image

  • Array slicing can be used to extract sub-images or modify areas of images, e. 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

  • Metadata is not retained when images are loaded as skimage images

Drawing and Bitwise Operations

Tổng quan

Teaching. 45 min
Exercises. 45 phút

câu hỏi

  • 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?

mục tiêu

  • 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

Loạt tập tiếp theo đề cập đến bộ công cụ cơ bản của các toán tử skidage. 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

Vẽ trên hình ảnh

Thường thì chúng tôi chỉ muốn chọn một phần của hình ảnh để phân tích và bỏ qua phần còn lại. 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ạ. Khi chuẩn bị mặt nạ, đôi khi chúng ta cần có thể vẽ một hình - chẳng hạn như hình tròn hoặc hình chữ nhật - trên một hình ảnh màu đen. 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ô

Bây giờ, giả sử chúng ta chỉ muốn phân tích vùng ảnh chứa rễ cây; . Hơn nữa, chúng tôi cũng muốn loại trừ khung của thùng chứa cây con. 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. Lập chỉ mục với một mảng boolean còn được gọi là lập chỉ mục mặt nạ. Các "pixel" trong một mảng mặt nạ như vậy chỉ có thể nhận hai 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 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. Khi lập chỉ mục một hình ảnh có mặt nạ như vậy, chỉ các giá trị pixel tại các vị trí có mặ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
265 mới được truy cập. 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. Lưu ý rằng chúng tôi chỉ sử dụng hai chỉ số đầu tiên của hình dạng của chúng tôi. Chúng tôi đã bỏ qua thứ nguyên kênh. 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. , các giá trị 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
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. Do đó, mặc dù chúng tôi 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
272 để tạo mặt nạ, nhưng các giá trị pixel của nó trên thực tế không phải 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
273 mà 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
265. Bạn có thể kiểm tra điều này, e. g. , bở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
275

Tiếp theo, chúng tôi vẽ một hình chữ nhật đầy trên mặ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
90

Đây là những gì mặt nạ xây dựng của chúng tôi trông giống như.

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
276
"""
 * 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 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
278, là tọa độ của góc trên bên trá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
279] và góc dưới bên phả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
280] của hình chữ nhật theo thứ tự [ry, cx]. Hàm trả về hình chữ nhật dưới dạng mảng tọa độ hà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
281] và cộ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
282]

Check the documentation

Khi sử dụng chức năng đọc lướt lần đầu tiên - hoặc lần thứ năm - bạn nên kiểm tra xem chức năng này được sử dụng như thế nào, thông qua tài liệu đọc lướt hoặc các ví dụ sử dụng khác trên các trang web liên quan đến lập trình như Stack Overflow. Thông tin cơ bản về các chức năng đọc lướt có thể được tìm thấy một cách tương tác trong Python, thông qua các lệ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
283 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
284. Ghi chú vào sổ tay phòng thí nghiệm của bạn. Và, bạn nên chạy một số mã kiểm tra để xác minh rằng các chức năng mà chương trình của bạn sử dụng đang hoạt động theo cách bạn dự định.

Quy ước đặt tên biến

Bạn có thể thắc mắc tại sao chúng tôi gọi các giá trị trả về của hàm hình chữ nhật 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
281 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
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. Trên thực tế, có thể rõ ràng hơn khi đặt tên cho các biế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
295 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
296; . 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

Các thao tác vẽ khác [15 phút]

Có các chức năng khác để vẽ trên hình ảnh, ngoài 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
297. 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

Dung dịch

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. Để làm điều này, chúng ta có thể sử dụng mô-đun python

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 và 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
702, có thể tạo ra các số ngẫu nhiên trong một phạm vi nhất định

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?

Dung dịch

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

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

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

Dung dịch

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

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

Dung dị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
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

Dung dịch

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

Những điểm chính

  • Chúng ta có thể sử dụng chức nă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
    
    714 để tạo một hình ảnh đen, trống

  • 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

câu hỏi

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

mục tiêu

  • 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.

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

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. Đối với thùng cuối cùng, mảng cũng phải chứa điểm dừng, vì vậy nó có thêm một phần tử ngoài biểu đồ

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

We create the plot 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
735, then label the figure and the coordinate axes 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
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, 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
738 functions. 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

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

Dung dị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
07

Your histogram of the masked area should look something like this

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

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
757 tích hợp trong Python lấy một danh sách và trả về một bộ lặp gồm các bộ, trong đó phần tử đầu tiên của bộ là chỉ mục và phần tử thứ hai là phần tử của danh sách

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

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

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

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

Dung dị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
43

Những điểm chính

  • 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 phút
Bài tập. 25 phút

câu hỏi

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

mục tiêu

  • 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. Các chi tiết liên quan đến tần số không gian cao là nhỏ, rất nhiều tính năng này sẽ phù hợp với một hình ảnh. Các tính năng liên quan đến tần số không gian thấp rất lớn - có thể có một số tính năng lớn trên mỗi hình ảnh

làm mờ

Blurring is to make something less clear or distinct. Điều này có thể được diễn giải khá rộng trong bối cảnh phân tích hình ảnh - bất cứ điều gì làm giảm hoặc bóp méo chi tiết của hình ảnh đều có thể áp dụng. 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

nhân

Một hạt nhân có thể được sử dụng để triển khai bộ lọc trên hình ảnh. Trong bối cảnh này, hạt nhân là một ma trận nhỏ được kết hợp với hình ảnh bằng kỹ thuật toán học. tích chập. Different sizes, shapes and contents of kernel produce different effects. Bản thân hạt nhân có thể được coi là một hình ảnh nhỏ và sẽ ưu tiên các tính năng có kích thước và hình dạng tương tự trong hình ảnh chính. Khi tích chập với một hình ảnh, một hạt nhân lớn, có đốm sẽ giữ lại các tính năng tần số không gian lớn, có đốm, thấp

Gaussian mờ

Hãy xem xét hình ảnh một con mèo này, đặc biệt là khu vực của hình ảnh được viền bởi hình vuông màu trắng

Now, zoom in on the area of the cat’s eye, as shown in the left-hand image below. Khi chúng tôi áp dụng bộ lọc, chúng tôi xem xét từng pixel trong ảnh, từng pixel một. Trong ví dụ này, pixel chúng tôi hiện đang làm việc được đánh dấu bằng màu đỏ, như thể hiện trong hình ảnh bên tay phải

Khi chúng tôi áp dụng bộ lọc, chúng tôi lần lượt xem xét các nhóm pixel hình chữ nhật bao quanh từng pixel trong ảnh. 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. Chiều rộng và chiều cao của kernel phải là số lẻ để pixel đang được xử lý luôn nằm ở trung tâm của nó. Trong ví dụ hiển thị ở trên, hạt nhân là hình vuông, có kích thước là 7 pixel

Để áp dụng kernel cho pixel hiện tại, giá trị trung bình của các giá trị màu của các pixel xung quanh pixel được tính toán, được tính theo trọng số của các giá trị trong kernel. Trong hiệu ứng làm mờ Gaussian, các pixel gần trung tâm nhất của hạt nhân có trọng lượng lớn hơn so với các pixel ở xa trung tâm. Tốc độ giảm trọng lượng này được xác định bởi hàm Gaussian, do đó có tên Gaussian blur

Hàm Gaussian ánh xạ các biến ngẫu nhiên thành phân phối chuẩn hoặc “Đường cong hình chuông”.

https. // vi. wikipedia. org/wiki/Gaussian_function#/media/File. Bình thường_Distribution_PDF. svg

Hình dạng của hàm được mô tả bằng giá trị trung bình μ và giá trị phương sai σ². Giá trị trung bình xác định điểm trung tâm của đường cong hình chuông trên trục x và phương sai mô tả độ mở rộng của đường cong

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

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

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

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

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

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

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. Do đó, chúng ta phải chọn một khoảng cách nhất định từ trung tâm của hàm nơi chúng ta dừng phép tính gần đúng này và đặt kích thước cuối cùng của hạt nhân của chúng ta. 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

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?

Dung dịch

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

Dung dị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
50

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

Những điểm chính

  • 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

câu hỏi

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

mục tiêu

  • 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

  • Giải thích khi nào phương pháp tạo ngưỡng tự động của Otsu là phù hợp

  • 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. In thresholding, we convert an image from colour or grayscale into a binary image, 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. Then, we will use the masks returned by these functions to select the parts of an image we are interested in

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

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. Specifically, in this simple, fixed-level thresholding, we have to provide a threshold 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
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

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

Since the image has a white background, most of the pixels in the image are white. 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”. Let us choose
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

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, we can use the numpy comparison operators to create a mask. 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. The operator returns a mask, that we capture in the variable
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

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. On the other hand, if we choose too low a value for the threshold, we could lose some of the shapes that are too bright. 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. Below we will introduce automatic thresholding, which uses a quantitative, mathematical definition for a good threshold that allows us to determine the 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 automatically. 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. What we are left with is only the coloured shapes from the original

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

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

First, plot the grayscale histogram as in the Creating Histogram episode and examine the distribution of grayscale values in the image. 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?

Dung dịch

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

We can see a large spike around 0. 3, and a smaller spike around 0. 7. The spike near 0. 3 represents the darker background, so it seems like a value close 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
927 would be a good choice

Next, create a mask to turn the pixels above 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 on and pixels below 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 off. Note that unlike the image with a white background we used above, here the peak for the background colour is at a lower gray level than the shapes. Therefore, change the comparison operator 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 to greater
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 to create the appropriate mask. Then apply the mask to the image and view the thresholded image. If everything works as it should, your output should show only the coloured shapes on a black background

Dung dịch

Here are the commands to create and view the binary 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
57

And here are the commands to apply the mask and view the thresholded 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
58

Automatic thresholding

The downside of the simple thresholding technique is that we have to make an educated guess about 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 by inspecting the histogram. There are also automatic thresholding methods that can determine the threshold automatically for us. One such method is Otsu’s method. It is particularly useful for situations where the grayscale histogram of an image has two peaks that correspond to background and objects of interest

Denoising an image before thresholding

In practice, it is often necessary to denoise the image before thresholding, which can be done with one of the methods from the Blurring Images episode

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
933 of a maize root system which we have seen before in the Image Representation in skimage 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
59

We use Gaussian blur with a sigma of 1. 0 to denoise the root image. Let us look at the grayscale histogram of the denoised 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
60

The histogram has a significant peak around 0. 2, và một đỉnh thứ hai, nhỏ hơn rất gần 1. 0. Thus, this image is a good candidate for thresholding with Otsu’s method. The mathematical details of how this works are complicated [see the skimage documentation if you are interested], but the outcome is that Otsu’s method finds a threshold value between the two peaks of a grayscale histogram

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
934 function can be used to determine the threshold automatically via Otsu’s method. Then numpy comparison operators can be used to apply it as before. Here are the Python commands to determine 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 with Otsu’s method

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

For this root image and a Gaussian blur with the chosen sigma of 1. 0, the computed threshold value is 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. As we have seen before, pixels above the threshold value will be turned on, those below the threshold will be turned off

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Finally, we use the mask to select the foreground

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Application. measuring root mass

Let us now turn to an application where we can apply thresholding and other techniques we have learned to this point. 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

Suppose we are interested in the amount of plant material in each image, and in particular how that amount changes from image to image. Perhaps the images represent the growth of the plant over time, or perhaps the images show four different maize varieties at the same phase of their growth. The question we would like to answer is, “how much root mass is in each image?”

We will first construct a Python program to measure this value for a single image. Our strategy will be this

  1. Read the image, converting it to grayscale as it is read. For this application we do not need the colour image
  2. Blur the image
  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. Save the binary image so it can be examined later
  5. Count the white pixels in the binary image, and divide by the number of pixels in the image. This ratio will be a measure of the root mass of the plant in the image
  6. Output the name of the image processed and the root mass ratio

Our intent is to perform these steps and produce the numeric result - a measure of the root mass in the image - without human intervention. 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. Since the function is intended to produce numeric output without human interaction, it does not display any of the images. Almost all of the commands should be familiar, and in fact, it may seem simpler than the code we have worked on thus far, because we are not displaying any of the 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
65

The function begins with reading the original image from 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
941. We 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
62 with the optional 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 automatically convert it to grayscale. Next, the grayscale image is blurred with a Gaussian filter with the 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
793 that is passed to the function. Then we determine 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 with Otsu’s method and create a binary mask just as we did in the previous section. Up to this point, everything should be familiar

The final part of the function determines the root mass ratio in the image. Recall that 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
916, every pixel has either a value of zero [black/background] or one [white/foreground]. We want to count the number of white pixels, which can be accomplished with a call to the numpy 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
947. Then we determine the width and height of the image by using the elements 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
948 [that is, the dimensions of the numpy array that stores the image]. Finally, the density ratio is calculated by dividing the number of white pixels by the total number of 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
949 in the image. The function returns then root density of the image

We can call this function with any filename and provide a sigma value for the blurring. If no sigma value is provided, the default value 1. 0 will be used. For example, for 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 and a sigma value of 1. 5, we would call the function like this

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Now we can use the function to process the series of four images shown above. In a real-world scientific situation, there might be dozens, hundreds, or even thousands of images to process. To save us the tedium of calling the function for each image by hand, we can write a loop that processes all files automatically. The following code block assumes that the files are located in the same directory and the filenames all start with the trial- prefix and end with the . jpg suffix

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

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. Therefore, our calculated root mass ratios include the white pixels of the label and white circle that are not part of the plant root. Those extra pixels affect how accurate the root mass calculation is

How might we remove the labels and circles before calculating the ratio, so that our results are more accurate? Think about some options given what we have learned so far

Dung dịch

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

Dung dịch

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. However, we have reduced the number of extraneous pixels, which should make the output more accurate

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

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. Create a binary mask that leaves the pixels in the bacteria colonies “on” while turning the rest of the pixels in the image “off”

Dung dịch

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

The peak near one corresponds to the white image background, and the broader peak around 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. the dark bacteria colonies. A reasonable choice thus might be to leave pixels below

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 on

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

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

Những điểm chính

  • Thresholding produces a binary image, where all pixels with intensities above [or below] a threshold value are turned on, while all other pixels are turned off

  • The binary images produced by thresholding are held in two-dimensional NumPy arrays, since they have only one colour value channel. They are boolean, hence they contain the values 0 [off] and 1 [on]

  • Thresholding can be used to create masks that select only the interesting parts of an image, or as the first step before edge detection or finding contours

Connected Component Analysis

Tổng quan

Teaching. 70 min
Exercises. 55 min

câu hỏi

  • How to extract separate objects from an image and describe these objects quantitatively

mục tiêu

  • Understand the term object in the context of images

  • Learn about pixel connectivity

  • 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

Objects

In the Thresholding episode we have covered dividing an image into foreground and background pixels. In the shapes example image, we considered the coloured shapes as foreground objects on a white background

In thresholding we went from the original image to this version

Here, we created a mask that only highlights the parts of the image that we find interesting, the objects. All objects have pixel value of

"""
 * 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 while the background pixels are
"""
 * 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

By looking at the mask image, one can count the objects that are present in the image [7]. But how did we actually do that, how did we decide which lump of pixels constitutes a single object?

Pixel Neighborhoods

In order to decide which pixels belong to the same object, one can exploit their neighborhood. pixels that are directly next to each other and belong to the foreground class can be considered to belong to the same object

Let’s discuss the concept of pixel neighborhoods in more detail. Consider the following mask “image” with 8 rows, and 8 columns. For the purpose of illustration, the digit

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 is used to represent background pixels, and the letter
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 is used to represent object pixels foreground]

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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. In order to understand pixel neighborhoods we will introduce the concept of “jumps” between pixels. The jumps follow two rules. First rule is that one jump is only allowed along the column, or the row. 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

Chúng tôi muốn xem lại mặt nạ hình ảnh ví dụ của chúng tôi từ phía trên và áp dụng hai quy tắc vùng lân cận khác nhau. 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 hình ả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

Dung dịch

b] 5

2 jumps

a] 2 b] 3 c] 5

Dung dịch

a] 2

Jumps and neighborhoods

We have just introduced how you can reach different neighboring pixels by performing one or more orthogonal jumps. We have used the terms 1-jump and 2-jump neighborhood. There is also a different way of referring to these neighborhoods. the 4- and 8-neighborhood. With a single jump you can reach four pixels from a given starting pixel. Hence, the 1-jump neighborhood corresponds to the 4-neighborhood. When two orthogonal jumps are allowed, eight pixels can be reached, so the 2-jump neighborhood corresponds to the 8-neighborhood

Connected Component Analysis

In order to find the objects in an image, we want to employ an operation that is called Connected Component Analysis [CCA]. This operation takes a binary image as an input. Usually, 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 value in this image is associated with background pixels, 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 value indicates foreground, or object pixels. Such an image can be produced, e. g. , with thresholding. Given a thresholded image, the connected component analysis produces a new labeled image with integer pixel values. Pixels with the same value, belong to the same object. Skimage provides connected component analysis in 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
977. Let us add this function to the already familiar steps of thresholding an image. Here we define a reusable Python 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
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

Note the new import 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
979 in order to 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
980 function that performs the CCA. The first four lines of code are familiar from the Thresholding episode

Then we call 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
980 function. This function has one positional argument where we pass 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, i. e. , the binary image to work on. With the optional 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
983, we specify the neighborhood in units of orthogonal jumps. 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. The function returns 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 where each pixel has a unique value corresponding to the object it belongs to. In addition, we pass the optional 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
986 to return the maximum label index 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
987

Optional parameters and return values

The optional 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
988 changes the data type that is returned by 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
980. The number of labels is only returned if
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 is True. Otherwise, the function only returns the labeled image. This means that we have to pay attention when assigning the return value to a variable. If we omit the optional 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
988 or pass
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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, we can call the function 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
02

If we pass

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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, the function returns a tuple and we can assign it 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
03

If we used the same assignment as in the first case, the variable

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 would become a tuple, in which
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 is the image 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
996 is the number of labels. This could cause confusion if we assume that
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 only contains the image and pass it to other functions. If you get an
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 or similar, check if you have assigned the return values consistently with the optional parameters

We can call the above 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
978 and display the labeled image like so

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Color mappings

Here you might get a warning

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 or just see an all black image [Note. this behavior might change in future versions or not occur with a different image viewer]

What went wrong? When you hover over the black image, the pixel values are shown as numbers in the lower corner of the viewer. You can see that some pixels have values different 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
764, so they are not actually pure black. Let’s find out more by examining
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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. Properties that might be interesting in this context are
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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, the minimum and maximum value. We can print them with the following lines

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Examining the output can give us a clue why the image appears 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
06

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
003 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
985 is
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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. This means that values in this image range 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
007 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
008. Those are really big numbers. From this available space we only use the range 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
764 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
010. When showing this image in the viewer, it squeezes the complete range into 256 gray values. 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

Fortunately, the skimage library has tools to cope with this situation

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 để chuyển đổi màu sắc 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 để chuyển sang thang độ xám]. 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
011, all objects are coloured according to a list of colours that can be customised. We can use the following commands to convert and show 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
07

How many objects are in that image [15 min]

Now, it is your turn to practice. 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
978, find two ways of printing out the number of objects found in the image

What number of objects would you expect to get?

How does changing 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
793 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
016 values influence the result?

Dung dịch

As you might have guessed, the return 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
987 already contains the number of found images. 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?

Đố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 thuộc tính của các vùng được gá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

Vẽ biểu đồ phân bố diện tích đối tượng [10 phút]

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 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
    
    028
  2. Biểu đồ cho bạn biết gì về các đối tượng?

Dung dịch

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

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

Dung dịch

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 một danh sách các đối tượng đó, sau đó lấy độ dài của danh sách đó làm đối tượng đếm. Đ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

Dung dịch

Để 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

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Dung dịch

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

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

Những điểm chí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
    
    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

câu hỏi

  • 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?

mục tiêu

  • 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

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à 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. Hình ảnh sẽ trông giống như hình ảnh này

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

Dung dịch

Đầ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

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

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

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

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

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

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

Những điểm chính

  • 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

    Làm cách nào để trích xuất dữ liệu từ hình ảnh trong Python?

    Bây giờ chúng tôi có mọi thứ mình cần và có thể dễ dàng trích xuất văn bản từ hình ảnh bằng Python. .
    từ hình ảnh nhập PIL
    từ pytesseract nhập pytesseract
    img = Hình ảnh. mở [đường_đến_hình ảnh]
    văn bản = pytesseract. image_to_string[img]
    in [văn bản]

    Làm cách nào để lấy dữ liệu EXIF ​​​​từ ảnh Python?

    Lấy dữ liệu exif . print[img. getexif[]] Đầu ra. {34665. 210, 36864. b'0220', 37121. b'\x01\x02\x03\x00', 37377. 9. 884, 36867. '2021. 11. 28 12. 35. 52',. . . . } Đoạn mã trên sẽ xuất dữ liệu exif dưới dạng từ điển với khóa tương ứng với từng mục siêu dữ liệu từ TAGS làm khóa.

    Làm cách nào để trích xuất siêu dữ liệu bằng Python?

    Python có thư viện PIL giúp cho việc trích xuất siêu dữ liệu từ một hình ảnh trở nên cực kỳ dễ dàng chỉ bằng một vài dòng. .
    Nhập mô-đun gối
    tải hình ảnh
    Nhận siêu dữ liệu. Siêu dữ liệu thu được
    Chuyển đổi nó thành dạng người có thể đọc được

    Thuộc tính nào được sử dụng để hiển thị kích thước của hình ảnh trong Python?

    open[] được sử dụng để mở hình ảnh và sau đó. chiều rộng và. Thuộc tính height của Image được sử dụng để lấy chiều cao và chiều rộng của hình ảnh.

Chủ Đề