Skip to content

Commit

Permalink
add image_threshold.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pqpo committed Jul 12, 2019
1 parent 00fc2b3 commit 4077048
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
40 changes: 40 additions & 0 deletions edge_detection/image_threshold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/python
#coding=utf8

# 图像二值化工具

from tensorflow import flags
from skimage import color
from skimage import io
from skimage import transform
import numpy as np
from skimage import filters
import os

flags.DEFINE_string('input_img', '',
'input image.')
flags.DEFINE_string('output_img', '',
'output image.')

FLAGS = flags.FLAGS

if not os.path.exists(FLAGS.input_img):
print('--input_img invalid')
exit()

if FLAGS.output_img == '':
print('--output_img invalid')
exit()


def threshold(ann_path, save_path):
img = io.imread(ann_path, mode='RGB')
img = color.rgb2gray(img)
thresh = filters.threshold_otsu(img)
img = np.where(img > thresh, [255], [0])
img = np.clip(img, 0, 255).astype(np.uint8)
io.imsave(save_path, img)


if __name__ == '__main__':
threshold(FLAGS.input_img, FLAGS.output_img)
31 changes: 31 additions & 0 deletions edge_detection/simple_maker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from tensorflow import flags
from skimage import color
from skimage import io
from skimage import transform
import numpy as np
from skimage import filters
import os
import math

# todo

if __name__ == '__main__':
background = io.imread('make_test/background.jpg')
background_shape = background.shape
background_width = background_shape[0]
background_height = background_shape[1]

rect = io.imread('make_test/rect.png')
rect_shape = rect.shape
rect_width = rect_shape[0]
rect_height = rect_shape[1]

src = np.array([[0, 0], [400, 400], [800, 400], [400, 0]])
dst = np.array([[0, 0], [0, rect_height], [rect_width, rect_height], [rect_width, 0]])

tform3 = transform.ProjectiveTransform()
tform3.estimate(src, dst)

warped = transform.warp(rect, tform3, output_shape=(background_width, background_height))

io.imsave('make_test/rect_tr.png', warped)
13 changes: 0 additions & 13 deletions edge_detection/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import scipy.misc
import numpy as np
from skimage import color


def save_img(out_path, img):
Expand All @@ -28,15 +27,3 @@ def load_sample_from_csv(csv_file):
return images, annotations


def threshold(ann_path, hold):
# shutil.copy(ann_path, '{}.backup'.format(ann_path))
img = scipy.misc.imread(ann_path, mode='RGB')
img = color.rgb2gray(img)
img = np.where(img > hold, [255], [0])
save_img(ann_path+"_threshold.jpg", img)


if __name__ == '__main__':
ann_file = './test_image/annotation_IMG_20190704_143127.png'
threshold(ann_file, 0.5)

0 comments on commit 4077048

Please sign in to comment.