Skip to content

Commit

Permalink
Merge pull request #6 from MatlabCompat/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ewiger committed Jul 17, 2015
2 parents a8a9ca1 + fa81287 commit 67f5e45
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/MatlabCompat/imagetools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function bwlabel(inputImage,connectivity)
error("Invalid pixel connectivity. Pixel connectivity can be either 4 or 8");
end

labeledImage = label_components(inputImage,connectivityMatrix)'; #transposing to be consistent with original input Image
labeledImage = label_components(inputImage,connectivityMatrix);


return labeledImage;
Expand Down
92 changes: 78 additions & 14 deletions test/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,81 @@ using MatlabCompat.MathTools
using Images,FixedPointNumbers


numberOfIterations = 1000;

innerExecutionTimes = Float64[0];
outerExecutionTimes = Float64[0];
img = imread("C:\\Users\\Vardan\\Documents\\Julia\\matlabcompat.github.io\\img\\example.tif");
function graythresh2(img)


#Check whether the input image is empty
if isempty(img)
error("Image is empty");
end
#Check whether the input image is gray

if Images.colorspace(img) != "Gray"
error("Input Image should be grayscale")
end

if isa(raw(img),Array{Uint16,2})
#Convert image to 8bit and return it's raw values to compute the histogram
image_array = map(Images.BitShift(Uint8,8),raw(img));
elseif isa(raw(img),Array{Uint8,2})
image_array = raw(img);
else
warn("Input Image is neither Uint8 or Uint16");
image_array = raw(img);
end
#image_array = raw(img)



expected_number_of_bins = 2^8;


(range, counts) = hist(reshape(image_array,length(image_array)),expected_number_of_bins);
methods(hist)

#get real number of bins to be used below
number_of_bins = size(counts)[1];
#Next the algorithm implementation according to the original paper
p = counts / sum(counts);
ω = cumsum(p);
μ = cumsum(p .* (1:number_of_bins));
μ_t = μ[end];


sigma_b_squared = (μ_t * ω - μ).^2 ./.* (1 - ω));

# Find the location of the maximum value of sigma_b_squared.
# The maximum may extend over several bins, so average together the
# locations. If maxval is NaN, meaning that sigma_b_squared is all NaN,
# then return 0.
maxvalue = maximum(sigma_b_squared);
if isfinite(maxvalue)
index = findfirst(sigma_b_squared, maxvalue);

# Normalize the threshold to the range [0, 1].
threshold = (index - 1) / (expected_number_of_bins - 1);
else
threshold = 0.0;
end

return threshold
end




numberOfIterations = 10000;

innerExecutionTimes = float64(zeros(1,numberOfIterations));
outerExecutionTimes = float64(zeros(1,numberOfIterations));
img = MatlabCompat.ImageTools.imread("http://matlabcompat.github.io/img/example.tif");

function simpleJanus()
threshold = graythresh(img); # compute graysacale threshold using Otsu algorithm
imgbw = im2bw(img, threshold); # create a binary image based on the graysacel image
imgbw =im2bw(img, threshold); # create a binary image based on the graysacel image
labeledbw = bwlabel(imgbw, 4); # lable each connected object in the image
numberOfCells = max(reshape(labeledbw, 1,numel(labeledbw))); # count cells
# numberOfCells = max(reshape(labeledbw, 1,numel(labeledbw))); # count cells
#disp(strcat("number of objects:", num2str(numberOfCells)));
end

Expand All @@ -51,22 +115,22 @@ function forSimpleJanus()
for j=1:numberOfIterations
simpleJanus();
end
end


end

methods(hist)

for i=1:numberOfIterations
inner = @elapsed(simpleJanus());
push!(innerExecutionTimes, inner);
innerExecutionTimes[i] = inner;
end


