Cara mencetak matriks gambar dengan python

Almost all the operations in this section are mainly related to Numpy rather than OpenCV. A good knowledge of Numpy is required to write better optimized code with OpenCV

Show

*( Contoh akan ditampilkan di terminal Python, karena kebanyakan hanya satu baris kode )*

Accessing and Modifying pixel values

Mari memuat gambar berwarna terlebih dahulu

>>> tegaskan img tidak ada, "file tidak dapat dibaca, periksa dengan os. jalur. ada()"

Anda dapat mengakses nilai piksel dengan koordinat baris dan kolomnya. Untuk gambar BGR, ini mengembalikan array nilai Biru, Hijau, Merah. Untuk gambar skala abu-abu, intensitas yang sesuai dikembalikan

Anda dapat mengubah nilai piksel dengan cara yang sama

>>> img[100,100] = [255,255,255]

Peringatan

Numpy adalah perpustakaan yang dioptimalkan untuk perhitungan larik cepat. Jadi cukup mengakses setiap nilai piksel dan memodifikasinya akan sangat lambat dan tidak disarankan

CatatanMetode di atas biasanya digunakan untuk memilih wilayah array, katakanlah 5 baris pertama dan 3 kolom terakhir. Untuk akses piksel individual, metode array Numpy, array. item() dan larik. itemset() dianggap lebih baik. Mereka selalu mengembalikan skalar, jadi jika Anda ingin mengakses semua nilai B,G,R, Anda perlu memanggil array. item() secara terpisah untuk setiap nilai

Metode pengaksesan dan pengeditan piksel yang lebih baik

>>> img. kumpulan item((10,10,2),100)

Mengakses Properti Gambar

Properti gambar mencakup jumlah baris, kolom, dan saluran;

Bentuk gambar diakses oleh img. membentuk. Ini mengembalikan Tuple dari jumlah baris, kolom, dan saluran (jika gambar berwarna)

CatatanJika sebuah gambar berwarna abu-abu, tupel yang dikembalikan hanya berisi jumlah baris dan kolom, jadi merupakan metode yang baik untuk memeriksa apakah gambar yang dimuat berwarna abu-abu atau berwarna

Jumlah total piksel diakses oleh img.size

Tipe data gambar diperoleh dengan `img. dtype`

Catatanimg. dtype sangat penting saat debugging karena sejumlah besar kesalahan dalam kode OpenCV-Python disebabkan oleh tipe data yang tidak valid

ROI gambar

Terkadang, Anda harus bermain dengan wilayah gambar tertentu. Untuk deteksi mata pada gambar, deteksi wajah pertama dilakukan pada seluruh gambar. Saat wajah diperoleh, kami memilih wilayah wajah saja dan mencari mata di dalamnya alih-alih mencari seluruh gambar. Ini meningkatkan akurasi (karena mata selalu tertuju pada wajah. D ) dan kinerja (karena kami mencari di area kecil)

ROI kembali diperoleh dengan menggunakan pengindeksan Numpy. Di sini saya memilih bola dan menyalinnya ke wilayah lain pada gambar

>>> bola = img[280. 340, 330. 390]

>>> img[273. 333, 100. 160] = bola

Periksa hasilnya di bawah ini

Cara mencetak matriks gambar dengan python

Memisahkan dan Menggabungkan Saluran Gambar

Terkadang Anda perlu bekerja secara terpisah pada saluran B,G,R dari suatu gambar. Dalam hal ini, Anda perlu membagi gambar BGR menjadi saluran tunggal. Dalam kasus lain, Anda mungkin perlu bergabung dengan masing-masing saluran ini untuk membuat gambar BGR. Anda dapat melakukan ini hanya dengan

Atau

Misalkan Anda ingin menyetel semua piksel merah ke nol - Anda tidak perlu membagi saluran terlebih dahulu. Pengindeksan Numpy lebih cepat

Peringatan

adalah operasi yang mahal (dalam hal waktu). Jadi gunakan hanya jika perlu. Kalau tidak, gunakan pengindeksan Numpy

Membuat Batas untuk Gambar (Padding)

Jika Anda ingin membuat bingkai di sekitar gambar, Anda dapat menggunakan sesuatu seperti bingkai foto. Tetapi ia memiliki lebih banyak aplikasi untuk operasi konvolusi, bantalan nol, dll. Fungsi ini membutuhkan argumen berikut

  • src - masukan gambar
  • atas, bawah, kiri, kanan - lebar batas dalam jumlah piksel dalam arah yang sesuai
  • borderType - Bendera yang menentukan jenis perbatasan yang akan ditambahkan. Ini bisa menjadi tipe berikut
  • nilai - Warna batas jika jenis batas adalah

Di bawah ini adalah contoh kode yang mendemonstrasikan semua tipe perbatasan ini untuk pemahaman yang lebih baik

dari matplotlib impor pyplot sebagai plt

assert img1 is not None, "file could not be read, check with os. jalur. ada()"

plt. anak petak(231),plt. imshow(img1,'abu-abu'),plt. judul('ASLI')

plt. anak petak(232),plt. imshow(replikasi,'abu-abu'),plt. judul('REPLIKASI')

plt. anak petak(233),plt. imshow(mencerminkan,'abu-abu'),plt. judul('REFLEKSI')

plt. anak petak(234),plt. imshow(reflect101,'gray'),plt. judul('REFLECT_101')

plt. anak petak(235),plt. imshow(bungkus,'abu-abu'),plt. judul('BUNGKUS')

plt. anak petak(236),plt. imshow(konstan,'abu-abu'),plt. judul('KONSTANT')

Lihat hasilnya di bawah ini. (Gambar ditampilkan dengan matplotlib. Jadi saluran RED dan BLUE akan dipertukarkan)

Mengenali masalah morfometrik (yang berhubungan dengan jumlah, ukuran, atau bentuk objek dalam sebuah gambar)

Karena sistem komputer menjadi lebih cepat dan lebih kuat, dan kamera serta sistem pencitraan lainnya telah menjadi hal biasa di banyak bidang kehidupan lainnya, kebutuhan telah berkembang bagi para peneliti untuk dapat memproses dan menganalisis data gambar. Mempertimbangkan besarnya volume data yang dapat dilibatkan - gambar beresolusi tinggi yang menghabiskan banyak ruang disk/memori virtual, dan/atau kumpulan banyak gambar yang harus diproses bersama - dan sifat yang memakan waktu dan rawan kesalahan

Pelajaran ini memperkenalkan perangkat sumber terbuka untuk memproses data gambar. bahasa pemrograman Python dan perpustakaan scikit-image (________0______32). Dengan desain eksperimental yang hati-hati, kode Python dapat menjadi alat yang ampuh dalam menjawab berbagai jenis pertanyaan

Penggunaan Pemrosesan Citra dalam Penelitian

Pemrosesan otomatis dapat digunakan untuk menganalisis berbagai properti gambar, termasuk distribusi dan perubahan warna pada gambar, jumlah, ukuran, posisi, orientasi, dan bentuk objek pada gambar, dan bahkan - bila digabungkan dengan pembelajaran mesin

Beberapa contoh metode pengolahan citra yang diterapkan dalam penelitian antara lain

  • pencitraan Lubang Hitam
  • memperkirakan populasi Penguin Kaisar
  • analisis keanekaragaman plankton laut skala global
  • segmentasi hati dan pembuluh darah dari gambar CT

Dengan pelajaran ini, kami bertujuan untuk memberikan landasan menyeluruh dalam konsep dasar dan keterampilan bekerja dengan data gambar dengan Python. Sebagian besar contoh yang digunakan dalam pelajaran ini berfokus pada satu kelas tertentu dari teknik pemrosesan gambar, morfometrik, tetapi apa yang akan Anda pelajari dapat digunakan untuk memecahkan masalah yang jauh lebih luas.

Morfometrik

Morfometri melibatkan menghitung jumlah objek dalam gambar, menganalisis ukuran objek, atau menganalisis bentuk objek. Sebagai contoh, kita mungkin tertarik untuk secara otomatis menghitung jumlah koloni bakteri yang tumbuh di cawan Petri, seperti yang ditunjukkan pada gambar ini

Cara mencetak matriks gambar dengan python

Kita dapat menggunakan pemrosesan gambar untuk menemukan koloni, menghitungnya, lalu menyorot lokasinya pada gambar asli, menghasilkan gambar seperti ini

Cara mencetak matriks gambar dengan python

Mengapa menulis sebuah program untuk melakukan itu?

Perhatikan bahwa Anda dapat dengan mudah menghitung secara manual jumlah koloni bakteri yang ditunjukkan pada contoh morfometrik di atas. Mengapa kita harus mempelajari cara menulis program Python untuk melakukan tugas yang dapat kita lakukan dengan mudah dengan mata kepala sendiri?

  1. Bagaimana jika ada lebih banyak koloni bakteri di cawan Petri?

    Cara mencetak matriks gambar dengan python

    Menghitung koloni secara manual pada gambar itu akan menghadirkan lebih banyak tantangan. A Python program using skimage could count the number of colonies more accurately, and much more quickly, than a human could

  2. Bagaimana jika Anda memiliki ratusan, atau ribuan gambar untuk dipertimbangkan? . Program Python yang menggunakan skimage dapat menelusuri semua gambar dalam hitungan detik;

Seperti yang Anda lihat, teknik pemrosesan gambar / visi komputer sederhana yang akan Anda pelajari selama lokakarya ini dapat menjadi alat yang sangat berharga untuk penelitian ilmiah

Saat kita menjalani lokakarya ini, kita akan mempelajari metode analisis citra yang berguna untuk berbagai masalah ilmiah. Ini akan dihubungkan bersama dan diterapkan pada masalah nyata dalam tantangan batu penjuru akhir lokakarya

Mari kita mulai, dengan mempelajari beberapa dasar tentang bagaimana gambar direpresentasikan dan disimpan secara digital

Poin Kunci

  • Teknik sederhana Python dan skimage (scikit-image) dapat digunakan untuk memecahkan masalah analisis gambar asli

  • Masalah morfometrik menyangkut jumlah, bentuk, dan/atau ukuran objek dalam suatu citra


Dasar Gambar

Ringkasan

Pengajaran. 20 menit
Latihan. 5 menit

Pertanyaan

  • Bagaimana gambar direpresentasikan dalam format digital?

Tujuan

  • Tentukan istilah bit, byte, kilobyte, megabyte, dll

  • Jelaskan bagaimana gambar digital terdiri dari piksel

  • Rekomendasikan menggunakan imageio (resp. skimage) untuk I/O (resp. tugas pemrosesan gambar).

  • Jelaskan bagaimana gambar disimpan dalam array NumPy

  • Jelaskan sistem koordinat kiri yang digunakan dalam gambar digital

  • Jelaskan model warna aditif RGB yang digunakan dalam gambar digital

  • Jelaskan urutan ketiga nilai warna pada gambar skimage

  • Jelaskan karakteristik format gambar BMP, JPEG, dan TIFF

  • Jelaskan perbedaan antara kompresi lossy dan lossless

  • Jelaskan keuntungan dan kerugian dari format gambar terkompresi

  • Jelaskan informasi apa yang dapat dimuat dalam metadata gambar

The images we see on hard copy, view with our electronic devices, or process with our programs are represented and stored in the computer as numeric abstractions, approximations of what we see with our eyes in the real world. Sebelum kita mulai mempelajari cara memproses gambar dengan program Python, kita perlu meluangkan waktu untuk memahami cara kerja abstraksi ini

Piksel

