外枠の矩形を消す

矩形の中に矩形があれば白で塗りつぶして消すということをしたかったのです。 

 

f:id:alakialaca:20170123012339p:plain→ f:id:alakialaca:20170123012342p:plain

こういうことです。

ほとんど↓と同じです。

alakialaca.hatenablog.com

 

 

#!/usr/bin/python
# -*- coding:utf-8 -*-

import os,sys,re,cv,cv2
import numpy as np
from scipy import misc



imgFile = sys.argv[1]
argc = len(imgFile)
colorImage = cv2.imread(imgFile)

(hi, wi, di) = colorImage.shape
size = hi*wi


im_gray = cv2.cvtColor(colorImage,cv2.COLOR_BGR2GRAY)

kernel = np.ones((2,2),np.uint8)
debuImage = cv2.erode(im_gray,kernel,iterations = 1)


minArea = 200
squares =

ret,thresh = cv2.threshold(debuImage, 127, 255, 0)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_L1)


sq_bar =
#四角の中心のy座標と長さを格納
real_square =
out_square_arr=

for cnt in contours:
    cnt_len = cv2.arcLength(cnt, True)#領域を囲む周囲長を調べるtrueが輪郭が閉じているを示す
    cnt = cv2.approxPolyDP(cnt, 0.03*cnt_len, True)
    if len(cnt) != 4 or cv2.contourArea(cnt) < minArea or not cv2.isContourConvex(cnt): continue
    for cnt_x in range(cnt[0][0][0]-5,cnt[0][0][0]+5,1):
        if cnt_x == cnt[3][0][0]:
            squares.append(cnt)
    #矩形の中に矩形があれば外す
    for sq1 in squares:
        out_square = 0
        for sq2 in squares:
            if (sq1[0][0][0]<sq2[0][0][0] and sq1[0][0][1]<sq2[0][0][1] and sq1[1][0][0]>sq2[1][0][0] and sq1[1][0][1]<sq2[1][0][1] and sq1[2][0][0]>sq2[2][0][0]
 and sq1[2][0][1]>sq2[2][0][1] and sq1[3][0][0]<sq2[3][0][0] and sq1[3][0][1]>sq2[3][0][1]):
                out_square += 1
                continue
        if out_square == 0:
            real_square.append(sq1)
        else:
            out_square_arr.append(sq1)


cv2.drawContours(colorImage, out_square_arr, -1, (255, 255, 255), 6)
cv2.imshow("our_square",colorImage)
cv2.waitKey()

 

 

 

 if (sq1[0][0][0]<sq2[0][0][0] and sq1[0][0][1]<sq2[0][0][1] and sq1[1][0][0]>sq2[1][0][0] and sq1[1][0][1]<sq2[1][0][1] and sq1[2][0][0]>sq2[2][0][0]
 and sq1[2][0][1]>sq2[2][0][1] and sq1[3][0][0]<sq2[3][0][0] and sq1[3][0][1]>sq2[3][0][1]):

この部分で矩形の中に矩形があるかを見ているはず。