In the code part below I have problems with the TH matrix, output of the function. IMG and level are an rgb image and a value between 0 and 255, respectively. The function imbinarize sets each pixel ot TH to 0 and I don't know why. Is it something very trivial I fail to take into consideration?
function [TH] = threshold(IMG, level)
szrgb = size(IMG,3);
if szrgb ~= 1
warning('Input image is not grayscale, an attempt to convert it will be made')
IMG = rgb2gray(IMG);
TH = imbinarize(IMG, level);
Steps that did not solve my problem
I checked every step in debug mode. Went with different values for level. Converted level into int8 before calling imbinarize. Also, imbinarize(IMG), when the function uses Otsu's method for the right threshold, works fine.