Example #1
0
 def __getOnePictureRegion(self,img):
     '''
     get region of the picture
     return:
          the boundingbox list of the image 
          and it save as ((xmin,ymin),(xmax,ymax))
     '''
     if img!=None:
         h_cof = float(img.shape[0])/self.height
         w_cof = float(img.shape[1])/self.width
         #resize the img to standard scale
         img = cv.resize(img,(self.height,self.width))
         #blur the image to remove the noise
         img = cv.GaussianBlur(img,(5,5),0.8,0.8,0)
         #get the edge set of the image
         e_set = []
         for i in range(0,self.height):
             for j in range(0,self.width):
                 ij_edge=[]
                 if j<self.width-1:
                     similar = abs(img.item(i,j)-img.item(i,j+1))
                     ij_edge.append((i*self.width+j+1,similar))
                 if i<self.height-1:
                     similar = abs(img.item(i,j)-img.item(i+1,j))
                     ij_edge.append(((i+1)*self.width+j,similar))
                 if j<self.width-1 and i<self.height-1:
                     similar = abs(img.item(i,j)-img.item(i+1,j+1))
                     ij_edge.append(((i+1)*self.width+(j+1),similar))
                 e_set.append(ij_edge)
         #do segment
         img_seg = segmentor.grap_base_seg(e_set,self.k)
         #return result in boundingbox list
         bbs = {}
         for i in range(0,len(img_seg)):
             raw = i//self.width
             column = i-raw*self.width
             region = disjoin_set.find(img_seg[i])
             if bbs.has_key(region):
                 if bbs[region][0] > column:
                     bbs[region][0] = column
                 if bbs[region][2] < column:
                     bbs[region][2] = column
                 if bbs[region][1] > raw:
                     bbs[region][1] = raw
                 if bbs[region][3] < raw:
                     bbs[region][3] = raw
             else:
                 bbs[region] = [column,raw,column,raw]
         regions = []
         filter_size = self.height*self.width*self.thresh
         for key in bbs:
             if (bbs[key][2]-bbs[key][0])\
                     *(bbs[key][3]-bbs[key][1])>=filter_size:
                         regions.append(
                                 [int(bbs[key][0]*w_cof),\
                                 int(bbs[key][1]*h_cof),\
                                 int(bbs[key][2]*w_cof),\
                                 int(bbs[key][3]*h_cof)]
                                 )
         return regions
def ssearch_test(img_path):
    img = cv.imread(img_path)
    if img == None:
        sys.stderr.write("error happend in Loading image!\n")
        return
    cv.namedWindow('image', cv.WINDOW_NORMAL)
    cv.imshow('image', img)
    cv.waitKey(0)
    img = cv.resize(img, (128, 128))
    img = cv.GaussianBlur(img, (5, 5), 0.8, 0.8, 0)
    lab_img = cv.cvtColor(img, cv.COLOR_RGB2LAB)
    '''
    for i in range(0,img.shape[0]):
        for j in range(0,img.shape[1]):
            if img.item(i,j,0) > 255 or img.item(i,j,0)<0\
                    or img.item(i,j,1) > 255 or img.item(i,j,1) < 0\
                    or img.item(i,j,2) > 255 or img.item(i,j,2) < 0:
                        print img[i,j]
    '''
    gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    e_set = []
    height = img.shape[0]
    width = img.shape[1]
    for i in range(0, height):
        for j in range(0, width):
            ij_edge = []
            if j < width - 1:
                similar = abs(gray_img.item(i, j) - gray_img.item(i, j + 1))
                ij_edge.append((i * width + j + 1, similar))
            if i < height - 1:
                similar = abs(gray_img.item(i, j) - gray_img.item(i + 1, j))
                ij_edge.append(((i + 1) * width + j, similar))
            if j < width - 1 and i < height - 1:
                similar = abs(
                    gray_img.item(i, j) - gray_img.item(i + 1, j + 1))
                ij_edge.append(((i + 1) * width + (j + 1), similar))
            e_set.append(ij_edge)
    k = 150
    initial_regions = segmentor.grap_base_seg(e_set, k)
    count = 0
    for item in initial_regions:
        if item == item.parent:
            count += 1
    print "The number of regions in initial is: " + str(count)
    bins = 25
    similar_coef = [1, 1, 1]
    bb_set = ssearch.group(lab_img, initial_regions, bins, similar_coef, 0)
    #draw the result
    for item in bb_set:
        mid_img = img.copy()
        if (item[1][0] - item[0][0]) * (item[1][1] - item[0][1]) < 900:
            continue
        cv.rectangle(mid_img, (item[0][1], item[0][0]),
                     (item[1][1], item[1][0]), [255, 255, 255])
        cv.imshow('result', mid_img)
        cv.waitKey(0)
    cv.destroyAllWindows()
