-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_detection.py
65 lines (51 loc) · 1.71 KB
/
object_detection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 18 15:35:08 2017
@author: SzMike
"""
import BMS
import numpy as np
from skimage import morphology
from PIL import Image
import selectivesearch
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import os
def find_salient_objects(im,vis_diag=False):
img=np.array(im)
sm=BMS.compute_saliency(img)
mask=sm>0
mask_dil=morphology.binary_dilation(mask,morphology.disk(1))
mask_dil = mask_dil[:,:,np.newaxis]
mask_dil = np.broadcast_arrays(img, mask_dil)[1]
img_mask=img.copy()
img_mask[mask_dil==0]=255
im = Image.fromarray(np.uint8(img_mask))
if vis_diag:
im.show()
return im
def find_rois(img,vis_diag=False):
simg, bb= selectivesearch.selective_search(img, scale=200.0, sigma=1.2, min_size=100)
candidates = set()
for r in bb:
# excluding same rectangle (with different segments)
if r['rect'] in candidates:
continue
# excluding regions smaller than 2000 pixels
if r['size'] < 500:
continue
# distorted rects
x, y, w, h = r['rect']
if w / h > 1.5 or h / w > 1.5:
continue
candidates.add(r['rect'])
# draw rectangles on the original image
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(img)
for x, y, w, h in candidates:
#print x, y, w, h
rect = mpatches.Rectangle(
(x, y), w, h, fill=False, edgecolor='red', linewidth=1)
ax.add_patch(rect)
plt.show()
os.system('e:\\IPOL\\ace_20121029\\bin\\ace -a 8 -m interp:12 .\image.bmp .\e_image.bmp')