Penting untuk disadari bahwa gambar disimpan sebagai susunan persegi panjang yang terdiri dari ratusan, ribuan, atau jutaan "elemen gambar" diskrit, atau dikenal sebagai piksel. Setiap piksel dapat dianggap sebagai satu titik persegi cahaya berwarna

Misalnya, perhatikan gambar bibit jagung ini, dengan area persegi yang ditandai dengan kotak merah

Cara mencetak matriks gambar dengan python

Sekarang, jika kita memperbesar cukup dekat untuk melihat piksel dalam kotak merah, kita akan melihat sesuatu seperti ini

Cara mencetak matriks gambar dengan python

Perhatikan bahwa setiap kotak di area gambar yang diperbesar - setiap piksel - semuanya adalah satu warna, tetapi setiap piksel dapat memiliki warna yang berbeda dari tetangganya. Dilihat dari kejauhan, piksel-piksel ini tampak menyatu membentuk gambar yang kita lihat

Bekerja dengan Piksel

Seperti disebutkan, dalam praktiknya, gambar dunia nyata biasanya terdiri dari sejumlah besar piksel, dan masing-masing piksel ini akan menjadi salah satu dari jutaan warna yang berpotensi. Sementara kita akan menangani gambar dengan kerumitan seperti itu sebentar lagi, mari kita mulai eksplorasi kita dengan 15 piksel dalam matriks 5 X 3 dengan 2 warna dan teruskan hingga kompleksitas itu

Matriks, array, gambar, dan piksel

Matriks adalah konsep matematika - angka yang disusun secara merata dalam persegi panjang. Ini bisa berupa persegi panjang dua dimensi, seperti bentuk layar yang Anda lihat sekarang. Atau bisa juga tiga dimensi yang setara, berbentuk kubus, atau memiliki dimensi yang lebih banyak lagi, tetapi selalu menjaga susunan angka yang berjarak sama. Dalam komputasi, array mengacu pada struktur dalam memori komputer tempat data disimpan dalam elemen dengan jarak yang sama. Ini sangat analog dengan matriks. 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
_33 adalah tipe variabel (contoh tipe yang lebih sederhana adalah integer). Untuk tujuan kami, perbedaan antara matriks dan array tidak penting, kami tidak terlalu peduli bagaimana komputer mengatur data kami di memorinya. Hal yang penting adalah komputer menyimpan nilai yang menggambarkan piksel dalam gambar, sebagai larik. Dan istilah matriks dan larik dapat digunakan secara bergantian

Pertama, impor yang diperlukan

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

Impor Pernyataan dengan Python

Dalam Python, pernyataan

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 digunakan untuk memuat fungsionalitas tambahan ke dalam program. Ini diperlukan ketika kita ingin kode kita melakukan sesuatu yang lebih terspesialisasi, yang tidak dapat dengan mudah dicapai dengan seperangkat alat dasar dan struktur data terbatas yang tersedia di lingkungan default Python.

Fungsionalitas tambahan dapat dimuat sebagai satu fungsi atau objek, modul yang mendefinisikan beberapa di antaranya, atau pustaka yang berisi banyak modul. Anda akan menemukan beberapa bentuk pernyataan ________0______34 yang berbeda

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

Penjelasan lebih lanjut

Pada contoh di atas, form 1 memuat seluruh pustaka

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 ke dalam program sebagai objek. Modul individual perpustakaan kemudian tersedia di dalam objek itu, mis. g. , untuk mengakses fungsi
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 yang digunakan dalam episode menggambar, Anda akan menulis
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Formulir 2 hanya memuat modul

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 dari
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 ke dalam program. Saat kami menjalankan kode, program akan memakan waktu lebih sedikit dan menggunakan lebih sedikit memori karena kami tidak akan memuat seluruh perpustakaan
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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. Sintaks yang diperlukan untuk menggunakan modul tetap tidak berubah. untuk mengakses fungsi
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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, kita akan menggunakan pemanggilan fungsi yang sama seperti yang diberikan untuk formulir 1

Untuk lebih mengurangi waktu dan kebutuhan memori untuk program Anda, formulir 3 dapat digunakan untuk mengimpor hanya fungsi/kelas tertentu dari perpustakaan/modul. Berbeda dengan bentuk lain, ketika pendekatan ini digunakan, fungsi atau kelas yang diimpor hanya dapat dipanggil dengan namanya saja, tanpa diawali dengan nama modul/perpustakaan dari mana ia dimuat, 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 bukan ________0______38 menggunakan contoh di atas. Salah satu bahaya dari bentuk ini adalah mengimpor seperti ini akan menimpa objek apa pun dengan nama yang sama yang telah ditentukan/diimpor sebelumnya dalam program, i. e. , contoh di atas akan mengganti objek apa pun yang ada bernama
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 dengan fungsi ________0______37 dari
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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

Terakhir, kata kunci

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 dapat digunakan saat mengimpor, untuk menentukan nama yang akan digunakan sebagai singkatan untuk perpustakaan/modul yang diimpor. Nama ini disebut sebagai alias. Biasanya, menggunakan alias (seperti
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 untuk perpustakaan NumPy) menghemat sedikit pengetikan. You may see
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
48 combined with any of the other first three forms of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
34 statement

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

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

%matplotlib widget

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

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
52 function from the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53 module and display it using the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
54 function from the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
55 module.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56 is a Python library for reading and writing image data.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53 is specifying that we want to use version 3 of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56. This version has the benefit of supporting nD (multidimensional) image data natively (think of volumes, movies)

Why not 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 59

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
32 library has its own function to read an image, so you might be asking why we don’t use it here. Actually,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
59 uses
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
62 internally when loading an image into Python. It is certainly something you may use as you see fit in your own code. 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. Using
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56 gives us more flexibility, especially when it comes to handling metadata

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)

Cara mencetak matriks gambar dengan python

You might be thinking, “That does look vaguely like an eight, and I see two colours but how can that be only 15 pixels”. The display of the eight you see does use a lot more screen pixels to display our eight so large, but that does not mean there is information for all those screen pixels in the file. All those extra pixels are a consequence of our viewer creating additional pixels through interpolation. It could have just displayed it as a tiny image using only 15 screen pixels if the viewer was designed differently

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

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

print(image.shape)
print(image)

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]

Thus if we have tools that will allow us to manipulate these arrays of numbers, we can manipulate the image. 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
33 library can be particularly useful here, so let’s try that out using
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
33 array slicing. Notice that the default behavior of the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
54 function appended row and column numbers that will be helpful to us as we try to address individual or groups of pixels. First let’s load another copy of our eight, and then make it look like a zero

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

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]

Cara mencetak matriks gambar dengan python

Coordinate system

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

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

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

This is called a left-hand coordinate system. Jika Anda memegang tangan kiri Anda di depan wajah Anda dan mengarahkan ibu jari Anda ke lantai, jari telunjuk Anda yang terulur akan sesuai dengan sumbu x sedangkan ibu jari Anda mewakili sumbu y

Cara mencetak matriks gambar dengan python

Sampai Anda telah bekerja dengan gambar untuk sementara waktu, kesalahan paling umum yang akan Anda buat dengan koordinat adalah lupa bahwa koordinat y menjadi lebih besar saat turun, bukan naik seperti dalam sistem koordinat Cartesian normal. Konsekuensinya, mungkin berguna untuk berpikir dalam hal menghitung mundur baris (r) untuk sumbu y dan melintasi kolom (c) untuk sumbu x. Ini dapat sangat membantu dalam kasus di mana Anda perlu mengubah urutan data penampil gambar yang disediakan dalam format x,y ke format y,x. Jadi, kami akan menggunakan cx dan ry jika perlu untuk membantu menjembatani kedua pendekatan ini

Mengubah Nilai Piksel (5 mnt)

Muat salinan lain dari delapan bernama lima, lalu ubah nilai piksel sehingga Anda memiliki tampilan seperti 5, bukan 8. Tampilkan gambar dan cetak matriksnya juga

Larutan

Ada banyak solusi yang mungkin, tetapi satu metode adalah. .

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]

Cara mencetak matriks gambar dengan python

More colours

Sampai saat ini, kami hanya memiliki matriks 2 warna, tetapi kami dapat memiliki lebih banyak jika kami menggunakan angka atau pecahan lain. Salah satu cara umum adalah menggunakan angka antara 0 dan 255 untuk memungkinkan 256 warna berbeda atau 256 tingkat abu-abu berbeda. Mari kita coba itu

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

Cara mencetak matriks gambar dengan python

We now have 3 colours, but are they the three colours you expected? They all appear to be on a continuum of dark purple on the low end and yellow on the high end. This is a consequence of the default colour map (cmap) in this library. You can think of a colour map as an association or mapping of numbers to a specific colour. However, the goal here is not to have one number for every possible colour, but rather to have a continuum of colours that demonstrate relative intensity. In our specific case here for example, 255 or the highest intensity is mapped to yellow, and 0 or the lowest intensity is mapped to a dark purple. The best colour map for your data will vary and there are many options built in, but this default selection was not arbitrary. A lot of science went into making this the default due to its robustness when it comes to how the human mind interprets relative colour values, grey-scale printability, and colour-blind friendliness (You can read more about this default colour map in a Matplotlib tutorial and an explanatory article by the authors). Thus it is a good place to start, and you should change it only with purpose and forethought. For now, let’s see how you can do that using an alternative map you have likely seen before where it will be even easier to see it as a mapped continuum of intensities. greyscale

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

Cara mencetak matriks gambar dengan python

Above we have exactly the same underying data matrix, but in greyscale. Zero maps to black, 255 maps to white, and 128 maps to medium grey. Here we only have a single channel in the data and utilize a grayscale color map to represent the luminance, or intensity of the data and correspondingly this channel is referred to as the luminance channel

Even More Colours

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. Having a one to one mapping of number to colour like this would be inefficient and make adjustments and building tools to do so very difficult. Rather than larger numbers, the solution is to have more numbers in more dimensions. Storing the numbers in a multi-dimensional matrix where each colour or property like transparency is associated with its own dimension allows for individual contributions to a pixel to be adjusted independently. This ability to manipulate properties of groups of pixels separately will be key to certain techniques explored in later chapters of this lesson. To get started let’s see an example of how different dimensions of information combine to produce a set of pixels using a 4 X 4 matrix with 3 dimensions for the colours red, green, and blue. 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

Cara mencetak matriks gambar dengan python

