Why the error?
이전 댓글 표시
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
Anjaneyulu Bairi
2024년 1월 10일
Can you post the screenshot of the error here
Dyuman Joshi
2024년 1월 10일
"... there is an error between a and b."
What error?
Azusa Tsukahara
2024년 1월 10일
Dyuman Joshi
2024년 1월 10일
Just to be sure, How is tmp defined?
Is it
tmp = max(abs(a-b),[],'all')
Azusa Tsukahara
2024년 1월 11일
채택된 답변
추가 답변 (1개)
Hassaan
2024년 1월 10일
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
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().
카테고리
도움말 센터 및 File Exchange에서 Images에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




