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, hiển thị hình ảnh matplotlib, sắp xếp các đường viền, phát hiện các cạnh và dễ dàng hơn nhiều với OpenCV và cả Python 2.7 và Python 3.
- Mô tả dự án
- Chi tiết dự án
- Lịch sử phát hành
- Tải tập tin
Mô tả dự án
Tác giả của gói này đã không cung cấp một mô tả dự án
Chi tiết dự án
Tải tập tin
Tải xuống tệp cho nền tảng của bạn. Nếu bạn không chắc chắn nên chọn cái nào, hãy tìm hiểu thêm về việc cài đặt các gói.
Phân phối nguồn
Đóng
Băm cho imutils-0.5.4.tar.gz
Băm cho imutils-0.5.4.tar.gzimport imutils imutils.find_function("contour")4 | |
import imutils imutils.find_function("contour")5 | |
import imutils imutils.find_function("contour")6 |
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 bạn đã có Numpy, Scipy, Matplotlib và OpenCV đã được cài đặt, gói import imutils
imutils.find_function("contour")7 hoàn toàn ____ 18 có thể cài đặt được: 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")9 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. Hãy tìm tất cả các tên chức năng chứa văn bản 1. contourArea
2. drawContours
3. findContours
4. isContourConvex0: Do đó, hàm 1. contourArea
2. drawContours
3. findContours
4. isContourConvex1 có thể được truy cập thông qua: 1. contourArea
2. drawContours
3. findContours
4. isContourConvex2 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:Imutils
Cài đặt
Tìm chức năng chức năng OpenCV theo tên
Example:
Output:
1. contourArea
2. drawContours
3. findContours
4. isContourConvexDịch
Và từ đó, bạn sẽ cần áp dụng chức năng 1. contourArea 2. drawContours 3. findContours 4. isContourConvex3.
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. isContourConvex3, bạn chỉ cần thực hiện cuộc gọi đến chức năng 1. contourArea 2. drawContours 3. findContours 4. isContourConvex5 của import imutils imutils.find_function("contour")7.
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. isContourConvex7 và 1. contourArea 2. drawContours 3. findContours 4. isContourConvex3. 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. isContourConvex9 trong import imutils imutils.find_function("contour")7 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 # translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate(workspace, 25, -75)1. 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 # translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate(workspace, 25, -75)2 này của import imutils imutils.find_function("contour")7 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)4 và # translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate(workspace, 25, -75)5 để 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)6, 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, hàm # translate the image x=25 pixels to the right and y=75 pixels up translated = imutils.translate(workspace, 25, -75)7 của import imutils imutils.find_function("contour")7 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)9 là kích thước của hạt nhân cấu trúc. Một đối số tùy chọ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)0, có thể được sử dụng để kiểm soát phần tử cấu trúc - nó mặc định là # 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, 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 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)2. 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)3 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)4 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)5.
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)6 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)7 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)6 đã đượ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 chức năng # 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 và # 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). 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 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)1 và # 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 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")0Tự độ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)3 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)3 ở đây.
Example:
import imutils imutils.find_function("contour")1Output:
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)5 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)6
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)9 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)8, 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)9, 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 # skeletonize the image gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY) skeleton = imutils.skeletonize(gray, size=(3, 3)) cv2.imshow("Skeleton", skeleton)0
Output:
(Đệ quy) Liệt kê các đường dẫn đến hình ảnh
Mô hình phụ # skeletonize the image gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY) skeleton = imutils.skeletonize(gray, size=(3, 3)) cv2.imshow("Skeleton", skeleton)1 của import imutils imutils.find_function("contour")7 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 tôi đ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)3, 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)4:
import imutils imutils.find_function("contour")2