Anonymous
Расширение изображения с помощью Python
Сообщение
Anonymous » 07 ноя 2025, 00:47
Я пытаюсь выполнить фрагмент кода, который нашел в Интернете, и он выдает следующую ошибку:
Код: Выделить всё
in image_masking(filepath)
15 gray = cv2.imread(filepath,0)
16 edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
---> 17 edges = cv2.dilate(edges,None)
18 edges = cv2.erode(edges, None)
19
error: OpenCV(3.4.1) C:\Miniconda3\conda-bld\opencv-
suite_1533128839831\work\modules\core\src\matrix.cpp:760: error: (-215)
dims 0 in function cv::Mat::locateROI
код:
Код: Выделить всё
import cv2
import numpy as np
def image_masking(filepath):
BLUR = 21
CANNY_THRESH_1 = 100
CANNY_THRESH_2 = 100
MASK_DILATE_ITER = 10
MASK_ERODE_ITER = 10
MASK_COLOR = (0.0,0.0,0.0) # In BGR format
gray = cv2.imread(filepath,0)
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
edges = cv2.dilate(edges,None)
edges = cv2.erode(edges, None)
contour_info = []
_, contours, __ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
for c in contours:
contour_info.append((c, cv2.isContourConvex(c), cv2.contourArea(c),))
contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
max_contour = contour_info[0]
for c in contour_info:
cv2.fillConvexPoly(mask, c[0], (255))
mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
mask_stack = np.dstack([mask]*3)
mask_stack = mask_stack.astype('float32') / 255.0
img = img.astype('float32') / 255.0
masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR)
masked = (masked * 255).astype('uint8')
fileName, fileExtension = filepath.split('.')
fileName += '-masked.'
filepath = fileName + fileExtension
print(filepath)
cv2.imwrite(filepath, masked)
if __name__ == '__main__':
filepath = 'C:\\Users\HP\Downloads\test3.jpg'
image_masking(filepath)
Я пробовал заменить None в функции расширения на ядро, но выдает ту же ошибку
Подробнее здесь:
https://stackoverflow.com/questions/550 ... ith-python
1762465675
Anonymous
Я пытаюсь выполнить фрагмент кода, который нашел в Интернете, и он выдает следующую ошибку: [code] in image_masking(filepath) 15 gray = cv2.imread(filepath,0) 16 edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2) ---> 17 edges = cv2.dilate(edges,None) 18 edges = cv2.erode(edges, None) 19 error: OpenCV(3.4.1) C:\Miniconda3\conda-bld\opencv- suite_1533128839831\work\modules\core\src\matrix.cpp:760: error: (-215) dims 0 in function cv::Mat::locateROI [/code] код: [code]import cv2 import numpy as np def image_masking(filepath): BLUR = 21 CANNY_THRESH_1 = 100 CANNY_THRESH_2 = 100 MASK_DILATE_ITER = 10 MASK_ERODE_ITER = 10 MASK_COLOR = (0.0,0.0,0.0) # In BGR format gray = cv2.imread(filepath,0) edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2) edges = cv2.dilate(edges,None) edges = cv2.erode(edges, None) contour_info = [] _, contours, __ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) for c in contours: contour_info.append((c, cv2.isContourConvex(c), cv2.contourArea(c),)) contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True) max_contour = contour_info[0] for c in contour_info: cv2.fillConvexPoly(mask, c[0], (255)) mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER) mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER) mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0) mask_stack = np.dstack([mask]*3) mask_stack = mask_stack.astype('float32') / 255.0 img = img.astype('float32') / 255.0 masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR) masked = (masked * 255).astype('uint8') fileName, fileExtension = filepath.split('.') fileName += '-masked.' filepath = fileName + fileExtension print(filepath) cv2.imwrite(filepath, masked) if __name__ == '__main__': filepath = 'C:\\Users\HP\Downloads\test3.jpg' image_masking(filepath) [/code] Я пробовал заменить None в функции расширения на ядро, но выдает ту же ошибку Подробнее здесь: [url]https://stackoverflow.com/questions/55022666/image-dilation-with-python[/url]