2016-04-25 7 views
4

私にはROIとイメージがあります。私はROIを自分のイメージで埋める必要があります。イメージはROIの形状とサイズに応じて拡大縮小し、イメージを繰り返さずにROI全体を埋める必要があります。 opencvを使ってこれをどうすれば実現できますか?これを達成するためのopencvの方法はありますか?ROIへの画像の適合

この白い部分は私のROIと

suppose this is the ROI

である、これは私の入力画像 enter image description here

であると仮定は、ImageMagickのを使用して、任意の解決策はあります?別の内部つの形状の最適なフィットを見つける

+0

ROIは、Rectangular Interestを意味します。あなたは理解するためにあなたの質問を編集してください。 – sturkmen

+0

@sturkmenはい..私は2つの画像を持っています..そして、1つの画像は別の画像のROIに合っている必要があります – Neeraj

+3

@sturkmen Roiは興味のある地域を意味します... – Neeraj

答えて

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 

(ヒューリスティック)果実の輪郭のサイズを変更し、最初の推測で始まります楕円に基づいて、輪郭使用して絞り込む(これを向上させることができますが、それは非自明な最適化問題である、あなたはhereより多くを見ることができます):

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]]) 

は、アルファチャンネルを使用して画像を合成:

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

私はホープことができます。

(私はフローの理解に寄与しないコード部分を省略した)

+0

明快に提示された一連の機能。なぜこの答えは検証されないのですか? –

関連する問題