Nhấp và cắt hình ảnh
Bạn có thể dễ dàng cắt một hình ảnh bằng cách sử dụng nhấp chuột trên OpenCV. Đối với điều này, bạn cần gọi openCV & nbsp; cv2.setMouseCallback [cửa sổ, hình ảnh]. & Nbsp; sau đó bạn cần phát hiện nút chuột trái bằng cách sử dụng & nbsp; ; cv2.Event_mousemove & nbsp; và cuối cùng bạn cần phát hiện nút chuột trái được phát hành hoặc & nbsp; cv2.event_lbuttonup & nbsp; sự kiện.cv2.setMouseCallback[“window”, image]. You then need to detect the left mouse button down using the cv2.EVENT_LBUTTONDOWN event, then continuously locate the position of the mouse using the cv2.EVENT_MOUSEMOVE event and at last you need to detect the left mouse button released or cv2.EVENT_LBUTTONUP event.
Bằng cách này, nó sẽ tạo một hình chữ nhật cho hình ảnh và cũng cung cấp ROI đầu ra của hình ảnh được cắt
Code:
import cv2 import numpy as np cropping = False x_start, y_start, x_end, y_end = 0, 0, 0, 0 image = cv2.imread['test.jpg'] oriImage = image.copy[] def mouse_crop[event, x, y, flags, param]: # grab references to the global variables global x_start, y_start, x_end, y_end, cropping # if the left mouse button was DOWN, start RECORDING # [x, y] coordinates and indicate that cropping is being if event == cv2.EVENT_LBUTTONDOWN: x_start, y_start, x_end, y_end = x, y, x, y cropping = True # Mouse is Moving elif event == cv2.EVENT_MOUSEMOVE: if cropping == True: x_end, y_end = x, y # if the left mouse button was released elif event == cv2.EVENT_LBUTTONUP: # record the ending [x, y] coordinates x_end, y_end = x, y cropping = False # cropping is finished refPoint = [[x_start, y_start], [x_end, y_end]] if len[refPoint] == 2: #when two points were found roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]] cv2.imshow["Cropped", roi] cv2.namedWindow["image"] cv2.setMouseCallback["image", mouse_crop] while True: i = image.copy[] if not cropping: cv2.imshow["image", image] elif cropping: cv2.rectangle[i, [x_start, y_start], [x_end, y_end], [255, 0, 0], 2] cv2.imshow["image", i] cv2.waitKey[1] # close all open windows cv2.destroyAllWindows[]
Output:
- Tác giả
- Bài viết gần đây
Hãy liên hệ với chúng tôi cho bất kỳ loại vấn đề kỹ thuật nào của bạn. Chúng tôi đang ở đây để giúp bạn.
Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều
'' ' | |
Installation: | |
Sử dụng Python 3.6 | |
$ pip cài đặt numpy | |
$ pip cài đặt gối | |
$ pip cài đặt opencv-python | |
'' ' | |
importcv2 cv2 | |
Sử dụng Python 3.6 numpy as np | |
$ pip cài đặt numpy PIL | |
$ pip cài đặt gối PIL import Image | |
cropping=False = False | |
$ pip cài đặt opencv-python, y_start, x_end, y_end = 0, 0, 0, 0 | |
basewidth=1000 = 1000 | |
Nhập khẩu | |
Nhập khẩu = Image.open['image_name1.jpg'] | |
Frompilimportimage = [basewidth/float[img.size[0]]] | |
x_start, y_start, x_end, y_end = 0, 0, 0, 0 = int[[float[img.size[1]]*float[wpercent]]] | |
# Vượt qua tên/đường dẫn hình ảnh=img.resize[[basewidth, hsize], PIL.Image.ANTIALIAS] | |
img = Image.open ['Image_Name1.jpg'].save['resized_image.jpg'] | |
wpercent = [baseWidth/float [img.size [0]]]] = cv2.imread['resized_image.jpg'] | |
hsize = int [[float [img.size [1]]*float [wpercent]]]] = image.copy[] | |
img = img.resize [[basewidth, hsize], pil.image.antialias] mouse_crop[event, x, y, flags, param]: | |
img.save ['resized_image.jpg'] | |
hình ảnh = cv2.ImRead ['resized_image.jpg'] x_start, y_start, x_end, y_end, cropping | |
oriimage = Image.Copy [] | |
DEFMOUSE_CROP [Sự kiện, X, Y, cờ, param]: | |
# Lấy tham chiếu đến các biến toàn cầu event == cv2.EVENT_LBUTTONDOWN: | |
GlobalX_Start, Y_Start, X_end, Y_end, Cây trồng, y_start, x_end, y_end = x, y, x, y | |
cropping=True = True | |
# Nếu nút chuột trái bị hỏng, hãy bắt đầu ghi | |
# [x, y] tọa độ và chỉ ra rằng việc cắt xén đang được event == cv2.EVENT_MOUSEMOVE: | |
ifEvent == CV2.Event_LbutTondown: cropping == True: | |
x_start, y_start, x_end, y_end = x, y, x, y, y_end = x, y | |
# Chuột đang di chuyển | |
ELIFEVENT == CV2.EVENT_MOUSEMOVE: event == cv2.EVENT_LBUTTONUP: | |
ifcropping == true: | |
x_start, y_start, x_end, y_end = x, y, x, y, y_end = x, y | |
# Chuột đang di chuyển = False # cropping is finished | |
ELIFEVENT == CV2.EVENT_MOUSEMOVE: = [[x_start, y_start], [x_end, y_end]] | |
ifcropping == true: len[refPoint] == 2: # when two points were found | |
x_end, y_end = x, y = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]] | |
# Nếu nút chuột trái được phát hành.imshow["Cropped", roi] | |
ELIFEVENT == CV2.EVENT_LBUTTONUP:.imwrite['mousecrop.jpg', roi] | |
# Ghi lại tọa độ kết thúc [x, y].namedWindow["image"] | |
cắt xén = giả# cắt xong.setMouseCallback["image", mouse_crop] | |
refpoint = [[x_start, y_start], [x_end, y_end]]] True: | |
iflen [refpoint] == 2: # Khi tìm thấy hai điểm = image.copy[] | |
ROI = oriimage [refpoint [0] [1]: refpoint [1] [1], refpoint [0] [0]: refpoint [1] [0]] not cropping: | |
cv2.imshow ["cắt", ROI].imshow["image", image] | |
cv2.imwrite ['mouseCrop.jpg', ROI] cropping: | |
CV2.namedWindow ["Hình ảnh"].rectangle[i, [x_start, y_start], [x_end, y_end], [255, 0, 0], 2] | |
cv2.setmouseCallback ["hình ảnh", mouse_crop].imshow["image", i] | |
Whiletrue:.waitKey[1] | |
i = Image.Copy [] all open windows | |
IFNOTCROPPING:.destroyAllWindows[] |