def ssearch_test(img_path):
    img = cv.imread(img_path)
    if img == None:
        sys.stderr.write("error happend in Loading image!\n")
        return
    cv.namedWindow('image',cv.WINDOW_NORMAL)
    cv.imshow('image',img)
    cv.waitKey(0)
    img = cv.resize(img,(128,128))
    img = cv.GaussianBlur(img,(5,5),0.8,0.8,0)
    lab_img = cv.cvtColor(img,cv.COLOR_RGB2LAB)
    '''
    for i in range(0,img.shape[0]):
        for j in range(0,img.shape[1]):
            if img.item(i,j,0) > 255 or img.item(i,j,0)<0\
                    or img.item(i,j,1) > 255 or img.item(i,j,1) < 0\
                    or img.item(i,j,2) > 255 or img.item(i,j,2) < 0:
                        print img[i,j]
    '''
    gray_img = cv.cvtColor(img,cv.COLOR_RGB2GRAY)
    e_set=[]
    height = img.shape[0]
    width = img.shape[1]
    for i in range(0,height):
        for j in range(0,width):
            ij_edge=[]
            if j<width-1:
                similar = abs(gray_img.item(i,j)-gray_img.item(i,j+1))
                ij_edge.append((i*width+j+1,similar))
            if i<height-1:
                similar = abs(gray_img.item(i,j)-gray_img.item(i+1,j))
                ij_edge.append(((i+1)*width+j,similar))
            if j<width-1 and i<height-1:
                similar = abs(gray_img.item(i,j)-gray_img.item(i+1,j+1))
                ij_edge.append(((i+1)*width+(j+1),similar))
            e_set.append(ij_edge)
    k=150
    initial_regions = segmentor.grap_base_seg(e_set,k)
    count=0
    for item in initial_regions:
        if item == item.parent:
            count += 1
    print "The number of regions in initial is: "+str(count)
    bins = 25
    similar_coef = [1,1,1]
    bb_set = ssearch.group(lab_img,initial_regions,bins,similar_coef,0)
    #draw the result
    for item in bb_set:
        mid_img=img.copy()
        if (item[1][0]-item[0][0])*(item[1][1]-item[0][1]) < 900:
            continue
        cv.rectangle(mid_img,(item[0][1],item[0][0]),(item[1][1],item[1][0]),[255,255,255])
        cv.imshow('result',mid_img)
        cv.waitKey(0)
    cv.destroyAllWindows()