for i=1:numberOfIterations
outer = @elapsed(forSimpleJanus());
push!(outerExecutionTimes,outer);
end
# for i=1:numberOfIterations
# outer = @elapsed(forSimpleJanus());
# outerExecutionTimes[i] = outer;
# end

println([median(innerExecutionTimes) std(innerExecutionTimes)])
print([median(outerExecutionTimes) std(outerExecutionTimes)])
# print([median(outerExecutionTimes) std(outerExecutionTimes)])

20 changes: 11 additions & 9 deletions test/benchmark.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.

numberOfIterations = 1000;
numberOfOuterIterations = 10;
numberInnerOfIterations =10000;
outerExecutionTimes=0;
innerExecutionTimes=0;
img = imread('http://matlabcompat.github.io/img/example.tif');

i=1;

for i = 1:numberOfIterations
outer = tic();
for j = 1:numberOfIterations
% for i = 1:numberOfOuterIterations
% outer = tic();
for j = 1:numberInnerOfIterations
inner = tic();
threshold = graythresh(img); % compute graysacale threshold using Otsu algorithm
imgbw = im2bw(img, threshold); % create a binary image based on the graysacel image
labeledbw = bwlabel(imgbw, 4); % lable each connected object in the image
numberOfCells = max(reshape(labeledbw, 1,numel(labeledbw))); % count cells
innerExecutionTimes(j) =toc(inner);
end
outerExecutionTimes(i)=toc(outer);
% numberOfCells = max(reshape(labeledbw, 1,numel(labeledbw))); % count cells
innerExecutionTimes(j) =toc(inner);
end
% outerExecutionTimes(i)=toc(outer);
%
% end


disp([median(innerExecutionTimes) std(innerExecutionTimes)]);
disp([median(outerExecutionTimes) std(outerExecutionTimes)]);
% disp([median(outerExecutionTimes) std(outerExecutionTimes)]);
9 changes: 4 additions & 5 deletions test/janus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ importall MatlabCompat.MathTools

tic()
img = imread("http://matlabcompat.github.io/img/example.tif"); # read the remote image
#img = imread("D:\github\matlabcompat.github.io\img\example.tif");
threshold = graythresh(img); # compute graysacale threshold using Otsu algorithm
imgbw = im2bw(img, threshold); # create a binary image based on the grayscale image
imshow(imgbw); # display the resulting binary image
labledbw = bwlabel(imgbw, 4); # lable each connected object in the image
numberOfCells = max(reshape(labledbw, 1,numel(labledbw))); # count cells
disp(strcat("number of objects:", num2str(numberOfCells)));
imshow(label2rgb(labeledbw,"jet",[1 1 1],"shuffle")# display the number of objects
labeledbw = bwlabel(imgbw, 4); # label each connected object in the image
numberOfCells = max(reshape(labeledbw, 1,numel(labeledbw))); # count cells
disp(strcat("number of objects:", num2str(numberOfCells)));# display the number of objects
imshow(label2rgb(labeledbw,"jet",[1 1 1],"shuffle"));
toc()

3 changes: 1 addition & 2 deletions test/janus.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
% SOFTWARE.
tic()
img = imread('http://matlabcompat.github.io/img/example.tif'); % read the remote image
%img = imread('D:\github\matlabcompat.github.io\img\example.tif');
threshold = graythresh(img); % compute graysacale threshold using Otsu algorithm
imgbw = im2bw(img, threshold); % create a binary image based on the grayscale image
imshow(imgbw); % display the resulting binary image
labeledbw = bwlabel(imgbw, 4); % lable each connected object in the image
labeledbw = bwlabel(imgbw, 4); % label each connected object in the image
numberOfCells = max(reshape(labeledbw, 1,numel(labeledbw))); % count cells
disp(strcat('number of objects:', num2str(numberOfCells)));% display the number of objects
imshow(label2rgb(labeledbw,'jet',[0 0 0],'shuffle'));
Expand Down

0 comments on commit 67f5e45

Please sign in to comment.