Esempio n. 1
0
    def segments(self, show_int, pause_int):
        img = self.img_gray

        kernel1 = np.ones((2, 2), np.uint8)
        kernel2 = np.ones((1, 1), np.uint8)

        all_heights = []

        def show_img(name, fname):
            if int(show_int) != 0:
                cv2.imshow(name, fname)
            if int(pause_int) != 0:
                cv2.waitKey(0)

        show_img('Output0', img)

        boxes_temp = np.zeros(img.shape[:2], np.uint8)
        print "boxes generated"

        binary = preprocessing.binary_img(img)
        show_img('BinaryOutput', binary)

        comp_labels, num = label(binary, return_num=True)
        comp_labels = np.array(comp_labels)
        allsegments = []

        for component in range(1, (num)):
            coordinates = {'x': 0, 'y': 0, 'h': 0, 'w': 0}
            index = np.where(comp_labels == component)
            Ymax = np.amax(index[0])
            Ymin = np.amin(index[0])
            Xmax = np.amax(index[1])
            Xmin = np.amin(index[1])
            cv2.rectangle(binary, (Xmin, Ymin), (Xmax, Ymax), (255, 0, 0), -1)
            coordinates['x'] = Xmin
            coordinates['y'] = Ymin
            coordinates['w'] = (Xmax - Xmin)
            coordinates['h'] = (Ymax - Ymin)
            allsegments.append(coordinates)
        show_img('WITHRECT', binary)

        return allsegments
resultimagename = splitimagename[0] + "-result.jpg"

kernel1 = np.ones((2, 2), np.uint8)
kernel2 = np.ones((1, 1), np.uint8)

all_heights = []


img = cv2.imread(image, 0)
resultimage = cv2.imread(image)

# cv2.imshow('Output0',img)
words_temp = np.zeros(img.shape[:2], np.uint8)
boxes_temp = np.zeros(img.shape[:2], np.uint8)

binary = preprocessing.binary_img(img)
# cv2.imshow('Outputimg1',binary)

dilation = cv2.dilate(binary, kernel1, iterations=1)
# cv2.imshow('Outputimg2',dilation)

erosion = cv2.dilate(dilation, kernel1, iterations=1)
# cv2.imshow('Outputimg3',erosion)

edges = cv2.Canny(dilation, 50, 100)
# cv2.imshow('edges',edges)

dilation2 = cv2.dilate(edges, kernel1, iterations=1)
# cv2.imshow('Outputimg9999',dilation2)

inv9999 = 255 - dilation2
Esempio n. 3
0
 def init(self):
     self.img_gray = cv2.cvtColor(self.img_rgb, cv2.COLOR_BGR2GRAY)
     self.img_bin = preprocessing.binary_img(self.img_gray)
     self.w, self.h = self.img_gray.shape[::-1]
Esempio n. 4
0
import os, sys
from array import array
import cv2
import numpy as np
import json
import cv
import argparse
import shutil
import preprocessing
from scipy.stats import mode

img = cv2.imread("durga.jpg",0)
img_rgb = preprocessing.binary_img(img)
cv2.imshow("edges",img_rgb)
cv2.waitKey(0)

with open( str(bookdir+jsonfile) ) as f :
     d = json.load( f )
allimages = []
allimages = d['book']['images']
#print allimages

f= open(fuzzysegbookdir+segbookfilename,'w')
"""str2="\n{\n    \"imagepath\": "+"\""+fuzzysegbookdir+"\""+","
f.write(str2)
str3="\n    \"template-imagepath\": "+"\""+tempimg+  "\""+","
f.write(str3)
str4= "\n    \"segments\":[\n"
f.write(str4)"""

templateimg = cv2.imread(tempimg,0)
tempimg_bin = preprocessing.binary_img(templateimg)
w, h = templateimg.shape[::-1]

for i in range(len(allimages)):
    nMatches = 0
    str2="\n{\n    \"imagepath\": "+"\""+fuzzysegbookdir+"\""+","
    f.write(str2)    
    str3="\n    \"template-imagepath\": "+"\""+tempimg+ "\""+","
    f.write(str3)
    str4= "\n    \"segments\":[\n"
    f.write(str4)
    #print bookdir+allimages[i];
    splitimagename=allimages[i].split(".")
    resultimage=splitimagename[0]+"-result.jpg"
    """
    ***** this code is for preprocessing of both the source and template image and then apply template-match function.*****
Esempio n. 6
0
    def find_segments(self, show_int, pause_int, known_segments=None):

        img = self.img_gray

        kernel1 = np.ones((2, 2), np.uint8)
        kernel2 = np.ones((1, 1), np.uint8)

        all_heights = []

        def show_img(name, fname):
            if int(show_int) != 0:
                cv2.imshow(name, fname)
            if int(pause_int) != 0:
                cv2.waitKey(0)

        show_img('Output0', img)

        boxes_temp = np.zeros(img.shape[:2], np.uint8)
        print "boxes generated"

        binary = preprocessing.binary_img(img)
        show_img('BinaryOutput', binary)

        dilation = cv2.dilate(binary, kernel1, iterations=1)
        show_img('Dilation', dilation)

        erosion = cv2.dilate(dilation, kernel1, iterations=1)
        show_img('Erosion', erosion)

        edges = cv2.Canny(dilation, 50, 100)
        show_img('Edges', edges)

        dilation2 = cv2.dilate(edges, kernel1, iterations=1)
        show_img('Dilation9999', dilation2)

        inv9999 = 255 - dilation2
        show_img('inv9999', inv9999)

        edges = cv2.dilate(edges, kernel1, iterations=1)
        ret, thresh = cv2.threshold(erosion, 127, 255, 0)
        contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)

        for c in contours:
            x, y, w, h = cv2.boundingRect(c)
            #annotate(boxes_temp,(255,255,255),-1)
            if h > 10:
                all_heights.append(h)

        std_dev = np.std(all_heights)
        mn = np.mean(all_heights)
        md = np.median(all_heights)

        for xx in contours:
            cv2.drawContours(edges, [xx], -1, (255, 255, 255), -1)

        show_img('edges2', edges)

        ret, thresh = cv2.threshold(erosion, 127, 255, 0)
        contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)

        for c in contours:
            x, y, w, h = cv2.boundingRect(c)
            #if (mn+(std_dev/2)<h):
            cv2.rectangle(boxes_temp, (x, y), (x + w, y + h), (255, 0, 0), -1)
            #annotate(img,(255,0,0),2)

        show_img('boxes_temp', boxes_temp)

        ret, thresh = cv2.threshold(boxes_temp, 127, 255, 0)
        contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                               cv2.CHAIN_APPROX_SIMPLE)

        allsegments = []

        for c in contours:
            coordinates = DotDict({
                'x': 0,
                'y': 0,
                'h': 0,
                'w': 0,
                'score': float(0.0)
            })
            x, y, w, h = cv2.boundingRect(c)
            coordinates['x'] = x
            coordinates['y'] = y
            coordinates['w'] = w
            coordinates['h'] = h
            allsegments.append(ImgSegment(coordinates))

        if known_segments is None:
            known_segments = DisjointSegments()
        disjoint_matches = known_segments.merge(allsegments)

        # print "Disjoint Segments = " + json.dumps(disjoint_matches)
        return disjoint_matches