MATLAB ERROR for logic operation in strel?

조회 수: 4 (최근 30일)
Yigit Goktas
Yigit Goktas 2023년 3월 27일
댓글: Walter Roberson 2023년 3월 27일
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

답변 (1개)

Walter Roberson
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
Yigit Goktas
Yigit Goktas 2023년 3월 27일
Correction, wrote it as:
eroded_val = all(struct_et.Neighborhood(:) == sub_matrix(:));
Walter Roberson
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))

댓글을 달려면 로그인하십시오.

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by