【发布时间】:2021-01-04 15:35:41
【问题描述】:
我有一个完美的代码可以用 OpenCV 找到轮廓。但是,我的代码处理缩小的图像以提高计算速度。如何将缩小图像的轮廓应用于原始图像?
这是我的 Python 代码:
# Image Read and Resizing
source_image = cv.imread(image_path)
copied_image = source_image.copy()
copied_image = imutils.resize(copied_image, height=500)
# Apply GaussianBlur + OTSU-Thresholding
grayscale_image = cv.cvtColor(copied_image, cv.COLOR_BGR2GRAY)
grayscale_image = cv.GaussianBlur(grayscale_image, (5, 5), 0)
ret, grayscale_image = cv.threshold(grayscale_image, 200, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# Find Contours
contours, hierarchy = cv.findContours(grayscale_image, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contour_sizes = [(cv.contourArea(contour), contour) for contour in contours]
biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
# Crop Image
x, y, w, h = cv.boundingRect(biggest_contour)
cropped_image = copied_image[y:y + h, x:x + w]
copied_image 小于source_image。我只使用了最大的轮廓。现在,我想用source_image 应用找到的轮廓。但是,在我的代码中,获取的轮廓是基于copied_image。
【问题讨论】: