The size r should match the original I think. And I donβt understand why you are converting to single precision with single . In any case, I think you want to do the following:
%
The result is as follows:
r = 0 0 0 0 0 0 0 0 0 0.2336 0.2157 0.2514 0.2436 0.2257 0.2344 0 0 0.2453 0.2444 0.2671 0.2693 0.2418 0.2240 0 0 0.2741 0.2728 0.2397 0.2505 0.2375 0.2436 0 0 0 0 0 0 0 0 0
And with imfilter , this is:
r2 = imfilter(original, filter) r2 = 0.3778 0.3325 0.3307 0.3442 0.3516 0.3312 0.3163 0.3856 0.3298 0.2336 0.2157 0.2514 0.2436 0.2257 0.2344 0.3386 0.3434 0.2453 0.2444 0.2671 0.2693 0.2418 0.2240 0.3512 0.3272 0.2741 0.2728 0.2397 0.2505 0.2375 0.2436 0.3643 0.3830 0.3181 0.3329 0.3403 0.3508 0.3272 0.3412 0.4035
As you can see, the results are the same as those indicated at the borders. There are several strategies for calculating borders at borders, such as reflecting an image at borders, keeping them the same, etc. Please read the imfilter documentation and choose one strategy.
Note that I did not flip the filter here, as the filter is symmetrical in both directions. And I recommend that you avoid cycles! Your code has nested depth of field loops!
Finally, you can use two-dimensional convolution to do the same as imfilter :
r3 = conv2(original, filter, 'same'); r3 = 0.3778 0.3325 0.3307 0.3442 0.3516 0.3312 0.3163 0.3856 0.3298 0.2336 0.2157 0.2514 0.2436 0.2257 0.2344 0.3386 0.3434 0.2453 0.2444 0.2671 0.2693 0.2418 0.2240 0.3512 0.3272 0.2741 0.2728 0.2397 0.2505 0.2375 0.2436 0.3643 0.3830 0.3181 0.3329 0.3403 0.3508 0.3272 0.3412 0.4035
petrichor
source share