Previously we had one number being mapped to one colour or intensity. Now we are combining the effect of 3 numbers to arrive at a single colour value. Let’s see an example of that using the blue square at the end of the second row, which has the index [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. array([ 7, 1, 110]) The integers in order represent Red, Green, and Blue. Looking at the 3 values and knowing how they map, can help us understand why it is blue. Jika kita membagi setiap nilai dengan 255, yang merupakan maksimum, kita dapat menentukan seberapa besar kontribusinya relatif terhadap potensi maksimumnya. Effectively, the red is at 7/255 or 2. 8 percent of its potential, the green is at 1/255 or 0. 4 persen, dan biru adalah 110/255 atau 43. 1 persen dari potensinya. So when you mix those three intensities of colour, blue is winning by a wide margin, but the red and green still contribute to make it a slightly different shade of blue than 0,0,110 would be on its own

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. We can do that by multiplying our image array representation with a 1d matrix that has a one for the channel we want to keep and zeros for the rest

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

Cara mencetak matriks gambar dengan 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
6

Cara mencetak matriks gambar dengan 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
7

Cara mencetak matriks gambar dengan python

If we look at the upper [1, 3] square in all three figures, we can see each of those colour contributions in action. Notice that there are several squares in the blue figure that look even more intensely blue than square [1, 3]. When all three channels are combined though, the blue light of those squares is being diluted by the relative strength of red and green being mixed in with them

24-bit RGB Colour

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

As we saw, the RGB model is an additive colour model, which means that the primary colours are mixed together to form other colours. Most frequently, the amount of the primary colour added is represented as an integer in the closed range [0, 255] as seen in the example. Therefore, there are 256 discrete amounts of each primary colour that can be added to produce another colour. The number of discrete amounts of each colour, 256, corresponds to the number of bits used to hold the colour channel value, which is eight (28=256). Since we have three channels with 8 bits for each (8+8+8=24), this is called 24-bit colour depth

Any particular colour in the RGB model can be expressed by a triplet of integers in [0, 255], representing the red, green, and blue channels, respectively. A larger number in a channel means that more of that primary colour is present

Thinking about RGB colours (5 min)

Suppose that we represent colours as triples (r, g, b), where each of r, g, and b is an integer in [0, 255]. What colours are represented by each of these triples? (Try to answer these questions without reading further. )

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

Larutan

  1. (255, 0, 0) represents red, because the red channel is maximised, while the other two channels have the minimum values
  2. (0, 255, 0) represents green
  3. (0, 0, 255) represents blue
  4. (255, 255, 255) is a little harder. When we mix the maximum value of all three colour channels, we see the colour white
  5. (0, 0, 0) represents the absence of all colour, or black
  6. (128, 128, 128) represents a medium shade of gray. Note that the 24-bit RGB colour model provides at least 254 shades of gray, rather than only fifty

Note that the RGB colour model may run contrary to your experience, especially if you have mixed primary colours of paint to create new colours. In the RGB model, the lack of any colour is black, while the maximum amount of each of the primary colours is white. With physical paint, we might start with a white base, and then add differing amounts of other paints to produce a darker shade

After completing the previous challenge, we can look at some further examples of 24-bit RGB colours, in a visual way. The image in the next challenge shows some colour names, their 24-bit RGB triplet values, and the colour itself

RGB colour table (optional, not included in timing)

Cara mencetak matriks gambar dengan python

We cannot really provide a complete table. To see why, answer this question. How many possible colours can be represented with the 24-bit RGB model?

Larutan

There are 24 total bits in an RGB colour of this type, and each bit can be on or off, and so there are 224 = 16,777,216 possible colours with our additive, 24-bit RGB colour model

Although 24-bit colour depth is common, there are other options. We might have 8-bit colour (3 bits for red and green, but only 2 for blue, providing 8 × 8 × 4 = 256 colours) or 16-bit colour (4 bits for red, green, and blue, plus 4 more for transparency, providing 16 × 16 × 16 = 4096 colours), for example. 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

Jika Anda menggunakan layar laptop atau monitor LCD lama atau murah untuk melihat gambar, mungkin hanya mendukung warna 18-bit, yang mampu menampilkan 64 × 64 × 64 = 262.144 warna. 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

Kami dapat menggabungkan sistem koordinat kami dengan model warna RGB 24-bit untuk mendapatkan pemahaman konseptual tentang gambar yang akan kami kerjakan. An image is a rectangular array of pixels, each with its own coordinate. Each pixel in the image is a square point of coloured light, where the colour is specified by a 24-bit RGB triplet. Gambar seperti itu adalah contoh grafik raster

Format gambar

Meskipun gambar yang akan kami manipulasi dalam program kami dikonseptualisasikan sebagai array persegi panjang dari triplet RGB, gambar tersebut tidak harus dibuat, disimpan, atau ditransmisikan dalam format tersebut. There are several image formats we might encounter, and we should know the basics of at least of few of them. Beberapa format yang mungkin kami temui, dan ekstensi filenya, ditampilkan di tabel ini

FormatExtensionDevice-Independent Bitmap (BMP). bmpJoint Photographic Experts Group (JPEG). jpg or . jpegTagged Image File Format (TIFF). tif or . tiff

BMP

The file format that comes closest to our preceding conceptualisation of images is the Device-Independent Bitmap, or BMP, file format. BMP files store raster graphics images as long sequences of binary-encoded numbers that specify the colour of each pixel in the image. 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. Depending on how it was created, a BMP image might have 8-bit, 16-bit, or 24-bit colour depth

24-bit BMP images have a relatively simple file format, can be viewed and loaded across a wide variety of operating systems, and have high quality. However, BMP images are not compressed, resulting in very large file sizes for any useful image resolutions

The idea of image compression is important to us for two reasons. 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. Since compression is important to us, we should take a brief detour and discuss the concept

Image compression

Before discussing additional formats, familiarity with image compression will be helpful. Let’s delve into that subject with a challenge. For this challenge, you will need to know about bits / bytes and how those are used to express computer storage capacities. If you already know, you can skip to the challenge below

Bits and bytes

Before we talk specifically about images, we first need to understand how numbers are stored in a modern digital computer. When we think of a number, we do so using a decimal, or base-10 place-value number system. 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. But, the electronic circuits inside a computer are much easier to construct if we restrict the numeric base to only two, instead of 10. (It is easier for circuitry to tell the difference between two voltage levels than it is to differentiate among 10 levels. ) So, values in a computer are stored using a binary, or base-2 place-value number system

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). We might imagine a four-bit binary number, 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

Internally, computers have a minimum number of bits that they work with at a given time. eight. A group of eight bits is called a 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. If we used an uncompressed image format such as BMP, with the 24-bit RGB colour model, how much storage would be required for the file?

Larutan

In such an image, there are 5,000 × 5,000 = 25,000,000 pixels, and 24 bits for each pixel, leading to 25,000,000 × 24 = 600,000,000 bits, or 75,000,000 bytes (71. 5MB). That is quite a lot of space for a very uninteresting image

Since image files can be very large, various compression schemes exist for saving (approximately) the same information while using less space. These compression techniques can be categorised as lossless or lossy

Lossless compression

In lossless image compression, we apply some algorithm (i. e. , a computerised procedure) to the image, resulting in a file that is significantly smaller than the uncompressed BMP file equivalent would be. Then, when we wish to load and view or process the image, our program reads the compressed file, and reverses the compression process, resulting in an image that is identical to the original. Nothing is lost in the process – hence the term “lossless. ”

The general idea of lossless compression is to somehow detect long patterns of bytes in a file that are repeated over and over, and then assign a smaller bit pattern to represent the longer sample. Then, the compressed file is made up of the smaller patterns, rather than the larger ones, thus reducing the number of bytes required to save the file. 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

To provide you with a concrete example, consider the 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 . zip version of the image is three orders of magnitude smaller than the original, and it can be decompressed into a file that is byte-for-byte the same as the original. Since the original is so repetitious - simply the same colour triplet repeated 25,000,000 times - the compression algorithm can dramatically reduce the size of the file

If you work with . zip or . gz archives, you are dealing with lossless compression

Lossy compression

Lossy compression takes the original image and discards some of the detail in it, resulting in a smaller file format. 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. The more detail that is sacrificed, the smaller the image files will be - but of course, the detail and richness of the image will be lower as well

This is probably fine for images that are shown on Web pages or printed off on 4 × 6 photo paper, but may or may not be fine for scientific work. You will have to decide whether the loss of image quality and detail are important to your work, versus the space savings afforded by a lossy compression format

It is important to understand that once an image is saved in a lossy compression format, the lost detail is just that - lost. I. e. , unlike lossless formats, given an image saved in a lossy format, there is no way to reconstruct the original image in a byte-by-byte manner

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)

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

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

Examine the file sizes of the two output 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
70 and
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
71. Does the BMP image size match our previous prediction? How about the JPEG?

Larutan

File 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, berukuran 75.000.054 byte, yang sangat cocok dengan prediksi kami. The JPEG file,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
71, is 392,503 bytes, two orders of magnitude smaller than the bitmap version

Membandingkan kompresi lossless versus lossy (opsional, tidak termasuk dalam pengaturan waktu)

Mari kita lihat contoh langsung dari kompresi lossless versus lossy. Sekali lagi, buka terminal dan arahkan ke direktori ________0______74. The two output images,

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

We can apply lossless compression to any file by using the

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
78 command. Recall that 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
70 file contains 75,000,054 bytes. Apply lossless compression to this image by executing the following command.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
80. This command tells the computer to create a new compressed 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
81, from the original bitmap image. Execute a similar command on the tree JPEG file.
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
82

Having created the compressed file, 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
83 command to display the contents of the directory. How big are the compressed files? How do those compare to the size 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
70 and
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
77? What can you conclude from the relative sizes?

Larutan

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

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

We can see that the regularity of the bitmap image (remember, it is a 5,000 x 5,000 pixel image containing only white pixels) allows the lossless compression scheme to compress the file quite effectively. On the other hand, compressing

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 does not create a much smaller file; this is because the JPEG image was already in a compressed format

Here is an example showing how JPEG compression might impact image quality. Consider this image of several maize seedlings (scaled down here from 11,339 × 11,336 pixels in order to fit the display)

Cara mencetak matriks gambar dengan python

Now, let us zoom in and look at a small section of the label in the original, first in the uncompressed format

Cara mencetak matriks gambar dengan python

Here is the same area of the image, but in JPEG format. We used a fairly aggressive compression parameter to make the JPEG, in order to illustrate the problems you might encounter with the format

Cara mencetak matriks gambar dengan python

The JPEG image is of clearly inferior quality. It has less colour variation and noticeable pixelation. Quality differences become even more marked when one examines the colour histograms for each image. A histogram shows how often each colour value appears in an image. The histograms for the uncompressed (left) and compressed (right) images are shown below

Cara mencetak matriks gambar dengan python

We learn how to make histograms such as these later on in the workshop. The differences in the colour histograms are even more apparent than in the images themselves; clearly the colours in the JPEG image are different from the uncompressed version

If the quality settings for your JPEG images are high (and the compression rate therefore relatively low), the images may be of sufficient quality for your work. It all depends on how much quality you need, and what restrictions you have on image storage space. Another consideration may be where the images are stored. For example,if your images are stored in the cloud and therefore must be downloaded to your system before you use them, you may wish to use a compressed image format to speed up file transfer time

PNG

PNG images are well suited for storing diagrams. It uses a lossless compression and is hence often used in web applications for non-photographic images. The format is able to store RGB and plain luminance (single channel, without an associated color) data, among others. Image data is stored row-wise and then, per row, a simple filter, like taking the difference of adjacent pixels, can be applied to increase the compressability of the data. The filtered data is then compressed in the next step and written out to the disk

TIFF

TIFF images are popular with publishers, graphics designers, and photographers. TIFF images can be uncompressed, or compressed using either lossless or lossy compression schemes, depending on the settings used, and so TIFF images seem to have the benefits of both the BMP and JPEG formats. The main disadvantage of TIFF images (other than the size of images in the uncompressed version of the format) is that they are not universally readable by image viewing and manipulation software

Metadata

JPEG and TIFF images support the inclusion of metadata in images. Metadata is textual information that is contained within an image file. Metadata holds information about the image itself, such as when the image was captured, where it was captured, what type of camera was used and with what settings, etc. We normally don’t see this metadata when we view an image, but we can view it independently if we wish to (see , below). The important thing to be aware of at this stage is that you cannot rely on the metadata of an image being fully preserved when you use software to process that image. The image reader/writer library that we use throughout this lesson,

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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, includes metadata when saving new images but may fail to keep certain metadata fields. In any case, remember. if metadata is important to you, take precautions to always preserve the original files

Accessing Metadata

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
53 provides a way to display or explore the metadata associated with an image. Metadata is served independently from pixel data

%matplotlib widget
0

%matplotlib widget
1

Other software exists that can help you handle metadata, e. g. , Fiji and ImageMagick. You may want to explore these options if you need to work with the metadata of your images

