将图像适应于ROI

4
我有一个 ROI 和一张图片。我需要用这张图片填充 ROI。图片应根据 ROI 的形状和大小进行缩放,并且应填满整个 ROI,而不重复图片。我如何使用 opencv 实现这一点?opencv 中是否有任何方法可以实现这一点?
假设这个白色部分是我的 ROI。

suppose this is the ROI

这是我的输入图片 在此输入图像描述 是否有使用ImageMagick的解决方案?

ROI 意思是感兴趣的矩形区域。你应该编辑你的问题以便被理解。 - sturkmen
@sturkmen 是的...我有2张图片..一张图片应该适合另一张图片的ROI - Neeraj
3
Roi的意思是“感兴趣的区域”(Region of interest)。 - Neeraj
你是对的,抱歉,但在OpenCV中通常意味着一个矩形区域。你只想适应两种形状的矩形区域吗? - sturkmen
1
请查看此示例。我尝试运行,但出现了错误。也许这个链接会有所帮助。 - sturkmen
显示剩余3条评论
1个回答

2
找到一个形状在另一个形状中的最佳适配并不容易,但如果你可以满足于次优结果,可以采取以下方法:
import cv2
import numpy as np
from matplotlib import pyplot as plt

bg_contours, bg_hierarchy = cv2.findContours(bg_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
bg_contour = bg_contours[0]
bg_ellipse = cv2.fitEllipse(bg_contour)

p_contours, p_hierarchy = cv2.findContours(fruit_alpha, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

pear_hull = cv2.convexHull(p_contours[0])
pear_ellipse = cv2.fitEllipse(pear_hull)

min_ratio = min(bg_ellipse[1][0] / pear_ellipse[1][0], bg_ellipse[1][1] / pear_ellipse[1][1])

x_shift = bg_ellipse[0][0] - pear_ellipse[0][0] * min_ratio
y_shift = bg_ellipse[0][1] - pear_ellipse[0][1] * min_ratio

(启发式)调整水果轮廓,从基于椭圆的初始猜测开始,使用轮廓进行优化(这可以改进,但它是一个非平凡的优化问题,您可以在此处了解更多:链接):

r_contour = np.array([[[int(j) for j in i[0]]] for i in min_ratio * p_contours[max_c_ix]])

min_dist, bad_pt = GetMinDist(outer_contour=bg_contour, inner_contour=r_contour, offset=(int(x_shift), int(y_shift)))
mask_size = max(bg_ellipse[1][0], bg_ellipse[1][1])
scale = min_ratio * (mask_size + min_dist) / mask_size

r_contour = np.array([[[int(j) for j in i[0]]] for i in scale * p_contours[max_c_ix]])

使用alpha通道合并图像:

combined = CombineImages(bg, fruit_rgb, fruit_alpha, scale, (int(x_shift), int(y_shift)))

实用函数:

def GetMinDist(outer_contour, inner_contour, offset):
    min_dist = 10000
    bad_pt = (0,0)
    for i_pt in inner_contour:
        #pt = (float(i_pt[0][0]), float(i_pt[0][1]))
        pt = (i_pt[0][0] + int(offset[0]), i_pt[0][1] + int(offset[1]))
        dst = cv2.pointPolygonTest(outer_contour, pt, True)
        if dst < min_dist:
            min_dist = dst
            bad_pt = pt
    return min_dist, bad_pt

def CombineImages(mask_img, fruit_img, fruit_alpha, scale, offset):
    mask_height, mask_width, mask_dim = mask_img.shape
    combined_img = np.copy(mask_img)
    resized_fruit = np.copy(mask_img)
    resized_fruit[:] = 0
    resized_alpha = np.zeros( (mask_height, mask_width), fruit_alpha.dtype)
    f_height, f_width, f_dim = fruit_img.shape
    r_fruit = cv2.resize(fruit_img, (int(f_width*scale), int(f_height*scale)) )
    r_alpha = cv2.resize(fruit_alpha, (int(f_width*scale), int(f_height*scale)) )
    height, width, channels = r_fruit.shape
    roi_x_from = offset[0]
    roi_x_to   = offset[0] + width
    roi_y_from = offset[1]
    roi_y_to   = offset[1] + height
    resized_fruit[roi_y_from:roi_y_to, roi_x_from:roi_x_to, :] = r_fruit
    resized_alpha[roi_y_from:roi_y_to, roi_x_from:roi_x_to] = r_alpha
    for y in range(0,mask_height):
        for x in range(0, mask_width):
            if resized_alpha[y,x] > 0:
                combined_img[y,x,:] = resized_fruit[y,x,:]

    return combined_img

enter image description here

希望这有所帮助。

(我省略了不影响流程理解的代码部分)


一系列函数清晰地呈现。为什么这个答案没有被验证? - Jeru Luke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接