2017-09-13 3 views
0

次のコードを使用して、画像を取得し、関心のある領域を含むさまざまな小さな画像で区切ります。プロセスから特定の次元の画像を除外します(OpenCV、Python)

画像ロード

origin

出力

output1

import cv2 
import sys 

sys.path.insert(0, 'C:\\Users\\Bob\\Desktop\\Project') 
sys.path.insert(0, 'C:\\Users\\Bob\\Desktop\\Project\\FOLDER') 
sys.path.insert(0, 'C:\\Users\\Bob\\Desktop\\Project\\READER') 

import FOLDER.folders 
import READER.extractor 

timestr = FOLDER.folders.timestr 

################## AREA 1 ################## 

# Load the image 
img = cv2.imread('C:\\Users\\Bob\\Desktop\\Destination\\' + str(timestr) + '\\EXTRACTED\\' + 'area1.png') 

# convert to grayscale 
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

# smooth the image to avoid noises 
gray = cv2.medianBlur(gray,5) 

# Apply adaptive threshold 
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2) 
thresh_color = cv2.cvtColor(thresh,cv2.COLOR_GRAY2BGR) 

# apply some dilation and erosion to join the gaps - change iterations value to detect more or less area's 
thresh = cv2.dilate(thresh,None,iterations = 15) 
thresh = cv2.erode(thresh,None,iterations = 15) 

# Find the contours 
image,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 

# For each contour, find the bounding rectangle and draw it 
idx =0 

for cnt in contours: 
    idx += 1 
    x,y,w,h = cv2.boundingRect(cnt) 
    roi = gray[y:y + h, x:x + w] 
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) 
    cv2.rectangle(thresh_color,(x,y),(x+w,y+h),(0,255,0),2) 
    cv2.imwrite('C:\\Users\\Bob\\Desktop\\Destination\\' + str(timestr) + '\\EXTRACTED\\ex_area1' + str(idx) + '.png',roi) 

この

は一例です

output2

コードには、私が望ましくないいくつかのマイナーイメージ(人工物を言いましょう)があります。これらの画像はすべて特定の次元以下です。

selection

私の質問は:私はこれらの画像を削除するには、上記のコードに追加する必要があるでしょうか?例えば、次の寸法:250(幅)×60(高さ)ピクセル以下の画像を削除するには?領域を検出するために、このコードを使用します:

はあなたに

ヒントをありがとうImprove text area detection (OpenCV, Python)

答えて

1

ちょうど最初の場所でそれらを書くいけません。 60 * 250以上の画像のみを書き込みます。

for cnt in contours: 
     idx += 1 
     x,y,w,h = cv2.boundingRect(cnt) 
     roi = gray[y:y + h, x:x + w] 
     cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) 
     cv2.rectangle(thresh_color,(x,y),(x+w,y+h),(0,255,0),2) 
     if (w*h>250*60): #change this for proper settings 
      cv2.imwrite('C:\\Users\\Bob\\Desktop\\Destination\\' + str(timestr) 
      + '\\EXTRACTED\\ex_area1' + str(idx) + '.png',roi) 
+0

ありがとうございます。それは動作するようです。 – Link

関連する問題