Summary of image formats used in this lesson

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

FormatCompressionMetadataAdvantagesDisadvantagesBMPNoneNoneUniversally viewable,Large file sizes   high quality JPEGLossyYesUniversally viewable,Detail may be lost   smaller file size PNGLosslessUniversally viewable, open standard, smaller file sizeMetadata less flexible than TIFF, RGB onlyTIFFNone, lossy,YesHigh quality orNot universally viewable or lossless smaller file size 

Poin Kunci

  • Digital images are represented as rectangular arrays of square pixels

  • Digital images use a left-hand coordinate system, with the origin in the upper left corner, the x-axis running to the right, and the y-axis running down. Some learners may prefer to think in terms of counting down rows for the y-axis and across columns for the x-axis. Thus, we will make an effort to allow for both approaches in our lesson presentation

  • Most frequently, digital images use an additive RGB model, with eight bits for the red, green, and blue channels

  • skimage images are stored as multi-dimensional NumPy arrays

  • In skimage images, the red channel is specified first, then the green, then the blue, i. e. , RGB

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

  • BMP images are uncompressed, meaning they have high quality but also that their file sizes are large

  • JPEG images use lossy compression, meaning that their file sizes are smaller, but image quality may suffer

  • TIFF images can be uncompressed or compressed with lossy or lossless compression

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


Working with skimage

Ringkasan

Teaching. 70 min
Exercises. 50 min

Pertanyaan

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

Tujuan

  • Read and save images with imageio

  • Display images with matplotlib

  • Resize images with skimage

  • Perform simple image thresholding with NumPy array operations

  • Extract sub-images using array slicing

We have covered much of how images are represented in computer software. In this episode we will learn some more methods for accessing and changing digital images

Reading, displaying, and saving images

Imageio provides intuitive functions for reading and writing (saving) images. Semua format gambar populer, seperti BMP, PNG, JPEG, dan TIFF didukung, bersama dengan beberapa format esoteris lainnya. Check the Supported Formats docs for a list of all formats. Matplotlib provides a large collection of plotting utilities

Let us examine a simple Python program to load, display, and save an image to a different format. Here are the first few lines

%matplotlib widget
2

First, we import 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
89 module of 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) 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
91 so we can read and write images. Then, 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 read a JPEG image entitled chair. jpg. Imageio reads the image, converts it from JPEG into a NumPy array, and returns the array; we save the array in a variable 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

Next, we will do something with the image

%matplotlib widget
3

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

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
94 so that we will have a fresh figure with a set of axis independent from our previous calls. Selanjutnya kita memanggil
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 untuk menampilkan gambar

Sekarang, kami akan menyimpan gambar dalam format lain

%matplotlib widget
4

The final statement in the 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
96, writes the image to a file 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
97 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
74 directory. The
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
99 function automatically determines the type of the file, based on the file extension we provide. In this case, the
%matplotlib widget
00 extension causes the image to be saved as a TIFF

Metadata, revisited

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

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
99 will not retain all metadata associated with the original image that was loaded into Python. If the image metadata is important to you, be sure to always keep an unchanged copy of the original image

Extensions do not always dictate file type

The

%matplotlib widget
02 function automatically uses the file type we specify in the file name parameter’s extension. Note that this is not always the case. For example, if we are editing a document in Microsoft Word, and we save the document as
%matplotlib widget
03 instead of
%matplotlib widget
04, the file is not saved as a PDF document

Named versus positional arguments

When we call functions in Python, there are two ways we can specify the necessary arguments. We can specify the arguments positionally, i. e. , in the order the parameters appear in the function definition, or we can use named arguments

For example, the

%matplotlib widget
02 function definition specifies two parameters, the resource to save the image to (e. g. , a file name, an http address) and the image to write to disk. So, we could save the chair image in the sample code above using positional arguments like this

%matplotlib widget
06

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

%matplotlib widget
07 means. The same goes for the second argument

The style we will use in this workshop is to name each argument, 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
96

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

Resizing an image (10 min)

Add

%matplotlib widget
09 and
%matplotlib widget
10 to your list of imports. Using the
%matplotlib widget
11 image located in the data folder, write a Python script to read your image into a variable 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. Then, resize the image to 10 percent of its current size using these lines of code

%matplotlib widget
5

As it is used here, the parameters to the

%matplotlib widget
13 function are the image to transform,
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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, the dimensions we want the new image to have,
%matplotlib widget
15

Note that the pixel values in the new image are an approximation of the original values and should not be confused with actual, observed data. This is because

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 interpolates the pixel values when reducing or increasing the size of an image.
%matplotlib widget
17 has a number of optional parameters that allow the user to control this interpolation. You can find more details in the

Image files on disk are normally stored as whole numbers for space efficiency, but transformations and other math operations often result in conversion to floating point numbers. Using the

%matplotlib widget
18 method converts it back to whole numbers before we save it back to disk. If we don’t convert it before saving,
%matplotlib widget
02 may not recognise it as image data

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

%matplotlib widget
20 in your data directory. Finally, 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
95 with each of your image variables to display both images in your notebook. Don’t forget to use
%matplotlib widget
22 so you don’t overwrite the first image with the second. Images may appear the same size in jupyter, but you can see the size difference by comparing the scales for each. You can also see the differnce in file storage size on disk by hovering your mouse cursor over the original and the new file in the jupyter file browser, using
%matplotlib widget
23 in your shell, or the OS file browser if it is configured to show file sizes

Larutan

Here is what your Python script might look like

%matplotlib widget
6

The script resizes the

%matplotlib widget
24 image by a factor of 10 in both dimensions, saves the result to the
%matplotlib widget
25 file, and displays original and resized for comparision

Manipulating pixels

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

Suppose we are interested in this maize root cluster image. We want to be able to focus our program’s attention on the roots themselves, while ignoring the black background

Cara mencetak matriks gambar dengan python

Since the image is stored as an array of numbers, we can simply look through the array for pixel colour values that are less than some threshold value. This process is called thresholding, and we will see more powerful methods to perform the thresholding task in the Thresholding episode. Here, though, we will look at a simple and elegant NumPy method for thresholding. Let us develop a program that keeps only the pixel colour values in an image that have value greater than or equal to 128. This will keep the pixels that are brighter than half of “full brightness”, i. e. , pixels that do not belong to the black background. We will start by reading the image and displaying it

%matplotlib widget
7

Now we can threshold the image and display the result

%matplotlib widget
8

The NumPy command to ignore all low-intensity pixels is

%matplotlib widget
26. Every pixel colour value in the whole 3-dimensional array with a value less that 128 is set to zero. In this case, the result is an image in which the extraneous background detail has been removed

Cara mencetak matriks gambar dengan python

Converting colour images to grayscale

It is often easier to work with grayscale images, which have a single channel, instead of colour images, which have three channels. Skimage offers the function

%matplotlib widget
27 to achieve this. This function adds up the three colour channels in a way that matches human colour perception, see . It returns a grayscale image with floating point values in the range from 0 to 1. We can use the function
%matplotlib widget
18 in order to convert it back to the original data type and the data range back 0 to 255. Note that it is often better to use image values represented by floating point values, because using floating point numbers is numerically more stable

Colour and %matplotlib widget 29

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

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32 contains many modules and functions that include the US English spelling,
%matplotlib widget
29. The exact spelling matters here, e. g. you will encounter an error if you try to run
%matplotlib widget
32. To account for this, we will use the US English spelling,
%matplotlib widget
29, in example Python code throughout the lesson. You will encounter a similar approach with “centre” and
%matplotlib widget
34

%matplotlib widget
9

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

%matplotlib widget
35 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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
0

Keeping only low intensity pixels (10 min)

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

The file

%matplotlib widget
37 is an RGB image of a sudoku puzzle

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

Hint. this is an instance where it is helpful to convert the image from RGB to grayscale

Larutan

First, load the image file in and convert it to grayscale

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
1

Then, change all high intensity pixel values (> 0. 75) to 0. 75

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
2

Terakhir, tampilkan gambar yang dimodifikasi

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
3

Merencanakan gambar saluran tunggal (cmap, vmin, vmax)

Dibandingkan dengan gambar berwarna, gambar skala abu-abu hanya berisi satu nilai intensitas per piksel. When we plot such an image with

%matplotlib widget
38, matplotlib uses a colour map, to assign each intensity value a colour. The default colour map is called “viridis” and maps low values to purple and high values to yellow. We can instruct matplotlib to map low values to black and high values to white instead, by calling
%matplotlib widget
38 with
%matplotlib widget
40. 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

%matplotlib widget
41 and
%matplotlib widget
42 to get the desired output

If you forget about this, it can lead to unexpected results. Try removing the

%matplotlib widget
42 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

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

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

%matplotlib widget
45

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

%matplotlib widget
46, and then the range of x-coordinates or columns,
%matplotlib widget
47. 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,
%matplotlib widget
48, indicates that we want all three colour channels in our new image

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
4

Then we use array slicing to create a new image with our selected area and then display the new image

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
5

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
6

First, we sample a single pixel’s colour at a particular location of the image, saving it in a variable named

%matplotlib widget
29, which creates a 1 × 1 × 3 NumPy array with the blue, green, and red colour values for the pixel located at (ry = 330, cx = 90). Then, with the
%matplotlib widget
50 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
%matplotlib widget
29 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

Cara mencetak matriks gambar dengan python

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”

Larutan

Here is the completed Python program to select only the plant and roots in the image

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
7

Poin Kunci

  • 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

    %matplotlib widget
    
    53 on the global figure object

  • Colour images can be transformed to grayscale using

    %matplotlib widget
    
    27 or, in many cases, be read as grayscale directly by passing the argument
    %matplotlib widget
    
    35 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

    %matplotlib widget
    
    13 function

  • NumPy array commands, such as

    %matplotlib widget
    
    26, 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. ,

    %matplotlib widget
    
    59

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


Drawing and Bitwise Operations

Ringkasan

Teaching. 45 min
Exercises. 45 min

Pertanyaan

  • How can we draw on skimage images and use bitwise operations and masks to select certain parts of an image?

Tujuan

  • Create a blank, black skimage image

  • Draw rectangles and other shapes on skimage images

  • Explain how a white shape on a black background can be used as a mask to select specific parts of an image

  • Use bitwise operations to apply a mask to an image

The next series of episodes covers a basic toolkit of skimage operators. With these tools, we will be able to create programs to perform simple analyses of images based on changes in colour or shape

Drawing on images

Often we wish to select only a portion of an image to analyze, and ignore the rest. Creating a rectangular sub-image with slicing, as we did in the Image Representation in skimage episode is one option for simple cases. Another option is to create another special image, of the same size as the original, with white pixels indicating the region to save and black pixels everywhere else. Such an image is called a mask. In preparing a mask, we sometimes need to be able to draw a shape - a circle or a rectangle, say - on a black image. skimage provides tools to do that

Consider this image of maize seedlings

Cara mencetak matriks gambar dengan python

Now, suppose we want to analyze only the area of the image containing the roots themselves; we do not care to look at the kernels, or anything else about the plants. Further, we wish to exclude the frame of the container holding the seedlings as well. Hovering over the image with our mouse, could tell us that the upper-left coordinate of the sub-area we are interested in is (44, 357), while the lower-right coordinate is (720, 740). These coordinates are shown in (x, y) order

A Python program to create a mask to select only that area of the image would start with a now-familiar section of code to open and display the original image

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
8

As before, we first import 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
89 submodule of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
56 (
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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). We also import the NumPy library, which we need to create the initial mask image. Then, we import the
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
39 submodule of
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw import disk  # form 3, load only the disk function
import numpy as np             # form 4, load all of numpy into an object called np
32. We load and display the initial image in the same way we have done before

