Why the error?

6 views (last 30 days)
Azusa Tsukahara
Azusa Tsukahara on 10 Jan 2024
Commented: DGM on 12 Jan 2024
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'));
0
  5 Comments
Dyuman Joshi
Dyuman Joshi on 10 Jan 2024
Just to be sure, How is tmp defined?
Is it
tmp = max(abs(a-b),[],'all')
Azusa Tsukahara
Azusa Tsukahara on 11 Jan 2024
tmp.m is the code above.
disp(max(abs(a-b),[],'all'));
is
1.1369e-13

Sign in to comment.

Accepted Answer

DGM
DGM on 10 Jan 2024
Edited: DGM on 10 Jan 2024
The error is (almost) entirely a consequence of the difference in the way you're handling the region boundaries. When the image is filtered by imfilter(), it will be padded by half the filter width prior to processing. The areas of the output image that are within this same radius are influenced by whatever the padding content is.
With imfilter(), the default padding is zero. This is usually a bad choice, as it causes noticeable vignetting. While using replication or mirroring for the padding will produce less visually-noticeable effects in the output, the result will not generally be the same as if the original image content were used to begin with.
rng(3)
img = im2uint8(rand(5)); % a 5x5 test image
fk = ones(3)/9; % a 3x3 box average filter
% crop first, then filter
a = img(2:4,:); % middle 3 rows
a = imfilter(a,fk); % filter with default padding (zero)
imshow(a,'border','tight') % top and bottom rows are dark due to the influence of the padding
% crop first, but filter with different padding
b = img(2:4,:); % middle 3 rows
b = imfilter(b,fk,'replicate'); % filter with better padding
imshow(b,'border','tight') % top and bottom rows come from replicated image data
% filter first (with better padding), then crop
c = imfilter(img,fk,'replicate'); % filter with better padding
c = c(2:4,:); % middle 3 rows
imshow(c,'border','tight') % top and bottom rows come from original image data
% the interior of the middle rows are all equal,
% because they're not influenced by the edge content
range([a(2,:); b(2,:); c(2,:)],1)
ans = 1×5
42 0 0 0 37
% but _none_ of the three methods are equal
% because the edge handling is different
% edge replication _is not_ equivalent to edge preservation
[immse(a,b) immse(b,c) immse(a,c)]
ans = 1×3
1.0e+03 * 1.4274 0.3520 2.4783
So that's the source of your error. I'd avoid using default padding with imfilter(), but if 'replicate' or 'symmetric' aren't good enough, and you want to crop prior to filtering, you'll have to over-crop by at least half the filter width and then crop again after filtering. That way you can be assured that the internal padding method doesn't influence the output.
Note that in both your example and mine, the ROI is the entire width of the image. In this case, the region can't be over-cropped on that axis, so the left and right edges will always be influenced by the internal padding method.
  7 Comments
Azusa Tsukahara
Azusa Tsukahara on 12 Jan 2024
Edited: Azusa Tsukahara on 12 Jan 2024
The problem
R2019a : don’t occur
R2022a : don’t occur
R2022b : occur
R2023a : occur
R2023b : occur
According to the following URL, imfilter() has changed in version R2022b.
I think the change contains a problem.
DGM
DGM on 12 Jan 2024
Ah. Maybe it's handling things differently internally. I'm going to have to find a way to dig into that if I want to be able to talk about its behavior.

Sign in to comment.

More Answers (1)

Hassaan
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
DGM
DGM on 10 Jan 2024
Edited: DGM on 10 Jan 2024
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().

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!