-
Notifications
You must be signed in to change notification settings - Fork 1
/
BgThresh.m
executable file
·31 lines (25 loc) · 1.13 KB
/
BgThresh.m
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
function [imgx, imgy,img_thresh, img_removed] = BgThresh(img_soure, thresh, small, big)
% Extract worm location from normalized image
% | Version | Author | Date | Commit
% | 0.1 | ZhouXY | 18.07.19 | The init version
% | 0.2 | H.F. | 18.09.05 |
% To Do: Binarize image with locally adaptive thresholding or only take
% threshold but keep graydrade
%
% Choose the threshold of image
img_thresh = imbinarize(img_soure,thresh);%previous use 0.2
% Choose the reasonable area to find out worm
imgremovesmall = bwareaopen(img_thresh,small); %remove regions <100 pixels
img_removed = RemoveBigArea(imgremovesmall,big);%remove regions >600 pixels
% Find connected components in binary image
CC = bwconncomp(img_removed,26); % should use 8 connected for 2d image
% Due to cellfun limit, size of img must be a cell form
s = size(img_removed);
SizeCell = cell(1,numel(CC.PixelIdxList));
SizeCell(1:end) = {s};
% Find out the centre of worm
centroids = cellfun(@LocateWormCentre, CC.PixelIdxList, SizeCell);
[x, y] = ind2sub(s, centroids); % Transfer linear index to subscript
imgx = y;
imgy = x;
%imgy = s(2)-imgy; % What is mean? invert the image