Arrays have imcompateble size error

2 次查看(过去 30 天)
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 个评论
Pratham Shah
Pratham Shah 2023-3-28
编辑:Pratham Shah 2023-3-29
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 2023-3-28
编辑:Walter Roberson 2023-3-28
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

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-3-28
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by