Apply imfilter on specific regions (ROI) of RGB-image
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169731/image.jpeg)
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169732/image.jpeg)
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169734/image.jpeg)
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169735/image.jpeg)
1 comentario
Image Analyst
el 18 de Nov. de 2017
With no code, and no input and output images, and no definition of "suitable" we have no ability to answer this.
Respuestas (1)
DGM
el 11 de Mzo. de 2022
Editada: DGM
el 2 de Oct. de 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.
0 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!