Apply imfilter on specific regions (ROI) of RGB-image

8 views (last 30 days)
BA
BA on 17 Nov 2017
Edited: DGM on 2 Oct 2022
Hi,
I´m trying to apply imfilter on a specific Region as it is possible with roifilt2 on 2D-images. Tried to apply the filter already onto the single layers of the RGB-Image but didn´t got a suitable result. Has someone a tip?
I=imread('banana.jpg');
grayI=rgb2gray(I);
c=[261 308 308 261]; %colum
r=[ 182 182 229 229]; %row
BW=roipoly(I,c,r); %or manually: BW= roipoly(I);
H = fspecial('disk',100);
J = roifilt2(H,grayI,BW);
imshow(J);
Picture below shows J (this kind of result is desired for an RGB-Image).
Tried on RGB-Image:
filtered_im = zeros(size(I));
filtered_im(:,:,1) = roifilt2(H, I(:,:,1), BW);
filtered_im(:,:,2) = roifilt2(H, I(:,:,2), BW);
filtered_im(:,:,3) = roifilt2(H, I(:,:,3), BW);
imshow(filtered_im);
As result I´m looking for an Image which is only manipulated in the roi, imfilter seems not to work with a binary mask.
filtered_im2 = imfilter(I,H);
imshow(filtered_im2);
  1 Comment
Image Analyst
Image Analyst on 18 Nov 2017
With no code, and no input and output images, and no definition of "suitable" we have no ability to answer this.

Sign in to comment.

Answers (1)

DGM
DGM on 11 Mar 2022
Edited: DGM on 2 Oct 2022
The output image is class 'double', and you're storing uint8-scale data in it. It will be improperly-scaled for its class and will appear clipped as shown. Set the output array to the appropriate class.
I = imread('banana.jpg');
c = [551 440 561 688]; %colum
r = [151 244 367 280]; %row
BW = roipoly(I,c,r); %or manually: BW= roipoly(I);
H = fspecial('disk',10);
J = zeros(size(I),'like',I);
for c = 1:size(I,3)
J(:,:,c) = roifilt2(H,I(:,:,c),BW);
end
imshow(J);
Note that IPT roifilt2() has some rather frustrating limitations. Relevant to this task, it can't really handle color images. Since it only accepts 2D inputs, it can't utilize filter functions that require color information (e.g. hue/saturation adjustment). For tasks which don't require color information (e.g. spatial filters), color images can be processed in an external loop, but this often degrades the speed advantage.
MIMT roifilter() addresses these limitations and is generally faster than roifilt2(). In this case, the speed advantage comes from being able to pass the entire image segment to imfilter() at once instead of performing redundant segmentation operations in a loop. It's also just easier to use.
I = imread('banana.jpg');
c = [551 440 561 688]; %colum
r = [151 244 367 280]; %row
BW = roipoly(I,c,r);
H = fspecial('disk',10);
J = roifilter(I,BW,H); % note the different syntax
imshow(J);
The output image is the same as before.
Although they do similar things conceptually, IPT roifilt2() and MIMT roifilter() are not interchangeable. See the help synopsis or webdocs for roifilter() for more information.

Tags

Community Treasure Hunt

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

Start Hunting!