def templating(): if 'filename' not in request.args: return 'please provide filename argument e.g \\templating?filename=example.png',400 if 'templateSize' in request.args: templateSize = request.args['templateSize'] else: templateSize = 15 template = np.full((templateSize, templateSize), 0, dtype=np.uint8) template = cv2.circle(template, (int(templateSize / 2), int(templateSize / 2)), 5, 255, cv2.FILLED) m = Templating(template, 0.50) filename = request.args['filename'] filepath = app.root_path + '/' + app.config['UPLOAD_FOLDER'] + '/' + secure_filename(filename) im = ImageManager(filepath) im.singleLayerFind(m,0) templateFile = filename.rsplit('.', 1)[0] + '_template.' + filename.rsplit('.', 1)[1] templateFilepath = filepath.rsplit('.', 1)[0] + '_template.' + filepath.rsplit('.', 1)[1] im.saveIntermidiary(templateFilepath) im.outline_mammal() outlinedFile = filename.rsplit('.', 1)[0] + '_template_outlined.' + filename.rsplit('.', 1)[1] outlinedFilepath = filepath.rsplit('.', 1)[0] + '_template_outlined.' + filepath.rsplit('.', 1)[1] im.saveOutlined(outlinedFilepath) return render_template('result.html', title='results', intermidaryFile=templateFile, outlinedFile=outlinedFile)
def colourBandCombiner(): s: ImageManager = ImageManager('images/image-4.tif') image = s.combineToSingleLayer([0.2,0.2,0.2,-0.2,-0.2]) image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) fig: plt.Figure = plt.figure() fig.suptitle('Combined') ax: Axes3D = fig.add_subplot(111, projection='3d') x = range(image.shape[0]) y = range(image.shape[1]) print(image.shape) X, Y = np.meshgrid(x, y) ax.plot_surface(X.T, Y.T, image) plt.savefig('images/image-4-combined.png') s: ImageManager = ImageManager('images/image-4.tif') image = s.combineToSingleLayer([0.2, 0.2, 0.2, -0.2, -0.2]) image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) row = image[4] #print(row) sum = 0 for x in row: sum = sum + x backav = sum / len(row) print('background', backav) sheep = image[27, 25:39] #print(sheep) sum = 0 for x in sheep: sum = sum + x sheepav = sum / len(sheep) print('sheep', sheepav) print('dif', sheepav - backav)
def large_file_th(): m = Thresholding(threshold_value=220) s: ImageManager = ImageManager("images/image3.tif") s.singleLayerFind(m, 3) s.saveIntermidiary('images/threshold.tif') s.outline_mammal() s.saveOutlined('images/outline.tif')
def single_channel_small_tiff_th(): m = Thresholding(threshold_value=120, min_pixels_in_sheep=10) s: ImageManager = ImageManager("images/image.TIF") s.singleLayerFind(m, 0) s.saveIntermidiary('images/result/threshold.tif') s.outline_mammal(baseImage='original') s.saveOutlined('images/result/outline.tif')
def single_channel_small_tiff(): template = np.zeros((30, 30), dtype=np.uint8) template = cv2.circle(template, (15, 15), 10, 255, cv2.FILLED) m = Templating(template, 0.75) s: ImageManager = ImageManager("images/image.TIF") s.singleLayerFind(m, 0) s.saveIntermidiary('images/result/templating.tif') s.outline_mammal(baseImage='original') s.saveOutlined('images/result/outlinetemplating.tif')
def three_channel_file_th(): """Main algorithm for this attempt using thresholding""" m = Thresholding(threshold_value=180) s: ImageManager = ImageManager("images/image2.JPG") s.singleLayerFind(m, 0) s.saveIntermidiary('images/result/threshold2.png') s.outline_mammal(baseImage='original') s.saveOutlined('images/result/outline2.png')
def threshold(filename): m = Thresholding(threshold_value=180,min_width_height=4) s: ImageManager = ImageManager(filename) sheeps = [] for x in range(0,255,20): m.threshold_value = x s.singleLayerFind(m, 0) s.saveIntermidiary('images/threshold/{0}-t-image-1.tif'.format(x)) s.outline_mammal(baseImage='rgb') s.saveOutlined('images/threshold/{0}-o-image-1.png'.format(x)) sheeps.append(len(s.locations)) print(sheeps)
def extracter(): templateSize = 10 template = np.full((templateSize, templateSize), 0, dtype=np.uint8) template = cv2.circle(template, (int(templateSize / 2), int(templateSize / 2)), 5, 255, cv2.FILLED) cv2.imwrite('images/template.png', template) m = Templating(template, 0.50) s: ImageManager = ImageManager("images/image3.tif") s.singleLayerFind(m, 0) s.extract_sheep("images/extracted")
def miniexample(): template = cv2.imread("images/sheep_(839, 3432).png", cv2.IMREAD_GRAYSCALE) m = Templating(template, 0.82) s: ImageManager = ImageManager("images/sheeps.png") l, intermidary = s.combinedSingleLayerFind(m, [0.299, 0.587, 0.114]) i = intermidary[:, :, 1] cv2.imwrite("images/sheeps1.png", i) i = cv2.normalize(i, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U) _, i = cv2.threshold(i, 245, 255, cv2.THRESH_BINARY) cv2.imwrite("images/sheeps2.png", i) s.outline_mammal("original") s.saveOutlined("images/sheeps3.png")
def testTempaltes(filename): sheeps = [] s: ImageManager = ImageManager(filename) order = [] for file in os.listdir('images/templates'): print(file) template = cv2.imread('images/templates/' + file,cv2.IMREAD_GRAYSCALE) m = Templating(template, 0.50) s.singleLayerFind(m, 0) s.saveIntermidiary('images/template-results/{0}-t-image-1.tif'.format(file)) s.outline_mammal(baseImage='rgb') s.saveOutlined('images/template-results/{0}'.format(file)) sheeps.append(len(s.locations)) order.append(file) for x in sheeps: print(x) for x in order: print(x)
def testExtraTemplates(filename): sheeps = [] order = [] template = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) print(template.shape) m = Templating(template, 0.9,False) for x in range(1,9): s: ImageManager = ImageManager('images/image-{0}.tif'.format(x)) s.singleLayerFind(m, 0) s.saveIntermidiary('images/template-results2/{0}.tif'.format(x)) s.outline_mammal(baseImage='rgb') s.saveOutlined('images/template-results2/image-{0}.png'.format(x)) sheeps.append(len(s.locations)) order.append(x) for x in sheeps: print(x) for x in order: print(x)
def large_file(): templateSize = 30 template = np.full((templateSize, templateSize), 0, dtype=np.uint8) #template = cv2.circle(template, (15, 15), 7, 200, cv2.FILLED) template = cv2.circle(template, (int(templateSize / 2), int(templateSize / 2)), 5, 255, cv2.FILLED) #template = cv2.ellipse(img=template, center=(int(templateSize/2), int(templateSize/2)), axes=(4,8),angle=90,startAngle=0,endAngle=360,color=255,thickness=cv2.FILLED) cv2.imwrite('images/template.png', template) m = Templating(template, 0.50) s: ImageManager = ImageManager("images/image3.tif") s.singleLayerFind(m, 1) #s.combinedSingleLayerFind(m,[0.299,0.587,0.114]) #s.combinedSingleLayerFind(m, [0.2, 0.2, 0.2, 0.2, 0.2]) #s.multiLayerFind(m) print(s.locations) s.saveIntermidiary('images/templating.tif') s.outline_mammal(padding=10) s.saveOutlined('images/outlinetemplating.tif')
def thresholding(): if 'filename' not in request.args: return 'please provide filename argument e.g \\thresholding?filename=example.png',400 if 'threshold' in request.args: threshold = request.args['threshold'] else: threshold = 220 m = Thresholding(threshold_value=threshold) filename = request.args['filename'] filepath = app.root_path + '/' + app.config['UPLOAD_FOLDER'] + '/' + secure_filename(filename) im: ImageManager = ImageManager(filepath) im.singleLayerFind(m, 0) thresholdFile = filename.rsplit('.', 1)[0] + '_threshold.' + filename.rsplit('.', 1)[1] thresholdFilepath = filepath.rsplit('.', 1)[0] + '_threshold.' + filepath.rsplit('.', 1)[1] im.saveIntermidiary(thresholdFilepath) im.outline_mammal() outlinedFile = filename.rsplit('.', 1)[0] + '_threshold_outlined.' + filename.rsplit('.', 1)[1] outlinedFilepath = filepath.rsplit('.', 1)[0] + '_threshold_outlined.' + filepath.rsplit('.', 1)[1] im.saveOutlined(outlinedFilepath) return render_template('result.html', title='results', intermidaryFile=thresholdFile, outlinedFile=outlinedFile)