如何将缩小图像的轮廓应用于原始图像? [英] How can I apply the contours of a downsized image to the original image?

查看:48
本文介绍了如何将缩小图像的轮廓应用于原始图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完美的代码,可以用OpenCV查找轮廓.但是,我的代码处理了缩小的图像以提高计算速度.如何将缩小图像的轮廓应用于原始图像?

I have a perfect code for finding the contours with OpenCV. But, my code processes a downsized image for improving the computational speed. How can I apply the contours of a downsized image to the original image?

这是我的Python代码:

This is my Python code:

# 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 .

copied_image is smaller than the source_image. I only used the largest contour. Now, I want to apply the found contour with the source_image. However, in my code, the acquired contour is based on the copied_image.

推荐答案

如果您可以使用1或2像素的(in)精度,那么一个非常简单的解决方案就是将 x,y,边界矩形的w,h 值以及相应的缩放因子:

If you can live with an (in)accuracy of 1 or 2 pixels, a quite simple solution would be to just multiply the x, y, w, h values of your bounding rectangle with the corresponding scaling factors:

import cv2
import numpy as np

# Set up some test image
image = np.zeros((400, 400), np.uint8)
image = cv2.circle(image, (160, 160), 80, 255, cv2.FILLED)

# Find contour, and determine original bounding rectangle
cnt_orig = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]
x, y, w, h = cv2.boundingRect(cnt_orig[0])
print('Original bounding rectangle: ', x, y, w, h)

# Downsize image
image_small = cv2.resize(image.copy(), (124, 287))

# Determine scaling factors
scale_x = image.shape[1] / image_small.shape[1]
scale_y = image.shape[0] / image_small.shape[0]

# Find contour, and determine reconstructed bounding rectangle w.r.t. the scaling factors
cnt_small = cv2.findContours(image_small, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]
x, y, w, h = cv2.boundingRect(cnt_small[0]) * np.array([scale_x, scale_y, scale_x, scale_y])
print('Reconstructed bounding rectangle: ', x, y, w, h)

输出:

Original bounding rectangle:  80 80 161 161
Reconstructed bounding rectangle:  80.64... 79.44... 161.29... 161.67...

注意:所使用的测试图像非常简单.在更复杂的图像中找到更复杂的轮廓时,(in)精度可能会增加.

Notice: The used test image is very simple. The (in)accuracy might increase when finding more complex contours in more complex images.

----------------------------------------
System information
----------------------------------------
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.5
NumPy:       1.19.4
OpenCV:      4.4.0
----------------------------------------

这篇关于如何将缩小图像的轮廓应用于原始图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