NumPy allows indexing of images/arrays with “boolean” arrays of the same size. Indexing with a boolean array is also called mask indexing. The “pixels” in such a mask array can only take two values.

%matplotlib widget
65 or
%matplotlib widget
66. When indexing an image with such a mask, only pixel values at positions where the mask is
%matplotlib widget
65 are accessed. But first, we need to generate a mask array of the same size as the image. Luckily, the NumPy library provides a function to create just such an array. The next section of code shows how

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
9

The first argument to the

%matplotlib widget
68 function is the shape of the original image, so that our mask will be exactly the same size as the original. Notice, that we have only used the first two indices of our shape. We omitted the channel dimension. Indexing with such a mask will change all channel values simultaneously. The second argument,
%matplotlib widget
69, indicates that the elements in the array should be booleans - i. e. , values are either
%matplotlib widget
65 or
%matplotlib widget
66. Thus, even though we use
%matplotlib widget
72 to create the mask, its pixel values are in fact not
%matplotlib widget
73 but
%matplotlib widget
65. You could check this, e. g. , by
%matplotlib widget
75

Next, we draw a filled, rectangle on the mask

print(image.shape)
print(image)
0

Here is what our constructed mask looks like.

Cara mencetak matriks gambar dengan python

The parameters of the

%matplotlib widget
76 function
%matplotlib widget
77 and
%matplotlib widget
78, are the coordinates of the upper-left (
%matplotlib widget
79) and lower-right (
%matplotlib widget
80) corners of a rectangle in (ry, cx) order. The function returns the rectangle as row (
%matplotlib widget
81) and column (
%matplotlib widget
82) coordinate arrays

Check the documentation

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

%matplotlib widget
83 or
%matplotlib widget
84. Take notes in your lab notebook. And, it is always wise to run some test code to verify that the functions your program uses are behaving in the manner you intend

Variable naming conventions

Anda mungkin bertanya-tanya mengapa kami menyebut nilai kembalian dari fungsi persegi panjang

%matplotlib widget
81 dan
%matplotlib widget
82?. Anda mungkin telah menebak bahwa
%matplotlib widget
87 adalah kependekan dari
%matplotlib widget
88 dan
%matplotlib widget
89 adalah kependekan dari
%matplotlib widget
90. Namun, fungsi persegi panjang mengembalikan banyak baris dan kolom; . Bahkan mungkin lebih jelas untuk menyebutkan variabel tersebut
%matplotlib widget
95 dan
%matplotlib widget
96; . Apa pun yang Anda putuskan untuk dilakukan, cobalah untuk tetap berpegang pada beberapa konvensi yang sudah ada, sehingga lebih mudah bagi orang lain untuk memahami kode Anda

Operasi menggambar lainnya (15 mnt)

Ada fungsi lain untuk menggambar pada gambar, selain fungsi

%matplotlib widget
97. Kita bisa menggambar lingkaran, garis, teks, dan bentuk lainnya juga. 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
%matplotlib widget
99 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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
00 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

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

print(image.shape)
print(image)
1

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

Cara mencetak matriks gambar dengan python

Larutan

Drawing a circle

print(image.shape)
print(image)
2

Drawing a line

print(image.shape)
print(image)
3

print(image.shape)
print(image)
4

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
01 python module, and the function
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
02, which can produce random numbers within a certain range

Let’s draw 15 randomly placed circles

print(image.shape)
print(image)
5

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
01 module, now using the function
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
04 that returns a random number between 0. 0 and 1. 0

print(image.shape)
print(image)
6

Image modification

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

%matplotlib widget
65 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

%matplotlib widget
66, while the values of the mask that corresponds to the portion of the image we want to remove are all
%matplotlib widget
65

How do we change the original image using the mask?

Larutan

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

%matplotlib widget
65. 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

print(image.shape)
print(image)
7

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

%matplotlib widget
65

print(image.shape)
print(image)
8

Then, we display the masked image

print(image.shape)
print(image)
9

The resulting masked image should look like this

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

Larutan

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

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
0

Masking a 96-well plate image (30 min)

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

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
1

Cara mencetak matriks gambar dengan python

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
10 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

Cara mencetak matriks gambar dengan python

Larutan

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
2

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

If you spent some time looking at the contents of the

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
10 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
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
12 file. Hint. use nested for loops

Larutan

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
12 file

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
3

Poin Kunci

  • We can use the NumPy

    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    14 function to create a blank, black image

  • We can draw on skimage images with functions such as

    %matplotlib widget
    
    97,
    import skimage                 # form 1, load whole skimage library
    import skimage.draw            # form 2, load skimage.draw module only
    from skimage.draw 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,
    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    00, and more

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


Creating Histograms

Ringkasan

Teaching. 40 min
Exercises. 40 min

Pertanyaan

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

Tujuan

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

Cara mencetak matriks gambar dengan python

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

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
4

Cara mencetak matriks gambar dengan python

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
%matplotlib widget
35 defines the type and depth of a pixel in the image (e. g. , an 8-bit pixel has a range of 0-255). Argumen ini diteruskan ke backend
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
_21, yang mode "L" berarti piksel 8-bit dan saluran tunggal (i. e. , grayscale).
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
21 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
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
21, you would pass this argument.
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
25); 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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
27. We will keep working with images in the value range 0 to 1 in this lesson

We now use the function

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
28 to compute the histogram of our image which, after all, is a NumPy array

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
5

The parameter

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
29 determines the number of “bins” to use for the histogram. We pass in
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
30 because we want to see the pixel count for each of the 256 possible values in the grayscale image

The parameter

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
31 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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
28 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
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
28 is an array with the bin edges and one column and 257 rows (one more than the histogram itself). There are no gaps between the bins, which means that the end of the first bin, is the start of the second and so on. For the last bin, the array also has to contain the stop, so it has one more element, than the histogram

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
34 library

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
6

We create the plot with

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
35, then label the figure and the coordinate axes with
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
36,
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
37, and
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
38 functions. The last step in the preparation of the figure is to set the limits on the values on the x-axis with the
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
39 function call

Variable-length argument lists

Note that we cannot used named parameters for the

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
40 or
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
41 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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
42. We use the left bin edges as x-positions for the histogram values by indexing the
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
43 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

Cara mencetak matriks gambar dengan python

Histograms in matplotlib

