**File:** modules/imgproc/src/median_blur.simd.hpp
**Change Type:** modified
**Context:** PR #27299: imgproc: medianblur: Performance improvement

**Code Changes:**
```diff
@@ -13,6 +13,7 @@
 // Copyright (C) 2000-2008, 2018, Intel Corporation, all rights reserved.
 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
 // Copyright (C) 2014-2015, Itseez Inc., all rights reserved.
+// Copyright (C) 2025, Advanced Micro Devices, all rights reserved.
 // Third party copyrights are property of their respective owners.
 //
 // Redistribution and use in source and binary forms, with or without modification,
@@ -693,8 +694,16 @@ medianBlur_SortNet( const Mat& _src, Mat& _dst, int m )
 #else
```

Review comment:
Check this: ```.diff diff --git a/modules/imgproc/src/median_blur.simd.hpp b/modules/imgproc/src/median_blur.simd.hpp index 7cc0aa693c..6185f5f9fd 100644 --- a/modules/imgproc/src/median_blur.simd.hpp +++ b/modules/imgproc/src/median_blur.simd.hpp @@ -693,8 +693,15 @@ medianBlur_SortNet( const Mat& _src, Mat& _dst, int m ) #else int nlanes = 1; #endif - for( ; j <= size.width - nlanes - cn; j += nlanes ) + for (; j < size.width - cn; j += nlanes) { + if ( j > size.width - cn - nlanes ) { + if (j == 0 || src == dst) { + break; + } + j = size.width - cn - nlanes; + } + VT p0 = vop.load(row0+j-cn), p1 = vop.load(row0+j), p2 = vop.load(row0+j+cn); VT p3 = vop.load(row1+j-cn), p4 = vop.load(row1+j), p5 = vop.load(row1+j+cn); VT p6 = vop.load(row2+j-cn), p7 = vop.load(row2+j), p8 = vop.load(row2+j+cn); @@ -798,8 +805,17 @@ medianBlur_SortNet( const Mat& _src, Mat& _dst, int m ) #else int nlanes = 1; #endif - for( ; j <= size.width - nlanes - cn*2; j += nlanes ) + // for( ; j <= size.width - nlanes - cn*2; j += nlanes ) + // { + for (; j < size.width - cn*2; j += nlanes) { + if ( j > size.width - cn*2 - nlanes ) { + if (j == 0 || src == dst) { + break; + } + j = size.width - cn*2 - nlanes; + } + VT p0 = vop.load(row[0]+j-cn*2), p5 = vop.load(row[1]+j-cn*2), p10 = vop.load(row[2]+j-cn*2), p15 = vop.load(row[3]+j-cn*2), p20 = vop.load(row[4]+j-cn*2); VT p1 = vop.load(row[0]+j-cn*1), p6 = vop.load(row[1]+j-cn*1), p11 = vop.load(row[2]+j-cn*1), p16 = vop.load(row[3]+j-cn*1), p21 = vop.load(row[4]+j-cn*1); VT p2 = vop.load(row[0]+j-cn*0), p7 = vop.load(row[1]+j-cn*0), p12 = vop.load(row[2]+j-cn*0), p17 = vop.load(row[3]+j-cn*0), p22 = vop.load(row[4]+j-cn*0); ``` I was able to achieve a better performance on higher input resolution on my host with i7-12700K. Perf: <details> ``` Geometric mean (ms) Name of Test medianBlur-base medianBlur-patch-amd medianBlur-patch-fyt medianBlur-patch-amd medianBlur-patch-fyt vs vs medianBlur-base medianBlur-base (x-factor) (x-factor) medianBlur::Size_MatType_kSize::(127x61, 8UC1, 3) 0.020 0.002 0.003 8.60 5.73 medianBlur::Size_MatType_kSize::(127x61, 8UC1, 5) 0.115 0.021 0.041 5.43 2.76 medianBlur::Size_MatType_kSize::(127x61, 16UC1, 3) 0.005 0.002 0.002 2.30 2.27 medianBlur::Size_MatType_kSize::(127x61, 16UC1, 5) 0.177 0.024 0.018 7.47 9.85 medianBlur::Size_MatType_kSize::(127x61, 16SC1, 3) 0.005 0.003 0.002 1.56 2.56 medianBlur::Size_MatType_kSize::(127x61, 16SC1, 5) 0.177 0.021 0.018 8.56 9.92 medianBlur::Size_MatType_kSize::(127x61, 32FC1, 3) 0.007 0.005 0.004 1.51 1.93 medianBlur::Size_MatType_kSize::(127x61, 32FC1, 5) 0.039 0.033 0.033 1.19 1.19 medianBlur::Size_MatType_kSize::(127x61, 8UC4, 3) 0.020 0.008 0.020 2.44 1.03 medianBlur::Size_MatType_kSize::(127x61, 8UC4, 5) 0.118 0.082 0.097 1.44 1.22 medianBlur::Size_MatType_kSize::(320x240, 8UC1, 3) 0.084 0.013 0.012 6.57 7.10 medianBlur::Size_MatType_kSize::(320x240, 8UC1, 5) 0.502 0.108 0.107 4.66 4.70 medianBlur::Size_MatType_kSize::(320x240, 16UC1, 3) 0.091 0.020 0.021 4.56 4.26 medianBlur::Size_MatType_kSize::(320x240, 16UC1, 5) 0.851 0.270 0.272 3.15 3.13 medianBlur::Size_MatType_kSize::(320x240, 16SC1, 3) 0.091 0.019 0.018 4.69 5.09 medianBlur::Size_MatType_kSize::(320x240, 16SC1, 5) 0.831 0.269 0.255 3.09 3.25 medianBlur::Size_MatType_kSize::(320x240, 32FC1, 3) 0.040 0.033 0.033 1.20 1.21 medianBlur::Size_MatType_kSize::(320x240, 32FC1, 5) 0.289 0.260 0.259 1.11 1.12 medianBlur::Size_MatType_kSize::(320x240, 8UC4, 3) 0.106 0.058 0.049 1.84 2.17 medianBlur::Size_MatType_kSize::(320x240, 8UC4, 5) 0.633 0.443 0.425 1.43 1.49 medianBlur::Size_MatType_kSize::(640x480, 8UC1, 3) 0.176 0.040 0.038 4.40 4.62 medianBlur::Size_MatType_kSize::(640x480, 8UC1, 5) 1.064 0.329 0.311 3.24 3.42 medianBlur::Size_MatType_kSize::(640x480, 16UC1, 3) 0.235 0.087 0.072 2.71 3.25 medianBlur::Size_MatType_kSize::(640x480, 16UC1, 5) 1.873 0.745 0.728 2.51 2.57 medianBlur::Size_MatType_kSize::(640x480, 16SC1, 3) 0.240 0.086 0.073 2.79 3.27 medianBlur::Size_MatType_kSize::(640x480, 16SC1, 5) 1.877 0.737 0.724 2.55 2.59 medianBlur::Size_MatType_kSize::(640x480, 32FC1, 3) 0.143 0.125 0.126 1.14 1.13 medianBlur::Size_MatType_kSize::(640x480, 32FC1, 5) 0.981 0.936 0.944 1.05 1.04 medianBlur::Size_MatType_kSize::(640x480, 8UC4, 3) 0.265 0.199 0.169 1.34 1.57 medianBlur::Size_MatType_kSize::(640x480, 8UC4, 5) 1.639 1.321 1.257 1.24 1.30 medianBlur::Size_MatType_kSize::(1280x720, 8UC1, 3) 0.313 0.128 0.105 2.45 2.99 medianBlur::Size_MatType_kSize::(1280x720, 8UC1, 5) 1.864 0.806 0.765 2.31 2.44 medianBlur::Size_MatType_kSize::(1280x720, 16UC1, 3) 0.455 0.246 0.198 1.85 2.30 medianBlur::Size_MatType_kSize::(1280x720, 16UC1, 5) 3.384 1.757 1.672 1.93 2.02 medianBlur::Size_MatType_kSize::(1280x720, 16SC1, 3) 0.454 0.242 0.199 1.88 2.29 medianBlur::Size_MatType_kSize::(1280x720, 16SC1, 5) 3.379 1.751 1.684 1.93 2.01 medianBlur::Size_MatType_kSize::(1280x720, 32FC1, 3) 0.399 0.370 0.364 1.08 1.10 medianBlur::Size_MatType_kSize::(1280x720, 32FC1, 5) 2.729 2.695 2.675 1.01 1.02 medianBlur::Size_MatType_kSize::(1280x720, 8UC4, 3) 0.572 0.507 0.416 1.13 1.38 medianBlur::Size_MatType_kSize::(1280x720, 8UC4, 5) 3.614 3.223 3.052 1.12 1.18 ``` </details>