Why the error?
Show older comments
As a result of executing the following code, there is an error between a and b.
a and b have different values.

I used R2023a.
Please tell me the cause.
img=rand(3248,2048);
img2=img(1613:3248,:);
filter=ones(25,25);
img=imfilter(img,filter,0);
img2=imfilter(img2,filter,0);
a=img(1625:3248,:);
b=img2(13:end,:);
disp(max(abs(a-b),[],'all'));
5 Comments
Anjaneyulu Bairi
on 10 Jan 2024
Can you post the screenshot of the error here
Dyuman Joshi
on 10 Jan 2024
"... there is an error between a and b."
What error?
Azusa Tsukahara
on 10 Jan 2024
Dyuman Joshi
on 10 Jan 2024
Just to be sure, How is tmp defined?
Is it
tmp = max(abs(a-b),[],'all')
Azusa Tsukahara
on 11 Jan 2024
Accepted Answer
More Answers (1)
Hassaan
on 10 Jan 2024
img = rand(3248,2048);
filter = ones(25,25);
% Apply the filter to the whole image with 'same' to ensure output size matches input
img_filtered = imfilter(img, filter, 'same', 'replicate');
% Extract the parts of the image to compare
a = img_filtered(1625:3248,:);
img2 = img_filtered(1613:3248,:);
b = img2(13:end,:);
% Ensure that 'a' and 'b' are the same size
assert(isequal(size(a), size(b)), 'Arrays a and b must be the same size.');
% Compute the maximum absolute difference
disp(max(abs(a - b), [], 'all'));
In this corrected code, the 'same' option in the imfilter function call ensures the output image is the same size as the input. The 'replicate' option is used to handle the boundaries by replicating the edge values of the image, which should be done consistently for both parts of the image.
By extracting a and b after applying the filter to the entire image, we ensure that they have the same boundary conditions. Finally, we assert that a and b are the same size before performing the element-wise subtraction and computing the maximum absolute difference. If they are not the same size, MATLAB will throw an error message.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
1 Comment
The example is obfuscating the problem.
The output variables a and b are not identical because of the difference in the specified edge handling. They're identical because they're simply the exact same region cropped out of one array.
% Extract the parts of the image to compare
a = img_filtered(1625:3248,:);
% img2 = img_filtered(1613:3248,:); % this is just obfuscation
% b = img2(13:end,:);
b = img_filtered(1625:3248,:); % the addressing is identical to a
Explicitly specifying 'same' isn't necessary or relevant, as it's already the default behavior of imfilter().
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