def main(img_path, thresh):
    #load the image
    raw_img = cv.imread(img_path)
    img = cv.cvtColor(raw_img, cv.COLOR_RGB2GRAY)  #use gray image
    if img == None:
        sys.stderr.write("error happened in Loading img!")
        return
    cv.namedWindow('image', cv.WINDOW_NORMAL)  # create a can resize window
    cv.imshow('image', img)  #draw image in window named "image"
    cv.waitKey(0)  #until a key stroke, it will continue
    #resize the iamge
    img = cv.resize(img, (128, 128))
    raw_img = cv.resize(raw_img, (128, 128))
    #blur to remove the noise
    img = cv.GaussianBlur(img, (5, 5), 0.8, 0.8, 0)
    #Get the edge set of the image
    e_set = []
    height = img.shape[0]
    width = img.shape[1]
    for i in range(0, height):
        for j in range(0, width):
            ij_edge = []
            if j < width - 1:
                similar = abs(img.item(i, j) - img.item(i, j + 1))
                #similar = pixel_similar.normalize_dif((i,j),(i,j+1),img,5)
                ij_edge.append((i * width + j + 1, similar))
            if i < height - 1:
                similar = abs(img.item(i, j) - img.item(i + 1, j))
                #similar = pixel_similar.normalize_dif((i,j),(i+1,j),img,5)
                ij_edge.append(((i + 1) * width + j, similar))
            if j < width - 1 and i < height - 1:
                similar = abs(img.item(i, j) - img.item(i + 1, j + 1))
                #similar = pixel_similar.normalize_dif((i,j),(i+1,j+1),img,5)
                ij_edge.append(((i + 1) * width + (j + 1), similar))
            e_set.append(ij_edge)
    print "The number of vertex is: " + str(len(e_set))
    #do segment
    k = 350  #(which is good for image with size: 128*128)
    img_seg = segmentor.grap_base_seg(e_set, k)
    print len(img_seg)
    count = 0
    for item in img_seg:
        if item == item.parent:
            count += 1
    print "region number is: " + str(count)
    #draw the result
    regions = {}
    '''
    for i in range(0,len(img_seg)):
        raw = i//width
        column = i-(raw*width)
        region = disjoin_set.find(img_seg[i])
        if regions.has_key(region):
            if regions[region][0]>raw:
                regions[region][0]=raw
            elif regions[region][1]<raw:
                regions[region][1] = raw
            else:
                pass
            if regions[region][2]>column:
                regions[region][2] = column
            elif regions[region][3]<column:
                regions[region][3] = column
            else:
                pass
        else:
            regions[region]=[raw,raw,column,column]
    '''
    for i in range(0, len(img_seg)):
        raw = i // width
        column = i - (raw * width)
        region = disjoin_set.find(img_seg[i])
        if regions.has_key(region):
            regions[region].append((raw, column))
        else:
            regions[region] = [(raw, column)]
    pixel_num = 0
    for region in regions:
        color = [
            random.randint(0, 255),
            random.randint(0, 255),
            random.randint(0, 255)
        ]
        pixel_num += len(regions[region])
        if len(regions[region]) > thresh:
            for p in regions[region]:
                raw_img[p[0], p[1]] = color
    print "The number of pixel in region is: " + str(pixel_num)
    cv.imshow('res', raw_img)
    cv.waitKey(0)
    cv.destroyAllWindows()
    '''
Example #5
0
 def __getOnePictureRegion(self, img):
     '''
     get region of the picture
     return:
          the boundingbox list of the image 
          and it save as ((xmin,ymin),(xmax,ymax))
     '''
     if img != None:
         h_cof = float(img.shape[0]) / self.height
         w_cof = float(img.shape[1]) / self.width
         #resize the img to standard scale
         img = cv.resize(img, (self.height, self.width))
         #blur the image to remove the noise
         img = cv.GaussianBlur(img, (5, 5), 0.8, 0.8, 0)
         #get the edge set of the image
         e_set = []
         for i in range(0, self.height):
             for j in range(0, self.width):
                 ij_edge = []
                 if j < self.width - 1:
                     similar = abs(img.item(i, j) - img.item(i, j + 1))
                     ij_edge.append((i * self.width + j + 1, similar))
                 if i < self.height - 1:
                     similar = abs(img.item(i, j) - img.item(i + 1, j))
                     ij_edge.append(((i + 1) * self.width + j, similar))
                 if j < self.width - 1 and i < self.height - 1:
                     similar = abs(img.item(i, j) - img.item(i + 1, j + 1))
                     ij_edge.append(
                         ((i + 1) * self.width + (j + 1), similar))
                 e_set.append(ij_edge)
         #do segment
         img_seg = segmentor.grap_base_seg(e_set, self.k)
         #return result in boundingbox list
         bbs = {}
         for i in range(0, len(img_seg)):
             raw = i // self.width
             column = i - raw * self.width
             region = disjoin_set.find(img_seg[i])
             if bbs.has_key(region):
                 if bbs[region][0] > column:
                     bbs[region][0] = column
                 if bbs[region][2] < column:
                     bbs[region][2] = column
                 if bbs[region][1] > raw:
                     bbs[region][1] = raw
                 if bbs[region][3] < raw:
                     bbs[region][3] = raw
             else:
                 bbs[region] = [column, raw, column, raw]
         regions = []
         filter_size = self.height * self.width * self.thresh
         for key in bbs:
             if (bbs[key][2]-bbs[key][0])\
                     *(bbs[key][3]-bbs[key][1])>=filter_size:
                 regions.append(
                         [int(bbs[key][0]*w_cof),\
                         int(bbs[key][1]*h_cof),\
                         int(bbs[key][2]*w_cof),\
                         int(bbs[key][3]*h_cof)]
                         )
         return regions
def main(img_path,thresh):
    #load the image
    raw_img = cv.imread(img_path)
    img = cv.cvtColor(raw_img,cv.COLOR_RGB2GRAY) #use gray image
    if img==None:
        sys.stderr.write("error happened in Loading img!")
        return
    cv.namedWindow('image',cv.WINDOW_NORMAL) # create a can resize window
    cv.imshow('image',img) #draw image in window named "image"
    cv.waitKey(0)          #until a key stroke, it will continue
    #resize the iamge
    img = cv.resize(img,(128,128))
    raw_img = cv.resize(raw_img,(128,128))
    #blur to remove the noise
    img = cv.GaussianBlur(img,(5,5),0.8,0.8,0)
    #Get the edge set of the image
    e_set=[]
    height = img.shape[0]
    width = img.shape[1]
    for i in range(0,height):
        for j in range(0,width):
            ij_edge=[]
            if j<width-1:
                similar = abs(img.item(i,j)-img.item(i,j+1))
                #similar = pixel_similar.normalize_dif((i,j),(i,j+1),img,5)
                ij_edge.append((i*width+j+1,similar))
            if i<height-1:
                similar = abs(img.item(i,j)-img.item(i+1,j))
                #similar = pixel_similar.normalize_dif((i,j),(i+1,j),img,5)
                ij_edge.append(((i+1)*width+j,similar))
            if j<width-1 and i<height-1:
                similar = abs(img.item(i,j)-img.item(i+1,j+1))
                #similar = pixel_similar.normalize_dif((i,j),(i+1,j+1),img,5)
                ij_edge.append(((i+1)*width+(j+1),similar))
            e_set.append(ij_edge)
    print "The number of vertex is: "+str(len(e_set))
    #do segment
    k=350   #(which is good for image with size: 128*128)
    img_seg = segmentor.grap_base_seg(e_set,k)
    print len(img_seg)
    count=0
    for item in img_seg:
        if item == item.parent:
            count+=1
    print "region number is: "+str(count)
    #draw the result
    regions = {}
    '''
    for i in range(0,len(img_seg)):
        raw = i//width
        column = i-(raw*width)
        region = disjoin_set.find(img_seg[i])
        if regions.has_key(region):
            if regions[region][0]>raw:
                regions[region][0]=raw
            elif regions[region][1]<raw:
                regions[region][1] = raw
            else:
                pass
            if regions[region][2]>column:
                regions[region][2] = column
            elif regions[region][3]<column:
                regions[region][3] = column
            else:
                pass
        else:
            regions[region]=[raw,raw,column,column]
    '''
    for i in range(0,len(img_seg)):
        raw = i//width
        column = i-(raw*width)
        region = disjoin_set.find(img_seg[i])
        if regions.has_key(region):
            regions[region].append((raw,column))
        else:
            regions[region]=[(raw,column)]
    pixel_num = 0 
    for region in regions:
        color=[random.randint(0,255),random.randint(0,255),random.randint(0,255)]
        pixel_num += len(regions[region])
        if len(regions[region]) > thresh:
            for p in regions[region]:
                raw_img[p[0],p[1]] = color
    print "The number of pixel in region is: "+str(pixel_num)
    cv.imshow('res',raw_img)
    cv.waitKey(0)
    cv.destroyAllWindows()

    '''