Matplotlib provides a dedicated function to compute and display histograms.

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
44. 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
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
41. Here, you could use it by calling
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
46 instead of
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
47 and
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
41 (
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
49 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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
28 function

Larutan

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
7

Your histogram of the masked area should look something like this

Cara mencetak matriks gambar dengan python

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

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
8

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

Next, we create the histogram, by calling the

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
28 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
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
52

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
9

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
53

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
40 function call

Next, we use the

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
55 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
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
55 statement

The Python built-in

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
57 function takes a list and returns an iterator of tuples, where the first element of the tuple is the index and the second element is the element of the list

Iterators, tuples, and image = iio.imread(uri="data/eight.tif") plt.imshow(image) 57

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
55 control structure. A tuple is a sequence of objects, just like a list. Namun, sebuah tuple tidak dapat diubah, dan sebuah tuple ditunjukkan dengan tanda kurung, bukan tanda kurung siku. Fungsi
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
_57 mengambil objek iterable, dan mengembalikan iterator tupel yang terdiri dari indeks berbasis 0 dan objek terkait

For example, consider this small Python program

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
0

Menjalankan program ini akan menghasilkan output berikut

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
_1

Dalam program histogram warna kami, kami menggunakan tuple,

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
61, sebagai variabel
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
55. Pertama kali melalui loop, variabel
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
_63 mengambil nilai
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
64, mengacu pada posisi saluran warna merah, dan variabel
%matplotlib widget
29 berisi string
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
66. Kedua kalinya melalui loop nilainya adalah indeks saluran hijau
%matplotlib widget
73 dan
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
68, dan ketiga kalinya mereka adalah indeks saluran biru
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
69 dan
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
70

Di dalam ________ 37 ________ 55 loop, kode Anda terlihat seperti contoh skala abu-abu. Kami menghitung histogram untuk saluran saat ini dengan

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
_72

pemanggilan fungsi, lalu tambahkan garis histogram dengan warna yang benar ke plot dengan

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
73

function call. Note the use of our loop variables,

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
63 and
%matplotlib widget
89

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

Cara mencetak matriks gambar dengan python

Colour histogram with a mask (25 min)

Kami juga dapat menerapkan topeng pada gambar yang kami terapkan proses histogram warna, dengan cara yang sama seperti yang kami lakukan untuk histogram skala abu-abu. 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

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
2

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

Larutan

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
3

Poin Kunci

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

    %matplotlib widget
    
    35 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

    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    28 function

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

  • We can display histograms using the

    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    79
    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    80,
    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    81,
    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    82,
    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    83,
    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    84,
    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    85, and
    %matplotlib widget
    
    53 functions


Blurring Images

Ringkasan

Teaching. 35 min
Exercises. 25 min

Pertanyaan

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

Tujuan

  • 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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
87 module, so we will focus on just one here, the Gaussian blur

Filters

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

Blurring

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

Kernels

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

gaussian blur

Pertimbangkan gambar kucing ini, khususnya area gambar yang digariskan oleh kotak putih

Cara mencetak matriks gambar dengan python

Sekarang, perbesar area mata kucing, seperti yang ditunjukkan pada gambar sebelah kiri di bawah. Saat kami menerapkan filter, kami mempertimbangkan setiap piksel dalam gambar, satu per satu. Dalam contoh ini, piksel yang sedang kita kerjakan disorot dengan warna merah, seperti yang ditunjukkan pada gambar sebelah kanan

Cara mencetak matriks gambar dengan python

Saat kami menerapkan filter, kami mempertimbangkan kelompok persegi panjang piksel yang mengelilingi setiap piksel dalam gambar, secara bergantian. 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. Lebar dan tinggi kernel harus ganjil, sehingga piksel yang dikerjakan selalu berada di tengahnya. Pada contoh yang ditunjukkan di atas, kernel berbentuk bujur sangkar, dengan dimensi tujuh piksel

Untuk menerapkan kernel ke piksel saat ini, rata-rata nilai warna piksel yang mengelilinginya dihitung, ditimbang dengan nilai di kernel. Dalam Gaussian blur, piksel yang paling dekat dengan pusat kernel diberi bobot lebih daripada yang jauh dari pusat. Tingkat pengurangan bobot ini ditentukan oleh fungsi Gaussian, oleh karena itu dinamai Gaussian blur

Fungsi Gaussian memetakan variabel acak ke dalam distribusi normal atau "Kurva Bell".

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

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

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

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

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

Cara mencetak matriks gambar dengan python

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

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
4

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?

Mode default adalah mengisi nilai piksel terdekat dari gambar. 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

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
5

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

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
6

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

Cara mencetak matriks gambar dengan python

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

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
7

Cara mencetak matriks gambar dengan python

Next, we apply the gaussian blur

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
8

The first two parameters to

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
88 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,
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
90. The third parameter
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
91 gives the radius of the kernel in terms of sigmas. A Gaussian function is defined from -infinity to +infinity, but our kernel (which must have a finite, smaller size) can only approximate the real function. Therefore, we must choose a certain distance from the centre of the function where we stop this approximation, and set the final size of our kernel. In the above example, we set
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
91 to 3. 5, which means the kernel size will be 2 * sigma * 3. 5. For example, for a
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
93 of 1. 0 the resulting kernel size would be 7, while for a
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
93 of 2. 0 the kernel size would be 14. The default value for
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
91 in scikit-image is 4. 0

The last parameter to

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
88 tells skimage to interpret our image, that has three dimensions, as a multichannel colour image

Finally, we display the blurred image

zero = iio.imread(uri="data/eight.tif")
zero[2,1]= 1.0
"""
The follwing line of code creates a new figure for imshow to use in displaying our output. Without it, plt.imshow() would overwrite our previous image in the cell above
"""
fig, ax = plt.subplots()
plt.imshow(zero)
print(zero)
9

Cara mencetak matriks gambar dengan python

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?

Larutan

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

Larutan

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
0

Cara mencetak matriks gambar dengan python

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. Namun, ini dapat membantu dalam keadaan tertentu e. g. ketika ada noise di gambar Anda dalam pola atau orientasi tertentu, seperti garis vertikal, atau saat Anda ingin menghilangkan noise seragam tanpa mengaburkan tepi yang ada di gambar dalam orientasi tertentu

Metode pengaburan lainnya

Gaussian blur adalah cara untuk menerapkan filter low-pass di 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 for a list of available filters

Poin Kunci

  • 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

    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    88 function

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


Thresholding

Ringkasan

Teaching. 60 min
Exercises. 50 min

Pertanyaan

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

Tujuan

  • 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

    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    99 or the operator
    print(image.shape)
    print(image)
    
    00 to threshold an image represented by a numpy array

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

    image = iio.imread(uri="data/eight.tif")
    plt.imshow(image)
    
    99 or
    print(image.shape)
    print(image)
    
    00

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

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

  • Use the

    print(image.shape)
    print(image)
    
    03 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

print(image.shape)
print(image)
04 with a series of crudely cut shapes set against a white background

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
1

Cara mencetak matriks gambar dengan python

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

print(image.shape)
print(image)
05

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

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
2

Cara mencetak matriks gambar dengan python

Next, we would like to apply the threshold

print(image.shape)
print(image)
05 such that pixels with grayscale values on one side of
print(image.shape)
print(image)
05 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
print(image.shape)
print(image)
05 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
print(image.shape)
print(image)
05 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

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
3

Cara mencetak matriks gambar dengan python

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. Jadi, kita harus memilih nilai

print(image.shape)
print(image)
_05 di suatu tempat sebelum puncak besar dan mengubah piksel di atas nilai tersebut menjadi "mati". Let us choose
print(image.shape)
print(image)
11

To apply the threshold

print(image.shape)
print(image)
05, 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
print(image.shape)
print(image)
00 to compare the
print(image.shape)
print(image)
14 to the threshold
print(image.shape)
print(image)
05. The operator returns a mask, that we capture in the variable
print(image.shape)
print(image)
16. It has only one channel, and each of its values is either 0 or 1. The binary mask created by the thresholding operation can be shown with
%matplotlib widget
38, where the
%matplotlib widget
66 entries are shown as black pixels (0-valued) and the
%matplotlib widget
65 entries are shown as white pixels (1-valued)

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
4

Cara mencetak matriks gambar dengan python

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

print(image.shape)
print(image)
20, 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
print(image.shape)
print(image)
05. 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
print(image.shape)
print(image)
05 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

print(image.shape)
print(image)
16 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

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
5

Cara mencetak matriks gambar dengan python

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

print(image.shape)
print(image)
24

Cara mencetak matriks gambar dengan python

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

print(image.shape)
print(image)
05?

Larutan

The histogram for the

print(image.shape)
print(image)
24 image can be shown with

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
6

Cara mencetak matriks gambar dengan python

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

print(image.shape)
print(image)
27 would be a good choice

Next, create a mask to turn the pixels above the threshold

print(image.shape)
print(image)
05 on and pixels below the threshold
print(image.shape)
print(image)
05 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
print(image.shape)
print(image)
00 to greater
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
99 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

Larutan

Here are the commands to create and view the binary mask

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
7

Cara mencetak matriks gambar dengan python

And here are the commands to apply the mask and view the thresholded image

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
8

Cara mencetak matriks gambar dengan python

Automatic thresholding

The downside of the simple thresholding technique is that we have to make an educated guess about the threshold

print(image.shape)
print(image)
05 by inspecting the histogram. Ada juga metode automatic thresholding yang dapat menentukan threshold secara otomatis untuk kita. Salah satu metode tersebut adalah metode Otsu. Ini sangat berguna untuk situasi di mana histogram skala abu-abu dari suatu gambar memiliki dua puncak yang sesuai dengan latar belakang dan objek yang diinginkan.

Denoising an image before thresholding

Dalam praktiknya, sering kali diperlukan denoise gambar sebelum melakukan thresholding, yang dapat dilakukan dengan salah satu metode dari episode Blurring Images

Consider the image

print(image.shape)
print(image)
33 of a maize root system which we have seen before in the Image Representation in skimage episode

[[0. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
9

Cara mencetak matriks gambar dengan python

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

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
0

Cara mencetak matriks gambar dengan python

The histogram has a significant peak around 0. 2, and a second, smaller peak very near 1. 0. Dengan demikian, citra ini merupakan kandidat yang baik untuk dilakukan thresholding dengan metode Otsu. The mathematical details of how this works are complicated (see 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

print(image.shape)
print(image)
34 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
print(image.shape)
print(image)
05 with Otsu’s method

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
1

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
2

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
99. As we have seen before, pixels above the threshold value will be turned on, those below the threshold will be turned off

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
3

Cara mencetak matriks gambar dengan python

Finally, we use the mask to select the foreground

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
4

Cara mencetak matriks gambar dengan python

Application. measuring root mass

Mari kita beralih ke aplikasi di mana kita dapat menerapkan ambang batas dan teknik lain yang telah kita pelajari hingga saat ini. Consider these four maize root system images, which you can find in the files

print(image.shape)
print(image)
37,
print(image.shape)
print(image)
38,
print(image.shape)
print(image)
39, and
print(image.shape)
print(image)
40

Cara mencetak matriks gambar dengan python

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

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
5

The function begins with reading the original image from the file

print(image.shape)
print(image)
41. 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
%matplotlib widget
35 to automatically convert it to grayscale. Next, the grayscale image is blurred with a Gaussian filter with the value of
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
93 that is passed to the function. Then we determine the threshold
print(image.shape)
print(image)
05 with Otsu’s method and create a binary mask just as we did in the previous section. Sampai saat ini, semuanya harus familiar

The final part of the function determines the root mass ratio in the image. Recall that in the

print(image.shape)
print(image)
16, 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
print(image.shape)
print(image)
47. Then we determine the width and height of the image by using the elements of
print(image.shape)
print(image)
48 (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
print(image.shape)
print(image)
49 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

print(image.shape)
print(image)
37 and a sigma value of 1. 5, we would call the function like this

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
6

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
7

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

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
8

five = iio.imread(uri="data/eight.tif")
five[1,2]= 1.0
five[3,0]= 1.0
fig, ax = plt.subplots()
plt.imshow(five)
print(five)
9

Ignoring more of the images – brainstorming (10 min)

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

print(image.shape)
print(image)
51 function

Cara mencetak matriks gambar dengan python

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

Larutan

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

print(image.shape)
print(image)
37 shown above again. Notice the peak near 1. 0? Recall that a grayscale value of 1. 0 corresponds to white pixels. puncak sesuai dengan label putih dan lingkaran. 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)

Terapkan versi yang disempurnakan dari fungsi

print(image.shape)
print(image)
_51 yang menerapkan ambang batas biner sederhana untuk menghilangkan lingkaran putih dan label dari gambar sebelum menerapkan metode Otsu

Larutan

We can apply a simple binary thresholding with a threshold

print(image.shape)
print(image)
54 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)

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
0

Output dari program yang ditingkatkan menggambarkan bahwa lingkaran putih dan label memiringkan rasio massa akar kami

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
1

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

Cara mencetak matriks gambar dengan python

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
print(image.shape)
print(image)
56

Cara mencetak matriks gambar dengan python

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”

Larutan

Here is the code to create the grayscale histogram

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
2

Cara mencetak matriks gambar dengan python

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. The small peak near zero is what we are after. the dark bacteria colonies. A reasonable choice thus might be to leave pixels below

print(image.shape)
print(image)
57 on

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

print(image.shape)
print(image)
00 operator with a threshold
print(image.shape)
print(image)
57

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
3

Cara mencetak matriks gambar dengan python

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

Poin Kunci

  • 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

Ringkasan

Teaching. 70 min
Exercises. 55 min

Pertanyaan

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

Tujuan

  • 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

Cara mencetak matriks gambar dengan python

In thresholding we went from the original image to this version

Cara mencetak matriks gambar dengan python

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

%matplotlib widget
65 while the background pixels are
%matplotlib widget
66

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

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
64 is used to represent background pixels, and the letter
print(image.shape)
print(image)
63 is used to represent object pixels foreground)

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
4

The pixels are organised in a rectangular grid. 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. Diagonal jumps are not allowed. So, from a centre pixel, denoted with

print(image.shape)
print(image)
64, only the pixels indicated with a
%matplotlib widget
73 are reachable

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
5

Piksel pada diagonal (dari

print(image.shape)
print(image)
64) tidak dapat dijangkau dengan satu lompatan, yang dilambangkan dengan
print(image.shape)
print(image)
67. The pixels reachable with a single jump form the 1-jump neighborhood

The second rule states that in a sequence of jumps, one may only jump in row and column direction once -> they have to be orthogonal. An example of a sequence of orthogonal jumps is shown below. Starting from

print(image.shape)
print(image)
64 the first jump goes along the row to the right. The second jump then goes along the column direction up. After this, the sequence cannot be continued as a jump has already been made in both row and column direction

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
6

All pixels reachable with one, or two jumps form the 2-jump neighborhood. The grid below illustrates the pixels reachable from the centre pixel

print(image.shape)
print(image)
64 with a single jump, highlighted with a
%matplotlib widget
73, and the pixels reachable with 2 jumps with a
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
69

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
7

We want to revisit our example image mask from above and apply the two different neighborhood rules. With a single jump connectivity for each pixel, we get two resulting objects, highlighted in the image with

print(image.shape)
print(image)
72’s and
print(image.shape)
print(image)
73’s

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
8

In the 1-jump version, only pixels that have direct neighbors along rows or columns are considered connected. Diagonal connections are not included in the 1-jump neighborhood. With two jumps, however, we only get a single object

print(image.shape)
print(image)
72 because pixels are also considered connected along the diagonals

[[0. 0. 0.]
 [0. 1. 1.]
 [0. 0. 0.]
 [1. 1. 0.]
 [0. 0. 0.]]
9

Object counting (optional, not included in timing)

How many objects with 1 orthogonal jump, how many with 2 orthogonal jumps?

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

a) 1 b) 5 c) 2

Larutan

b) 5

2 jumps

a) 2 b) 3 c) 5

Larutan

a) 2

Jumps and neighborhoods

Kami baru saja memperkenalkan bagaimana Anda dapat menjangkau piksel tetangga yang berbeda dengan melakukan satu atau lebih lompatan ortogonal. 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. Ketika dua lompatan ortogonal diperbolehkan, delapan piksel dapat dicapai, sehingga lingkungan 2 lompatan sesuai dengan lingkungan 8 lingkungan

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). Operasi ini mengambil citra biner sebagai masukan. Usually, the

%matplotlib widget
66 value in this image is associated with background pixels, and the
%matplotlib widget
65 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
print(image.shape)
print(image)
77. Let us add this function to the already familiar steps of thresholding an image. Di sini kita mendefinisikan fungsi Python yang dapat digunakan kembali ________39______78

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

print(image.shape)
print(image)
79 in order to use the
print(image.shape)
print(image)
80 function that performs the CCA. The first four lines of code are familiar from the Thresholding episode

