MATLAB ERROR for logic operation in strel?
4 次查看(过去 30 天)
显示 更早的评论
I have a code functions for dilation and erosion without using built-ins. When I run the code in both dilation and erosion function parts the logic operation for strel gave an error. How to solve it?
Errors I get on these lines:
dilated_val = any(struct_et(:) | sub_matrix(:));
eroded_val = all(struct_et(:) == sub_matrix(:));
The entire code is given below:
% Read the car plate image
carplate = imread('Figure1.png');
carplate_bw = imbinarize(rgb2gray(carplate));
struct_et = strel('disk', 5);
% Perform dilation operation
dilated_img = dilation(carplate_bw, struct_et);
% Perform erosion operation
eroded_img = erosion(dilated_img, struct_et);
% Display the original image, dilated image, and eroded image
figure;
subplot(1,3,1); imshow(carplate); title('Original Image');
subplot(1,3,2); imshow(dilated_img); title('Dilated Image');
subplot(1,3,3); imshow(eroded_img); title('Eroded Image');
function dilated_img = dilation(src_img, struct_et)
[src_h, src_w] = size(src_img);
[et_h, et_w] = size(struct_et);
src_img_padded = padarray(src_img, [et_h-1, et_w-1], 'replicate', 'both');
dilated_img = zeros(src_h, src_w);
for i = 1:src_h
for j = 1:src_w
if src_img(i,j) == 1
% Get the sub-matrix corresponding to the structuring element
sub_matrix = src_img_padded(i:i+et_h-1, j:j+et_w-1);
% Perform OR operation between the sub-matrix and the structuring element
dilated_val = any(struct_et(:) | sub_matrix(:));
% Set the output pixel to 1 if any of the corresponding pixels in the sub-matrix is 1
dilated_img(i, j) = dilated_val;
end
end
end
end
function eroded_img = erosion(src_img, struct_et)
[src_h, src_w] = size(src_img);
[et_h, et_w] = size(struct_et);
src_img_padded = padarray(src_img, [et_h-1, et_w-1], 'replicate', 'both');
eroded_img = ones(src_h, src_w);
for i = 1:src_h
for j = 1:src_w
% Get the sub-matrix corresponding to the structuring element
sub_matrix = src_img_padded(i:i+et_h-1, j:j+et_w-1);
% Perform AND operation between the sub-matrix and the structuring element
eroded_val = all(struct_et(:) == sub_matrix(:));
% Set the output pixel to 0 if any of the corresponding pixels in the sub-matrix is 0
eroded_img(i,j) = eroded_val;
end
end
end
0 个评论
回答(1 个)
Walter Roberson
2023-3-27
struct_et = strel('disk', 5);
That is 1 x 1 object of class strel
dilated_img = dilation(carplate_bw, struct_et);
That is passed as the second parameter to dilation()
function dilated_img = dilation(src_img, struct_et)
which receives it under the name struct_et
dilated_val = any(struct_et(:) | sub_matrix(:));
With it being a 1 x 1 object of strel class, struct_et(:) is the same as struct_et without the (:)
You are then trying to use the logical or operator | with an object of class strel but logical or is not defined for that class.
Perhaps
dilated_val = any(struct_et.Neighborhood(:) | sub_matrix(:));
3 个评论
Walter Roberson
2023-3-27
Erosion should only pay attention to the pixels selected by the mask.
If you were to use
mask = logical(struct_et.Neighborhood);
then
all(sub_matrix(mask))
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!