Một loạt các chức năng tiện lợi để tạo các chức năng xử lý hình ảnh cơ bản như dịch thuật, xoay, thay đổi kích thước, bộ xương và hiển thị hình ảnh matplotlib dễ dàng hơn với openCV và cả Python 2.7 và python 3.both Python 2.7 and Python 3. Để biết thêm thông tin, cùng với đánh giá mã chi tiết, hãy xem các bài đăng sau trên blog PyimageSarch.com: Với điều kiện là bạn đã có Numpy, Scipy, Matplotlib và OpenCV đã được cài đặt, gói Imutils
Cài đặt
import imutils
imutils.find_function["contour"]
4 hoàn toàn ____ 15 có thể cài đặt được:
$ pip install imutils
Tìm chức năng chức năng OpenCV theo tên
OpenCV có thể là một thư viện lớn, khó điều hướng, đặc biệt nếu bạn mới bắt đầu học tầm nhìn máy tính và xử lý hình ảnh. Phương thức
import imutils imutils.find_function["contour"]6 cho phép bạn nhanh chóng tìm kiếm tên chức năng trên các mô-đun [và các mô hình phụ tùy ý] để tìm chức năng bạn đang tìm kiếm.
Example:
Hãy tìm tất cả các tên chức năng chứa văn bản
import imutils imutils.find_function["contour"]7:
import imutils imutils.find_function["contour"]
Output:
1. contourArea 2. drawContours 3. findContours 4. isContourConvex
Do đó, chức năng
import imutils imutils.find_function["contour"]8 có thể được truy cập thông qua:
import imutils imutils.find_function["contour"]9
Dịch
Dịch là sự dịch chuyển của một hình ảnh theo hướng x hoặc y. Để dịch một hình ảnh trong openCV, bạn sẽ cần cung cấp [x, y] -shift, được ký hiệu là [tx, ty] để xây dựng ma trận dịch m:
Và từ đó, bạn sẽ cần áp dụng chức năng
1. contourArea 2. drawContours 3. findContours 4. isContourConvex0.
Thay vì xây dựng thủ công ma trận dịch m và gọi
1. contourArea 2. drawContours 3. findContours 4. isContourConvex0, bạn chỉ có thể thực hiện cuộc gọi đến hàm
1. contourArea 2. drawContours 3. findContours 4. isContourConvex2 của
import imutils imutils.find_function["contour"]4.
Example:
# translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate[workspace, 25, -75]
Output:
Vòng xoay
Xoay một hình ảnh trong OpenCV được thực hiện bằng cách thực hiện cuộc gọi đến
1. contourArea 2. drawContours 3. findContours 4. isContourConvex4 và
1. contourArea 2. drawContours 3. findContours 4. isContourConvex0. Cần phải cẩn thận hơn nữa để cung cấp [x, y] -Coordinate của điểm mà hình ảnh sẽ được xoay. Những cuộc gọi tính toán này có thể nhanh chóng thêm và làm cho mã của bạn cồng kềnh và không dễ đọc. Hàm
1. contourArea 2. drawContours 3. findContours 4. isContourConvex6 trong
import imutils imutils.find_function["contour"]4 giúp giải quyết vấn đề này.
Example:
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]
Output:
Thay đổi kích thước
Thay đổi kích thước một hình ảnh trong openCV được thực hiện bằng cách gọi hàm
1. contourArea 2. drawContours 3. findContours 4. isContourConvex8. Tuy nhiên, cần được chăm sóc đặc biệt để đảm bảo rằng tỷ lệ khung hình được duy trì. Hàm
1. contourArea 2. drawContours 3. findContours 4. isContourConvex9 này của
import imutils imutils.find_function["contour"]4 duy trì tỷ lệ khung hình và cung cấp các đối số từ khóa
# translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate[workspace, 25, -75]1 và
# translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate[workspace, 25, -75]2 để hình ảnh có thể được thay đổi kích thước theo chiều rộng/chiều cao trong khi [1] duy trì tỷ lệ khung hình và [2] được tính toán rõ ràng bởi các nhà phát triển.
Một đối số từ khóa tùy chọn khác,
# translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate[workspace, 25, -75]3, cũng có thể được sử dụng để chỉ định phương thức nội suy.
Example:
# loop over varying widths to resize the image to for width in [400, 300, 200, 100]: # resize the image and display it resized = imutils.resize[workspace, width=width] cv2.imshow["Width=%dpx" % [width], resized]
Output:
Bộ xương
Bộ xương là quá trình xây dựng "bộ xương tôpô" của một đối tượng trong một hình ảnh, trong đó vật thể được cho là màu trắng trên nền đen. OpenCV không cung cấp một chức năng để xây dựng bộ xương một cách rõ ràng, nhưng không cung cấp các chức năng hình thái và nhị phân để làm như vậy.
Để thuận tiện, chức năng
# translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate[workspace, 25, -75]4 của
import imutils imutils.find_function["contour"]4 có thể được sử dụng để xây dựng bộ xương tôpô của hình ảnh.
Đối số đầu tiên,
# translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate[workspace, 25, -75]6 là kích thước của hạt nhân cấu trúc. Một đối số tùy chọn,
# translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate[workspace, 25, -75]7, có thể được sử dụng để kiểm soát phần tử cấu trúc - nó mặc định là
# translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate[workspace, 25, -75]8, nhưng có thể là bất kỳ phần tử cấu trúc hợp lệ nào.
Example:
# skeletonize the image gray = cv2.cvtColor[logo, cv2.COLOR_BGR2GRAY] skeleton = imutils.skeletonize[gray, size=[3, 3]] cv2.imshow["Skeleton", skeleton]
Output:
Hiển thị với matplotlib
Trong các ràng buộc python của opencv, hình ảnh được biểu diễn dưới dạng mảng numpy theo thứ tự BGR. Điều này hoạt động tốt khi sử dụng chức năng
# translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate[workspace, 25, -75]9. Tuy nhiên, nếu bạn có ý định sử dụng matplotlib, hàm
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]0 giả định hình ảnh theo thứ tự RGB. Một cuộc gọi đơn giản đến
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]1 sẽ giải quyết vấn đề này hoặc bạn có thể sử dụng hàm tiện lợi
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]2.
Example:
# INCORRECT: show the image without converting color spaces plt.figure["Incorrect"] plt.imshow[cactus] # CORRECT: convert color spaces before using plt.imshow plt.figure["Correct"] plt.imshow[imutils.opencv2matplotlib[cactus]] plt.show[]
Output:
URL để hình ảnh
Đây là hàm
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]3 chấp nhận một tham số duy nhất:
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]4 của hình ảnh chúng tôi muốn tải xuống và chuyển đổi thành một mảng numpy ở định dạng OpenCV. Hàm này thực hiện tải xuống trong bộ nhớ. Hàm
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]3 đã được trình bày chi tiết ở đây trên blog PyimageSearch.
Example:
url = "//pyimagesearch.com/static/pyimagesearch_logo_github.png" logo = imutils.url_to_image[url] cv2.imshow["URL to Image", logo] cv2.waitKey[0]
Output:
Kiểm tra các phiên bản OpenCV
OpenCV 3 cuối cùng đã được phát hành! Nhưng với bản phát hành chính trở thành các vấn đề tương thích ngược [chẳng hạn như với các hàm
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]6 và
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]7]. Nếu bạn muốn mã OpenCV 3 của mình tương thích ngược với OpenCV 2.4.x, bạn sẽ cần cẩn thận để kiểm tra phiên bản OpenCV nào hiện đang được sử dụng và sau đó thực hiện hành động thích hợp.
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]8 và
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]9 là các hàm đơn giản có thể được sử dụng để tự động xác định phiên bản OpenCV của môi trường hiện tại.
Example:
print["Your OpenCV version: {}".format[cv2.__version__]] print["Are you using OpenCV 2.X? {}".format[imutils.is_cv2[]]] print["Are you using OpenCV 3.X? {}".format[imutils.is_cv3[]]]
Output:
import imutils imutils.find_function["contour"]0
Tự động phát hiện cạnh Canny
Máy dò Canny Edge yêu cầu hai tham số khi thực hiện độ trễ. Tuy nhiên, điều chỉnh hai tham số này để có được bản đồ cạnh tối ưu là không tầm thường, đặc biệt là khi làm việc với một bộ dữ liệu hình ảnh. Thay vào đó, chúng ta có thể sử dụng hàm
# loop over varying widths to resize the image to for width in [400, 300, 200, 100]: # resize the image and display it resized = imutils.resize[workspace, width=width] cv2.imshow["Width=%dpx" % [width], resized]0 sử dụng trung bình của cường độ pixel thang độ xám để rút ra các ngưỡng trên và dưới. Bạn có thể đọc thêm về chức năng
# loop over varying widths to resize the image to for width in [400, 300, 200, 100]: # resize the image and display it resized = imutils.resize[workspace, width=width] cv2.imshow["Width=%dpx" % [width], resized]0 ở đây.
Example:
import imutils imutils.find_function["contour"]1
Output:
Biến đổi phối cảnh 4 điểm
Một nhiệm vụ phổ biến trong tầm nhìn máy tính và xử lý hình ảnh là thực hiện biến đổi phối cảnh 4 điểm của ROI trong một hình ảnh và có được một từ trên xuống, "Chim Eye Eye" của ROI. Mô -đun
# loop over varying widths to resize the image to for width in [400, 300, 200, 100]: # resize the image and display it resized = imutils.resize[workspace, width=width] cv2.imshow["Width=%dpx" % [width], resized]2 chăm sóc điều này cho bạn. Một ví dụ trong thế giới thực về việc áp dụng biến đổi phối cảnh 4 điểm có thể bị ràng buộc trong blog này trên việc xây dựng một máy quét tài liệu di động kick-ass.
Thí dụ
Xem nội dung của
# loop over varying widths to resize the image to for width in [400, 300, 200, 100]: # resize the image and display it resized = imutils.resize[workspace, width=width] cv2.imshow["Width=%dpx" % [width], resized]3
Output:
Sắp xếp đường viền
Các đường viền trở lại từ
# loop over the angles to rotate the image for angle in xrange[0, 360, 90]: # rotate the image and display it rotated = imutils.rotate[bridge, angle=angle] cv2.imshow["Angle=%d" % [angle], rotated]6 chưa được phân loại. Bằng cách sử dụng mô-đun
# loop over varying widths to resize the image to for width in [400, 300, 200, 100]: # resize the image and display it resized = imutils.resize[workspace, width=width] cv2.imshow["Width=%dpx" % [width], resized]5, hàm
# loop over varying widths to resize the image to for width in [400, 300, 200, 100]: # resize the image and display it resized = imutils.resize[workspace, width=width] cv2.imshow["Width=%dpx" % [width], resized]6, chúng ta có thể sắp xếp một danh sách các đường viền từ từ trái sang phải, từ phải, từ trên xuống dưới và từ dưới lên, tương ứng.
Example:
Xem nội dung của
# loop over varying widths to resize the image to for width in [400, 300, 200, 100]: # resize the image and display it resized = imutils.resize[workspace, width=width] cv2.imshow["Width=%dpx" % [width], resized]7
Output:
[Đệ quy] Liệt kê các đường dẫn đến hình ảnh
Mô hình phụ
# loop over varying widths to resize the image to for width in [400, 300, 200, 100]: # resize the image and display it resized = imutils.resize[workspace, width=width] cv2.imshow["Width=%dpx" % [width], resized]8 của
import imutils imutils.find_function["contour"]4 bao gồm một hàm để tìm hình ảnh đệ quy dựa trên thư mục gốc.
Example:
Giả sử chúng ta đang ở trong thư mục
# skeletonize the image gray = cv2.cvtColor[logo, cv2.COLOR_BGR2GRAY] skeleton = imutils.skeletonize[gray, size=[3, 3]] cv2.imshow["Skeleton", skeleton]0, hãy liệt kê các nội dung của
# skeletonize the image gray = cv2.cvtColor[logo, cv2.COLOR_BGR2GRAY] skeleton = imutils.skeletonize[gray, size=[3, 3]] cv2.imshow["Skeleton", skeleton]1:
import imutils imutils.find_function["contour"]2
Output:
import imutils imutils.find_function["contour"]3