median filter on a region of interest
조회 수: 12 (최근 30일)
이전 댓글 표시
In image processing toolbox, is there a way to apply median filter only on specific region of interest (and not on the whole image)?.
If there is, can you give me an example written in code?
Thanks you all !
댓글 수: 2
Matt J
2022년 4월 26일
Would it be a rectangular region? If not, how would the filter handle pixel neighborhoods that straddle the ROI boundary?
답변 (2개)
DGM
2022년 4월 26일
편집: DGM
2022년 4월 26일
Just filter the whole image and then compose the two using a mask that defines the ROI.
A = imread('cameraman.tif');
% make a mask
imshow(A) % set up the axes
R = images.roi.Polygon(gca); % create ROI object
R.Position = [66 56;112 19;168 29;198 86;150 163;64 143;83 95];
mask = createMask(R); % create binary mask
% make a filtered copy
Amed = medfilt2(A,[5 5]);
% insert filtered region into original image
B = A;
B(mask) = Amed(mask);
imshow(B)
In this example, I used an ROI object to create a mask. You can use any method you want to create such a mask. The rest is just basic logical indexing.
댓글 수: 3
DGM
2022년 4월 26일
편집: DGM
2022년 4월 26일
That is correct. A compromise may be that you could select a rectangular region which circumscribes the non-rectangular ROI (plus padding determined by the window size). You could then filter that region of the image and compose as before. The relative benefit depends how big the ROI is compared to the image. At least that would let you get away with using standard tools.
DGM
2022년 4월 26일
편집: DGM
2022년 4월 26일
Actually, I didn't know this, but apparently you can manage to do this with roifilt2().
A = imread('cameraman.tif'); % same picture as before
mask = imread('mk.png'); % same mask as before
F = @(x) medfilt2(x,[5 5]);
B = roifilt2(A,mask,F);
imshow(B)
I don't know which is faster, but doing it by composition is at least more flexible.
EDIT: at least with cursory testing, using roifilt2() on such an image/mask isn't any faster than the logical composition example. Upon opening roifilt2(), it appears to be doing exactly what I described above -- filtering a circumscribed rectangular region and then doing the composition via simple logical indexing. With that in mind, there may be relative speed benefits if the ROI is much smaller than it is in these examples.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!