Clear Filters
Clear Filters

Arrays have imcompateble size error

2 views (last 30 days)
had an error about: imcompatable size etc.
ERROR:
Arrays have incompatible sizes for this operation.
Error in Q3>otsu_threshold (line 52)
variance = prob_background .* prob_foreground .* ((mean_background - mean_foreground).^2);
Error in Q3 (line 6)
binary_image1 = otsu_threshold(gray_image1);
Related documentation
CODE:
image1 = imread('Figure3_a.jpg');
gray_image1 = im2gray(image1);
% apply Otsu's thresholding to produce a binary image
binary_image1 = otsu_threshold(gray_image1);
% plot the original and binary images side by side
figure;
subplot(1, 2, 1); imshow(gray_image1); title('Original Image');
subplot(1, 2, 2); imshow(binary_image1); title('Binary Image');
% read the second image and convert it to grayscale
image2 = imread('Figure3_b.png');
gray_image2 = im2gray(image2);
% apply Otsu's thresholding to produce a binary image
binary_image2 = otsu_threshold(gray_image2);
% plot the original and binary images side by side
figure;
subplot(1, 2, 1); imshow(gray_image2); title('Original Image');
subplot(1, 2, 2); imshow(binary_image2); title('Binary Image');
function binary_image = otsu_threshold(source_image)
% calculate histogram of the input image
histogram = imhist(source_image);
% normalize the histogram so that its values lie in the range [0, 1]
histogram = histogram / numel(source_image);
% initialize variables for the maximum between-class variance and the optimal threshold value
max_variance = -inf;
optimal_threshold = 0;
% loop over all possible threshold values (1 to 255) and calculate the between-class variance for each
for t = 1:255
% calculate the probability of the pixels below the threshold
prob_background = sum(histogram(1:t));
% calculate the probability of the pixels above the threshold
prob_foreground = sum(histogram(t+1:end));
% calculate the mean intensity of the pixels below the threshold
mean_background = sum((0:t-1) .* histogram(1:t)) / prob_background;
% calculate the mean intensity of the pixels above the threshold
mean_foreground = sum((t:255) .* histogram(t+1:end)) / prob_foreground;
% calculate the between-class variance for the current threshold
variance = prob_background * prob_foreground * ((mean_background - mean_foreground).^2);
% update the maximum between-class variance and the optimal threshold if necessary
if variance > max_variance
max_variance = variance;
optimal_threshold = t;
end
end
% use the optimal threshold to produce the binary image
binary_image = source_image >= optimal_threshold;
end
  3 Comments
Pratham Shah
Pratham Shah on 28 Mar 2023
Edited: Pratham Shah on 29 Mar 2023
You need the change the way of obtaining mean_background and mean_background. You must be receiving an array in those variable.
One more thing, start the loop from i=2 as i=1 will result in 'divided by 0' error because prob_background is 0 for first iteration.
Yigit Goktas
Yigit Goktas on 28 Mar 2023
Edited: Walter Roberson on 28 Mar 2023
function binary_image = otsu_threshold(source_image)
histogram = imhist(source_image);
% normalize the histogram so that its values lie in the range [0, 1]
histogram = histogram / numel(source_image);
% initialize variables for the maximum between-class variance and the optimal threshold value
max_variance = -inf;
optimal_threshold = 0;
% loop over all possible threshold values (1 to 255) and calculate the between-class variance for each
for t = 2:255
% calculate the probability of the pixels below the threshold
prob_background = sum(histogram(1:t));
prob_foreground = sum(histogram(t+1:end));
mean_background = sum((0:t-1) .* histogram(1:t)) / prob_background;
mean_foreground = sum((t:255) .* histogram(t+1:end)) / prob_foreground;
variance = prob_background * prob_foreground * ((mean_background - mean_foreground).^2);
if variance > max_variance
max_variance = variance;
optimal_threshold = t;
end
end
binary_image = source_image >= optimal_threshold;
end
Walter its the otsu_threshold function? I changed i=1 to 2 as Pratham said but don't know how to obtain mean_background as an array here

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Mar 2023
The problem you were encountering is that your variable named histogram is a column vector, but you were combining it with row vectors.
img = imread('cameraman.tif');
out = otsu_threshold(img);
imshow(img); title('original');
imshow(out); title('thresholded')
function binary_image = otsu_threshold(source_image)
histogram = imhist(source_image);
% normalize the histogram so that its values lie in the range [0, 1]
histogram = histogram / numel(source_image);
% initialize variables for the maximum between-class variance and the optimal threshold value
max_variance = -inf;
optimal_threshold = 0;
% loop over all possible threshold values (1 to 255) and calculate the between-class variance for each
for t = 2:255
% calculate the probability of the pixels below the threshold
prob_background = sum(histogram(1:t));
prob_foreground = sum(histogram(t+1:end));
mean_background = sum((0:t-1).' .* histogram(1:t)) / prob_background;
mean_foreground = sum((t:255).' .* histogram(t+1:end)) / prob_foreground;
variance = prob_background * prob_foreground * ((mean_background - mean_foreground).^2);
if variance > max_variance
max_variance = variance;
optimal_threshold = t;
end
end
binary_image = source_image >= optimal_threshold;
end

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!