Then we call the

print(image.shape)
print(image)
80 function. This function has one positional argument where we pass the
print(image.shape)
print(image)
16, i. e. , citra biner yang akan dikerjakan. Dengan argumen opsional
print(image.shape)
print(image)
_83, kami menentukan lingkungan dalam satuan lompatan ortogonal. Misalnya, dengan menyetel
print(image.shape)
print(image)
_84 kita akan mempertimbangkan lingkungan 2 lompatan yang diperkenalkan di atas. Fungsi mengembalikan
print(image.shape)
print(image)
_85 di mana setiap piksel memiliki nilai unik yang sesuai dengan objek miliknya. Selain itu, kami meneruskan parameter opsional
print(image.shape)
print(image)
86 untuk mengembalikan indeks label maksimum sebagai
print(image.shape)
print(image)
87

Parameter opsional dan mengembalikan nilai

Parameter opsional

print(image.shape)
print(image)
88 mengubah tipe data yang dikembalikan oleh fungsi
print(image.shape)
print(image)
80. The number of labels is only returned if
print(image.shape)
print(image)
88 is True. Jika tidak, fungsi hanya mengembalikan gambar berlabel. Ini berarti bahwa kita harus memperhatikan saat menugaskan nilai kembalian ke sebuah variabel. Jika kita menghilangkan parameter opsional
print(image.shape)
print(image)
_88 atau meneruskan
print(image.shape)
print(image)
92, kita dapat memanggil fungsi tersebut sebagai

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

Jika kita melewati

print(image.shape)
print(image)
86, fungsi mengembalikan tuple dan kita dapat menetapkannya sebagai

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

Jika kita menggunakan penugasan yang sama seperti pada kasus pertama, variabel

print(image.shape)
print(image)
85 akan menjadi tuple, di mana
print(image.shape)
print(image)
95 adalah gambar dan
print(image.shape)
print(image)
96 adalah jumlah label. Hal ini dapat menimbulkan kebingungan jika kita berasumsi bahwa
print(image.shape)
print(image)
85 hanya berisi gambar dan menyebarkannya ke fungsi lain. Jika Anda mendapatkan
print(image.shape)
print(image)
_98 atau serupa, periksa apakah Anda telah menetapkan nilai pengembalian secara konsisten dengan parameter opsional

Kita dapat memanggil fungsi di atas

print(image.shape)
print(image)
_78 dan menampilkan gambar berlabel seperti itu

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

Pemetaan warna

Di sini Anda mungkin mendapatkan peringatan

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_00 atau hanya melihat gambar hitam semua (Catatan. perilaku ini mungkin berubah di versi mendatang atau tidak terjadi dengan penampil gambar yang berbeda)

Apa yang salah? . You can see that some pixels have values different from

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
64, so they are not actually pure black. Mari cari tahu lebih lanjut dengan memeriksa ________39______85. Properti yang mungkin menarik dalam konteks ini adalah
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
03, nilai minimum dan maksimum. Kita dapat mencetaknya dengan baris berikut

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

Meneliti hasilnya dapat memberi kita petunjuk mengapa gambar tampak hitam

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

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_03 dari
print(image.shape)
print(image)
85 adalah
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
06. Artinya, nilai dalam gambar ini berkisar dari
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
07 hingga
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
08. Itu angka yang sangat besar. Dari ruang yang tersedia ini kami hanya menggunakan rentang dari
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
64 hingga
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
10. Saat menampilkan gambar ini di penampil, ini meremas rentang lengkap menjadi 256 nilai abu-abu. Oleh karena itu, kisaran angka kami tidak menghasilkan perubahan yang terlihat

Untungnya, perpustakaan skimage memiliki alat untuk mengatasi situasi ini

Kita dapat menggunakan fungsi

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_11 untuk mengonversi warna pada gambar (ingat bahwa kita telah menggunakan fungsi
%matplotlib widget
27 untuk mengonversi ke skala abu-abu). Dengan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_11, semua objek diwarnai sesuai dengan daftar warna yang dapat disesuaikan. 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

Cara mencetak matriks gambar dengan python

Berapa banyak objek dalam gambar tersebut (15 menit)

Sekarang, giliran Anda untuk berlatih. Menggunakan fungsi

print(image.shape)
print(image)
_78, temukan dua cara untuk mencetak jumlah objek yang ditemukan pada gambar

Berapa jumlah objek yang Anda harapkan?

Bagaimana mengubah nilai

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
93 dan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
16 memengaruhi hasil?

Larutan

Seperti yang sudah Anda duga, nilai pengembalian

print(image.shape)
print(image)
87 sudah berisi jumlah gambar yang ditemukan. Jadi itu bisa dicetak dengan mudah

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

Namun ada juga cara untuk mendapatkan jumlah objek yang ditemukan dari gambar berlabel itu sendiri. Ingatlah bahwa semua piksel milik satu objek diberi nilai integer yang sama. Algoritma komponen terhubung menghasilkan angka berurutan. Latar belakang mendapatkan nilai

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
_64, objek pertama mendapatkan nilai
%matplotlib widget
73, objek kedua mendapatkan nilai
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
69, dan seterusnya. Artinya dengan mencari objek dengan nilai maksimum, kita juga mengetahui berapa banyak objek yang ada di dalam citra. Dengan demikian kita dapat menggunakan fungsi
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_21 dari Numpy untuk menemukan nilai maksimum yang sama dengan jumlah objek yang ditemukan

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

Invoking the function with

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
22, and
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
23, both methods will print

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

Menurunkan ambang batas akan menghasilkan objek yang lebih sedikit. Semakin tinggi ambang batas yang ditetapkan, semakin banyak objek yang ditemukan. Semakin banyak kebisingan latar belakang diambil sebagai objek. Sigma yang lebih besar menghasilkan topeng biner dengan lebih sedikit noise dan karenanya jumlah objek yang lebih sedikit. Menetapkan sigma terlalu tinggi menimbulkan bahaya penggabungan objek

Anda mungkin bertanya-tanya mengapa analisis komponen yang terhubung dengan

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
22, dan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
23 menemukan 11 objek, padahal kita hanya mengharapkan 7 objek. Di manakah keempat objek tambahan itu?

Cara mencetak matriks gambar dengan python

Bagi kami jelas bahwa bintik-bintik kecil ini adalah artefak dan bukan objek yang kami minati. Tapi bagaimana kita bisa memberi tahu komputer? . Dalam beberapa kasus, kebisingan latar belakang diambil sebagai objek. Dan dengan parameter lain, beberapa objek di latar depan bisa pecah atau hilang sama sekali. Oleh karena itu, diperlukan kriteria lain untuk mendeskripsikan properti yang diinginkan dari objek yang ditemukan

Morfometrik - Jelaskan fitur objek dengan angka

Morfometri berkaitan dengan analisis kuantitatif objek dan mempertimbangkan sifat-sifat seperti ukuran dan bentuk. Untuk contoh gambar dengan bentuk, intuisi kita memberi tahu kita bahwa objek harus berukuran atau luas tertentu. Jadi kita bisa menggunakan area minimum sebagai kriteria kapan suatu objek harus dideteksi. Untuk menerapkan kriteria tersebut, diperlukan suatu cara untuk menghitung luas objek yang ditemukan oleh komponen yang terhubung. Recall how we determined the root mass in the Thresholding episode by counting the pixels in the binary mask. But here we want to calculate the area of several objects in the labeled image. Pustaka skimage menyediakan fungsi

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
28 untuk mengukur properti wilayah berlabel. It returns a list of
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
29 that describe each connected region in the images. Properti dapat diakses menggunakan atribut tipe data ________40______29. Di sini kita akan menggunakan properti
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_31 dan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
32. Anda dapat menjelajahi dokumentasi skimage untuk mempelajari tentang properti lain yang tersedia

Kita bisa mendapatkan daftar area objek berlabel sebagai berikut

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

Ini akan menghasilkan 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
_12

Plot histogram distribusi area objek (10 mnt)

Serupa dengan cara kami menentukan ambang batas "baik" dalam episode Thresholding, seringkali berguna untuk memeriksa histogram properti objek. Misalnya, kami ingin melihat distribusi area objek

  1. Buat dan periksa histogram dari area objek yang diperoleh dengan
    (5, 3)
    [[0. 0. 0.]
     [0. 1. 0.]
     [0. 0. 0.]
     [0. 1. 0.]
     [0. 0. 0.]]
    
    28
  2. Apa histogram memberitahu Anda tentang objek?

Larutan

Histogram dapat diplot dengan

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

Cara mencetak matriks gambar dengan python

Histogram menunjukkan jumlah objek (sumbu vertikal) yang luasnya berada dalam rentang tertentu (sumbu horizontal). Ketinggian batang pada histogram menunjukkan prevalensi objek dengan luas tertentu. Seluruh histogram memberi tahu kita tentang distribusi ukuran objek dalam gambar. Seringkali dimungkinkan untuk mengidentifikasi celah antara kelompok batang (atau puncak jika kita menggambar histogram sebagai kurva kontinu) yang memberi tahu kita tentang kelompok tertentu dalam gambar.

In this example, we can see that there are four small objects that contain less than 50000 pixels. Lalu ada sekelompok empat (1+1+2) objek dalam kisaran antara 200000 dan 400000, dan tiga objek dengan ukuran sekitar 500000. Untuk jumlah objek kami, kami mungkin ingin mengabaikan objek kecil sebagai artefak, mis. e, kami ingin mengabaikan bilah histogram paling kiri. Kita bisa menggunakan ambang 50000 sebagai area minimum untuk dihitung. Bahkan, daftar

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_34 sudah memberi tahu kita bahwa ada kurang dari 200 piksel dalam objek ini. Oleh karena itu, masuk akal untuk memerlukan area minimal minimal 200 piksel untuk objek yang terdeteksi. Dalam praktiknya, menemukan ambang batas yang “tepat” bisa rumit dan biasanya melibatkan tebakan berdasarkan pengetahuan domain

Filter objek berdasarkan area (10 mnt)

Sekarang kami ingin menggunakan kriteria area minimum untuk mendapatkan jumlah objek yang lebih akurat dalam gambar

  1. Temukan cara untuk menghitung jumlah objek dengan hanya menghitung objek di atas area tertentu

Larutan

Salah satu cara untuk menghitung hanya objek di atas area tertentu adalah dengan terlebih dahulu membuat daftar objek tersebut, lalu mengambil panjang daftar tersebut sebagai objek yang dihitung. Hal ini dapat dilakukan sebagai berikut

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

Pilihan lainnya adalah menggunakan array Numpy untuk membuat daftar objek besar. Pertama-tama kita membuat larik

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_34 berisi area objek, dan larik
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
36 berisi label objek. Label objek juga dikembalikan oleh
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
28. Kita telah melihat bahwa kita dapat membuat array boolean menggunakan operator pembanding. Di sini kita dapat menggunakan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_38 untuk menghasilkan array yang memiliki dimensi yang sama dengan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
36. It can then used to select the labels of objects whose area is greater than
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
40 by indexing

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

Keuntungan menggunakan array Numpy adalah

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
55 loop dan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
42 pernyataan dalam Python bisa lambat, dan dalam praktiknya pendekatan pertama mungkin tidak dapat dilakukan jika gambar berisi sejumlah besar objek. Dalam hal ini, fungsi array Numpy ternyata sangat berguna karena jauh lebih cepat

Dalam contoh ini, kita juga dapat menggunakan fungsi

print(image.shape)
print(image)
47 yang telah kita lihat sebelumnya bersama dengan operator
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
99 untuk menghitung objek yang luasnya di atas
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
40

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

