OpenCV giúp dễ dàng đọc một hình ảnh vào bộ nhớ và biểu diễn nó dưới dạng một mảng Numpy. Có một mảng Numpy giúp dễ dàng thao tác với hình ảnh dưới dạng các phép biến đổi ma trận toán học khác nhau Làm cách nào để lưu hình ảnh vào đĩa và làm cách nào để tạo một đối tượng nhị phân của nó trong bộ nhớ Đọc hình ảnh thành một mảng NumpyTrong ví dụ này, chúng tôi lấy một tệp hình ảnh và tải nó vào bộ nhớ bằng imread ví dụ/trăn/opencv_read_image. py import cv2 as cv
import sys
if len(sys.argv) != 2:
exit(f"Usage: {sys.argv[0]} FILENAME")
filename = sys.argv[1]
img = cv.imread(filename)
print(type(img)) # numpy.array
Đọc/Ghi hình ảnh và chuyển đổi sang nhị phânỞ đây chúng tôi đọc hình ảnh từ một tệp thành một mảng có nhiều mảng bằng cách sử dụng OpenCV imread Sau đó, chúng tôi thực hiện một số thao tác đơn giản, vẽ một hình chữ nhật ở giữa. Chúng tôi chỉ sử dụng thực tế rằng nó là một mảng Numpy khi trích xuất hình dạng của hình ảnh. Chúng tôi có thể đã thực hiện các thao tác khác không có triển khai trong OpenCV Sau đó, chúng tôi lưu hình ảnh dưới dạng tệp khác bằng cách sử dụng imwrite ví dụ/trăn/opencv_read_write_image. py import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
Ngoài ra, chúng tôi có thể đã chuyển đổi hình ảnh thành một tập hợp byte trong bộ nhớ bằng cách sử dụng imencode của OpenCV và BytesIO của gói io Đôi khi, bạn có thể cần chuyển đổi hình ảnh thành hình ảnh nhị phân. Nói cách khác, bạn cần chuyển đổi ảnh màu hoặc ảnh thang độ xám sang ảnh đen trắng Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách chuyển đổi một hình ảnh từ màu sang đen trắng Các bước để chuyển đổi hình ảnh màu thành hình ảnh nhị phânChuyển đổi hình ảnh thành đen trắng bao gồm hai bước - Đọc hình ảnh nguồn dưới dạng hình ảnh thang độ xám
- Chuyển đổi hình ảnh thang độ xám thành nhị phân với ngưỡng bạn chọn
Nếu hình ảnh nguồn của bạn là hình ảnh có tỷ lệ xám, thì bạn có thể đọc hình ảnh ở bước #1 dưới dạng hình ảnh gốc và tiếp tục với bước #2. Ví dụ sau minh họa hoạt động của ngưỡng khi chuyển đổi từ thang màu xám sang nhị phân hoặc đen trắng ví dụ 1. Chuyển ảnh màu sang đen trắngTrong ví dụ sau, chúng ta sẽ đọc ảnh màu sau bằng cách sử dụng cv2. imread() dưới dạng hình ảnh thang độ xám và sau đó áp dụng cv2. hàm ngưỡng () trên mảng hình ảnh Hình ảnh đầu vào Chương trình Python import cv2
#read image
img_grey = cv2.imread('D:/original.png', cv2.IMREAD_GRAYSCALE)
# define a threshold, 128 is the middle of black and white in grey scale
thresh = 128
# threshold the image
img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]
#save image
cv2.imwrite('D:/black-and-white.png',img_binary)
Đầu ra đen trắng. png như hình bên dưới Đầu ra hình ảnh nhị phân ví dụ 2. Chuyển đổi hình ảnh thang độ xám thành đen trắngTrong ví dụ sau, chúng ta sẽ đọc một hình ảnh thang độ xám bằng cách sử dụng cv2. imread() và sau đó áp dụng cv2. hàm ngưỡng () trên mảng hình ảnh. Không có sự khác biệt trong việc chuyển đổi một hình ảnh màu thành đen trắng và hình ảnh tỷ lệ xám thành đen trắng Chương trình Python import cv2
#read image as grey scale
img_grey = cv2.imread('D:/greyscale.png', cv2.IMREAD_GRAYSCALE)
# define a threshold, 128 is the middle of black and white in grey scale
thresh = 128
# threshold the image
img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]
#save image
cv2.imwrite('D:/black-and-white.png',img_binary)
Tóm lượcTrong hướng dẫn Ví dụ về Python này, chúng ta đã học cách chuyển đổi ảnh màu thành ảnh nhị phân hay nói cách khác là ảnh đen trắng Trong học máy, Python sử dụng dữ liệu hình ảnh ở dạng mảng NumPy, tôi. e. , định dạng [Chiều cao, Chiều rộng, Kênh]. Để nâng cao hiệu suất của mô hình dự đoán, chúng ta phải biết cách tải và thao tác với hình ảnh. Trong Python, chúng ta có thể thực hiện một tác vụ theo nhiều cách khác nhau. Chúng tôi có các tùy chọn từ Numpy đến Pytorch và CUDA, tùy thuộc vào mức độ phức tạp của vấn đề Đến cuối hướng dẫn này, bạn sẽ có kinh nghiệm thực hành với - Tải và hiển thị hình ảnh bằng API Matplotlib, OpenCV và Keras
- Chuyển đổi hình ảnh đã tải thành mảng NumPy và ngược lại
- Tiến hành thao tác cơ bản với hình ảnh bằng thư viện Gối và NumPy và lưu nó vào hệ thống cục bộ của bạn
- Đọc hình ảnh dưới dạng mảng trong Keras API và OpenCV
thư viện gốiGối là một công cụ thao tác hình ảnh ưa thích. Python phiên bản 2 đã sử dụng Python Image Library (PIL) và Python phiên bản 3 sử dụng Pillow Python Library, một bản nâng cấp của PIL Trước tiên, bạn nên tạo một môi trường ảo trong Anaconda cho các dự án khác nhau. Đảm bảo rằng bạn có các gói hỗ trợ như NumPy, SciPy và Matplotlib để cài đặt trong môi trường ảo mà bạn tạo Sau khi thiết lập các gói, bạn có thể dễ dàng cài đặt Gối bằng cách sử dụng ______48 . 1pip install Pillow
vỏ bọc Bạn có thể xác nhận rằng thư viện đã được cài đặt đúng cách bằng cách kiểm tra phiên bản của nó 1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
con trăn 1Pillow Version: 7.0.0
Tuyệt quá. Phiên bản mới nhất hiện đã được tải xuống. Hãy chuyển sang bước tiếp theo Đang tải hình ảnhSau đây chúng ta sẽ tìm hiểu hai cách để tải và lấy thông tin chi tiết của một hình ảnh. sử dụng thư viện Gối và sử dụng thư viện Matplotlib Phương pháp 1. thư viện gốiChọn hình ảnh thử nghiệm để tải và làm việc với thư viện Gối (PIL). Hình ảnh có thể là PNG hoặc JPEG. Trong ví dụ này, chúng tôi sẽ sử dụng một hình ảnh có tên kolala. jpeg. Tải lên hình ảnh trong thư mục làm việc hoặc cung cấp đường dẫn mong muốn của bạn. Lớp 1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__) 9 là trung tâm của 1Pillow Version: 7.0.0 0 và các thuộc tính của nó giúp thao tác . Lớp 1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__) 9 sử dụng các hàm này. * 1Pillow Version: 7.0.0 2 . Điều này có thể trực tiếp tải hình ảnh. Nó có 1Pillow Version: 7.0.0 3 các thuộc tính như 1Pillow Version: 7.0.0 4 , cung cấp thông tin về tệp kỹ thuật số . g. , 480x240). 1Pillow Version: 7.0.0 5size`, which displays the dimensions of the image in pixels (e.g., 480x240).
1Pillow Version: 7.0.0 6 . Điều này sẽ hiển thị hình ảnh. Ứng dụng xem trước ảnh mặc định của bạn sẽ bật lên.
import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
6con trăn import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
7Phương pháp 2. thư viện matplotlibChúng ta sẽ sử dụng thư viện Matplotlib để tải cùng một hình ảnh và hiển thị nó trong khung Matplotlib. Giống như PIL, nó có lớp 1Pillow Version: 7.0.0 7 thực hiện chức năng tương tự. Các hàm được sử dụng trong đoạn mã này là 1Pillow Version: 7.0.0 8 , tải hình ảnh ở dạng một mảng pixel và 1Pillow Version: 7.0.0 9, which displays that image. Chúng ta sẽ sử dụng lớp import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
60 từ thư viện Matplotlib để vẽ hình ảnh vào khung. import cv2
#read image
img_grey = cv2.imread('D:/original.png', cv2.IMREAD_GRAYSCALE)
# define a threshold, 128 is the middle of black and white in grey scale
thresh = 128
# threshold the image
img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]
#save image
cv2.imwrite('D:/black-and-white.png',img_binary) 2con trăn Sau bước đầu tiên tải hình ảnh bằng đối số import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
61 , chúng tôi nhận được báo cáo về kiểu dữ liệu của mảng. Trong trường hợp này, đó là số nguyên không dấu 8 bit. Hình dạng của mảng rộng 800 pixel, cao 450 pixel và import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
62 biểu thị các kênh màu cho đỏ, lục và lam. Chuyển đổi sang mảng NumPy và quay lạiTrong Python, Gối là thư viện tiêu chuẩn và phổ biến nhất khi làm việc với dữ liệu hình ảnh NumPy sử dụng lớp import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
63 để chuyển đổi hình ảnh PIL thành mảng NumPy. Hàm import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
64 cũng cho kết quả tương tự. Hàm ______165 hiển thị loại hình ảnh. Có thể đảo ngược quy trình bằng cách sử dụng chức năng import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
66 . Chức năng này rất hữu ích khi thao tác được thực hiện trên import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
67 dữ liệu hình ảnh mà sau này chúng ta muốn lưu dưới dạng tệp PNG hoặc JPEG. import cv2
#read image as grey scale
img_grey = cv2.imread('D:/greyscale.png', cv2.IMREAD_GRAYSCALE)
# define a threshold, 128 is the middle of black and white in grey scale
thresh = 128
# threshold the image
img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]
#save image
cv2.imwrite('D:/black-and-white.png',img_binary) 0con trăn import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
68 đưa ra giá trị của từng pixel của hình ảnh mảng NumPy.
import cv2
#read image as grey scale
img_grey = cv2.imread('D:/greyscale.png', cv2.IMREAD_GRAYSCALE)
# define a threshold, 128 is the middle of black and white in grey scale
thresh = 128
# threshold the image
img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]
#save image
cv2.imwrite('D:/black-and-white.png',img_binary) 2con trăn Thao tác và lưu hình ảnhBây giờ chúng tôi đã chuyển đổi hình ảnh của mình thành một mảng Numpy, chúng tôi có thể gặp trường hợp cần thực hiện một số thao tác trên hình ảnh trước khi sử dụng nó vào mô hình mong muốn. Trong phần này, bạn sẽ có thể xây dựng một bộ chuyển đổi thang độ xám. Bạn cũng có thể thay đổi kích thước mảng của hình ảnh pixel và cắt nó Sau khi thực hiện các thao tác, điều quan trọng là phải lưu ảnh trước khi thực hiện các bước tiếp theo. Đối số 1Pillow Version: 7.0.0 4 lưu tệp ở các định dạng khác nhau, chẳng hạn như PNG, GIF hoặc PEG. Ví dụ: mã bên dưới tải ảnh ở định dạng JPEG và lưu ảnh ở định dạng PNG Chuyển đổi hình ảnh sang thang độ xámimport cv2
#read image as grey scale
img_grey = cv2.imread('D:/greyscale.png', cv2.IMREAD_GRAYSCALE)
# define a threshold, 128 is the middle of black and white in grey scale
thresh = 128
# threshold the image
img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]
#save image
cv2.imwrite('D:/black-and-white.png',img_binary) 4con trăn import cv2
#read image as grey scale
img_grey = cv2.imread('D:/greyscale.png', cv2.IMREAD_GRAYSCALE)
# define a threshold, 128 is the middle of black and white in grey scale
thresh = 128
# threshold the image
img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]
#save image
cv2.imwrite('D:/black-and-white.png',img_binary) 5Thay đổi kích thước hình ảnh1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__) 0con trăn 1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__) 1Cắt xén hình ảnh1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__) 2con trăn Kiểm tra các hình ảnh trong đường dẫn bạn đã đề cập API máy ảnhHãy xem xét cùng một hình ảnh thử nghiệm. Keras cung cấp các chức năng tải, chuyển đổi và lưu dữ liệu hình ảnh. Để cài đặt Keras API trong môi trường ảo Anaconda, hãy sử dụng lệnh import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
70 (phiên bản CPU). Máy ảnh chạy trên đỉnh của khung TensorFlow. Đảm bảo gói được cài đặt chính xác. Các chức năng này có thể là các chức năng thuận tiện hữu ích khi bắt đầu một dự án thị giác máy tính học sâu mới hoặc khi bạn cần kiểm tra các hình ảnh cụ thể Tải hình ảnh bằng Keras APIMáy ảnh cung cấp chức năng ____171 để tải ảnh PIL. Tìm hiểu thêm về chức năng tại đây. 1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__) 3con trăn Theo đầu ra bên dưới, chúng tôi có thể xác nhận rằng hình ảnh được tải ở định dạng PIL và có các kênh màu RGB định dạng JPEG và kích thước 800 x 450 pixel. Hình ảnh sẽ được hiển thị trong trình xem ảnh mặc định của bạn Chuyển đổi hình ảnh bằng Keras APIKeras sử dụng chức năng import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
72 để chuyển đổi hình ảnh PIL thành numpy. API cũng cung cấp chức năng ____173 , có thể được sử dụng để chuyển đổi một mảng dữ liệu pixel thành hình ảnh PIL. 1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__) 4con trăn Lưu hình ảnh bằng KerasAPI Keras sử dụng chức năng import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
74 để lưu hình ảnh cục bộ. Ở đây hàm lấy đường dẫn và tên tệp mà chúng ta muốn lưu dữ liệu hình ảnh ở định dạng mảng NumPy. Chức năng này rất hữu ích khi bạn đã thao tác với hình ảnh và muốn lưu hình ảnh để sử dụng sau. 1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__) 5con trăn Thư viện OpenCVOpenCV là một thư viện thực hiện các thuật toán thị giác máy tính truyền thống. Khung Tensorflow + Keras là một lựa chọn phù hợp cho bất kỳ ai muốn làm việc với deep learning. OpenCV phiên bản 3. x đã giới thiệu các khung DNN và Caffe để giải quyết các vấn đề về học sâu Để làm việc với thư viện OpenCV, bạn cần cài đặt thư viện đó trong môi trường ảo bằng cách sử dụng import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
75 . Gói import cv2 as cv
import sys
import io
if len(sys.argv) != 3:
exit(f"Usage: {sys.argv[0]} IN_FILENAME OUT_FILENAME")
in_filename = sys.argv[1]
out_filename = sys.argv[2]
img = cv.imread(in_filename)
print(type(img)) # numpy.array
print(img.shape)
# Draw a rectangle in the middle in some color:
y_top = img.shape[0] // 4
x_top = img.shape[1] // 4
y_bottom = img.shape[0] - y_top
x_bottom = img.shape[1] - x_top
blue = 80
green = 70
red = 90
cv.rectangle(img, (x_top, y_top), (x_bottom, y_bottom), color=(blue, green , red), thickness=2)
# save the image to a file on the disk
cv.imwrite(out_filename, img)
# Convert the numpy array to a binary object in memory
def numpy_to_binary(arr):
is_success, buffer = cv.imencode(".jpg", arr)
io_buf = io.BytesIO(buffer)
print(type(io_buf))
return io_buf.read()
binary_image = numpy_to_binary(img)
print(type(binary_image)) # bytes
76 cung cấp chức năng 1Pillow Version: 7.0.0 8 để tải hình ảnh. Nó cũng đọc một hình ảnh PIL ở định dạng mảng NumPy. Điều duy nhất chúng ta cần chuyển đổi là màu hình ảnh từ BGR sang RGB. ______178 lưu hình ảnh vào tệp. 1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__) 6con trăn import cv2
#read image as grey scale
img_grey = cv2.imread('D:/greyscale.png', cv2.IMREAD_GRAYSCALE)
# define a threshold, 128 is the middle of black and white in grey scale
thresh = 128
# threshold the image
img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1]
#save image
cv2.imwrite('D:/black-and-white.png',img_binary) 5Phần kết luậnPython là một công cụ linh hoạt, cho chúng ta lựa chọn tải hình ảnh PIL theo hai cách khác nhau. Trong hướng dẫn này, bạn đã học được một số thủ thuật thao tác trên hình ảnh Mảng Numpy, sau đó chuyển đổi nó trở lại hình ảnh PIL và lưu công việc của chúng tôi. Hướng dẫn này cũng cung cấp cho bạn thông tin chi tiết về cách chuyển đổi hình ảnh thành dạng mảng bằng cách sử dụng Keras API và thư viện OpenCV. Hơn nữa, bạn có thể theo liên kết tài liệu thư viện Gối và thử thực hiện các kỹ thuật thao tác khác nhau, chẳng hạn như xây dựng một chức năng để mở rộng dữ liệu hình ảnh và đưa vào mạng lưới thần kinh học sâu
Làm cách nào để chuyển đổi hình ảnh thành nhị phân trong Python?
Nhập thư viện cần thiết. Trong tất cả các ví dụ sau, thư viện Python bắt buộc là OpenCV. . Đọc một hình ảnh đầu vào sử dụng cv2. imread(). . Bây giờ hãy chuyển đổi hình ảnh BGR này thành hình ảnh thang độ xám như bên dưới bằng cách sử dụng cv2. hàm cvtColor(). . Áp dụng ngưỡng cv2. . Hiển thị hình ảnh nhị phân đã chuyển đổi
Làm cách nào để chuyển đổi hình ảnh RGB thành nhị phân trong Python?
Làm cách nào để bạn chuyển đổi RGB sang nhị phân trong Python? . nhập numpy dưới dạng np từ numpy nhập ngẫu nhiên # Tạo một hình ảnh có giá trị từ 1 đến 255 im_thresh = ngẫu nhiên. randint(1,256, (64,64)) # Đặt giá trị nhỏ hơn 255 thành 0 # Phải thực hiện trước thao tác bên dưới để không đặt tất cả các giá trị thành 0 |