我關注了這個討論:How extract pictures from an big image in python
- 做我的,這是輸出鏈接https://imgur.com/a/GK8747f和下面的 .py 腳本
import cv2
image = cv2.imread('/home/joy/桌面/test_11_4/original.png', cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
gradient = cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel)
contours = cv2.findContours(gradient, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
for cnt in contours:
(x,y,w,h) = cv2.boundingRect(cnt)
aaa = cv2.rectangle(image, (x,y), (x w,y h), (0,0,255))
cv2.imwrite("/home/joy/桌面/test_11_4/output.png", aaa)
我確實把它們框起來了,然后如何保存每個/二維碼
uj5u.com熱心網友回復:
我確實把它們框起來了,然后如何保存每個/二維碼
如果要保存每個矩形的內容,有兩個步驟。
在你的 for 回圈中,第一步是復制影像并將其裁剪為矩形的大小。
現在您已經提取了該影像,下一步是保存它。您需要為每個檔案使用不同的檔案名。
for cnt in contours:
(x,y,w,h) = cv2.boundingRect(cnt)
crop_img = image[y:y h, x:x w]
cv2.imwrite(f"/home/joy/桌面/test_11_4/output_{x}_{y}.png", crop_img)
在此示例中,我使用矩形的 X 和 Y 坐標來選擇檔案名。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/529390.html