Untuk ketiga alternatif, outputnya sama dan memberikan jumlah yang diharapkan dari 7 objek

Menggunakan fungsi dari Numpy dan paket Python lainnya

Fungsi dari paket Python seperti Numpy seringkali lebih efisien dan membutuhkan lebih sedikit kode untuk ditulis. Sebaiknya telusuri halaman referensi

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 dan
import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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 untuk mencari fungsi yang tersedia yang dapat menyelesaikan tugas yang diberikan

Hapus objek kecil (20 mnt)

Kita mungkin juga ingin mengecualikan (menutupi) objek kecil saat memplot gambar berlabel

  1. Tingkatkan fungsi
    print(image.shape)
    print(image)
    
    78 sedemikian rupa sehingga secara otomatis menghapus objek yang berada di bawah area tertentu yang diteruskan ke fungsi sebagai parameter opsional

Larutan

Untuk menghapus objek kecil dari gambar berlabel, kami mengubah nilai semua piksel milik objek kecil ke label latar belakang 0. Salah satu cara untuk melakukannya adalah dengan mengulang semua objek dan menyetel piksel yang cocok dengan label objek ke 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

Di sini fungsi Numpy juga dapat digunakan untuk menghilangkan

image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
55 loop dan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
42 pernyataan. Seperti di atas, kita dapat membuat array dari label objek kecil dengan perbandingan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
51. Kita dapat menggunakan fungsi Numpy lainnya,
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
52, untuk mengatur piksel semua objek kecil menjadi 0.
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_52 mengambil dua larik dan mengembalikan larik boolean dengan nilai
%matplotlib widget
65 jika entri larik pertama ditemukan di larik kedua, dan
%matplotlib widget
66 sebaliknya. This array can then be used to index the
print(image.shape)
print(image)
85 and set the entries that belong to small objects to
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
64

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

Cara yang lebih elegan untuk menghapus objek kecil dari gambar adalah dengan memanfaatkan modul

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
58. Ini menyediakan fungsi
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_59 yang melakukan persis seperti yang kita cari. Itu dapat diterapkan pada gambar biner dan mengembalikan topeng di mana semua objek yang lebih kecil dari
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
40 dikecualikan, i. e, nilai pikselnya disetel ke
%matplotlib widget
66. Kami kemudian dapat menerapkan
print(image.shape)
print(image)
80 ke gambar bertopeng

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

Menggunakan fitur

import skimage                 # form 1, load whole skimage library
import skimage.draw            # form 2, load skimage.draw module only
from skimage.draw 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, kita dapat mengimplementasikan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
64 sebagai berikut

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

Kita sekarang dapat memanggil fungsi dengan

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
40 yang dipilih dan menampilkan gambar berlabel yang dihasilkan

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

Cara mencetak matriks gambar dengan 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
_22

Perhatikan bahwa benda-benda kecil "hilang" dan kami memperoleh jumlah yang benar dari 7 benda dalam gambar

Warnai objek berdasarkan area (opsional, tidak termasuk dalam pengaturan waktu)

Terakhir, kami ingin menampilkan gambar dengan objek yang diwarnai sesuai dengan luasnya. Dalam praktiknya, ini dapat digunakan dengan properti lain untuk memberikan isyarat visual dari properti objek

Larutan

Kita sudah mengetahui cara mendapatkan luas objek dari

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
66. Kita hanya perlu memasukkan nilai area nol untuk latar belakang (untuk mewarnainya seperti objek ukuran nol). Latar belakang juga diberi label
image = iio.imread(uri="data/eight.tif")
plt.imshow(image)
64 di
print(image.shape)
print(image)
85, jadi kami memasukkan nilai area nol di depan elemen pertama
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
34 dengan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
70. Kemudian kita dapat membuat
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_71 di mana kita menetapkan setiap piksel nilai area dengan mengindeks
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
34 dengan nilai label di
print(image.shape)
print(image)
85

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

Cara mencetak matriks gambar dengan python

Anda mungkin telah memperhatikan bahwa dalam solusinya, kami telah menggunakan

print(image.shape)
print(image)
85 untuk mengindeks array
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
34. Ini adalah contoh dari Hasilnya adalah array dengan bentuk yang sama dengan
print(image.shape)
print(image)
85 yang nilai pikselnya dipilih dari
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
34 sesuai dengan label objek. Karenanya objek akan diwarnai berdasarkan area saat hasilnya ditampilkan. Perhatikan bahwa pengindeksan lanjutan dengan larik bilangan bulat bekerja sedikit berbeda dari pengindeksan dengan larik Boolean yang telah kita gunakan untuk menutupi. Sementara pengindeksan array Boolean hanya mengembalikan entri yang sesuai dengan
%matplotlib widget
65 nilai indeks, pengindeksan array integer mengembalikan array dengan bentuk yang sama dengan indeks. Anda dapat membaca lebih lanjut tentang pengindeksan lanjutan di

Poin Kunci

  • Kita dapat menggunakan

    print(image.shape)
    print(image)
    
    _80 untuk menemukan dan melabeli objek yang terhubung dalam sebuah gambar

  • Kita dapat menggunakan

    (5, 3)
    [[0. 0. 0.]
     [0. 1. 0.]
     [0. 0. 0.]
     [0. 1. 0.]
     [0. 0. 0.]]
    
    _28 untuk mengukur properti objek berlabel

  • Kita dapat menggunakan

    (5, 3)
    [[0. 0. 0.]
     [0. 1. 0.]
     [0. 0. 0.]
     [0. 1. 0.]
     [0. 0. 0.]]
    
    _59 untuk menutupi objek kecil dan menghapus artefak dari suatu gambar

  • Kami dapat menampilkan gambar berlabel untuk melihat objek yang diwarnai oleh label


Tantangan Batu Capung

Ringkasan

Pengajaran. 10 menit
Exercises. 40 min

Pertanyaan

  • Bagaimana cara menghitung koloni bakteri secara otomatis dengan analisis gambar?

Tujuan

  • Kumpulkan semua yang telah Anda pelajari sejauh ini untuk menghitung koloni bakteri dalam 3 gambar

Dalam episode ini, kami akan memberikan tantangan terakhir untuk Anda coba, berdasarkan semua keterampilan yang telah Anda peroleh sejauh ini. This challenge will be related to the shape of objects in images (morphometrics)

Morfometrik. Penghitungan Koloni Bakteri

Seperti disebutkan dalam pengenalan lokakarya, tantangan morfometrik Anda adalah menentukan berapa banyak koloni bakteri di setiap gambar ini

Cara mencetak matriks gambar dengan python

Cara mencetak matriks gambar dengan python

Cara mencetak matriks gambar dengan python

File gambar dapat ditemukan di

(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
_82,
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
83, dan
(5, 3)
[[0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]
84

Morfometri untuk koloni bakteri

Tulis program Python yang menggunakan skimage untuk menghitung jumlah koloni bakteri di setiap gambar, dan untuk setiap gambar, buat gambar baru yang menyorot koloni. Gambar harus terlihat mirip dengan yang ini

Cara mencetak matriks gambar dengan python

Selain itu, cetak jumlah koloni untuk setiap gambar

Gunakan apa yang telah Anda pelajari tentang histogram, thresholding, dan analisis komponen terhubung. Cobalah untuk memasukkan kode Anda ke dalam fungsi yang dapat digunakan kembali, sehingga dapat diterapkan dengan mudah ke file gambar apa pun

Larutan

Pertama, mari kita kerjakan proses untuk satu gambar

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

Cara mencetak matriks gambar dengan python

Selanjutnya, kita perlu membatasi gambar untuk membuat topeng yang hanya menutupi koloni bakteri gelap. Ini lebih mudah menggunakan gambar skala abu-abu, jadi kami mengonversinya di sini

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

Cara mencetak matriks gambar dengan python

Selanjutnya, kami memburamkan gambar dan membuat 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
_26

Cara mencetak matriks gambar dengan python

Dalam histogram ini, kita melihat tiga puncak - yang kiri (i. e. piksel tergelap) adalah koloni kita, puncak tengahnya adalah media kultur kuning/coklat di cawan, dan yang kanan (i. e. piksel paling terang) adalah latar belakang gambar putih. Therefore, we choose a threshold that selects the small left peak

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

Cara mencetak matriks gambar dengan python

Topeng ini menunjukkan kepada kita di mana koloni berada dalam gambar - tetapi bagaimana kita bisa menghitung berapa jumlahnya?

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

Terakhir, kami membuat gambar ringkasan koloni berwarna di atas gambar skala abu-abu

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

Cara mencetak matriks gambar dengan python

Sekarang kita telah menyelesaikan tugas untuk satu gambar, kita perlu mengulanginya untuk dua gambar yang tersisa. Ini adalah poin yang bagus untuk mengumpulkan baris di atas menjadi fungsi yang dapat digunakan kembali

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

Sekarang kita dapat dengan mudah melakukan analisis ini pada semua gambar melalui for loop

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

Cara mencetak matriks gambar dengan python
Cara mencetak matriks gambar dengan python
Cara mencetak matriks gambar dengan python

Anda akan melihat bahwa untuk gambar dengan lebih banyak koloni, hasilnya tidak sempurna. Misalnya, beberapa koloni kecil hilang, dan kemungkinan ada beberapa bintik hitam kecil yang salah diberi label sebagai koloni. Anda dapat memperluas solusi ini untuk, misalnya, menggunakan ambang batas yang ditentukan secara otomatis untuk setiap gambar, yang mungkin lebih cocok untuk setiap gambar. Selain itu, Anda dapat memfilter koloni di bawah ukuran tertentu (seperti yang kami lakukan di episode Analisis Komponen Terhubung). Anda juga akan melihat bahwa beberapa koloni yang bersentuhan digabungkan menjadi satu koloni besar. Hal ini dapat diperbaiki dengan metode segmentasi yang lebih rumit (di luar cakupan pelajaran ini) seperti DAS

Poin Kunci

  • Dengan menggunakan thresholding, analisis komponen yang terhubung, dan alat lainnya, kami dapat secara otomatis mengelompokkan gambar koloni bakteri

    Bagaimana cara mencetak matriks gambar dengan Python?

    Bagaimana Anda mengubah gambar menjadi matriks dengan Python? .
    impor matplotlib. gambar sebagai gambar
    img=gambar. imread('nama_gambar. png')
    print('Bentuk gambar adalah. ', img. shape)
    print('Gambar sebagai array adalah. ')
    cetak(img)

    Bagaimana Anda memplot array gambar dengan Python?

    Skema Interpolasi Larik .
    dari PIL impor Gambar img = Gambar. membuka('. /. /doc/_static/sinkbug. png') img. thumbnail((64, 64)) # mengubah ukuran gambar di tempat imglot = plt. imshow(img)
    imgplot = plt. imshow(img, interpolasi="terdekat")
    imgplot = plt. imshow(img, interpolasi="bicubic")

    Bagaimana gambar diubah menjadi matriks?

    Gambar skala abu-abu digital disajikan di komputer dengan matriks piksel. Setiap piksel dari gambar tersebut disajikan oleh satu elemen matriks – bilangan bulat dari himpunan . Nilai numerik dalam presentasi piksel diubah secara seragam dari nol (piksel hitam) menjadi 255 (piksel putih).

    Bagaimana Anda merepresentasikan gambar dengan representasi matriks?

    Gambar adalah larik, atau matriks, dari piksel persegi (elemen gambar) yang diatur dalam kolom dan baris . Gambar 1. Gambar — larik atau matriks piksel yang disusun dalam kolom dan baris. Dalam gambar skala abu-abu (8-bit) setiap elemen gambar memiliki intensitas yang ditetapkan mulai dari 0 hingga 255.