How can I filter region of interest (ROI) selected in a 3 d array image (RGB) without converting to grayscale?

조회 수: 10(최근 30일)
  • Create a filter or mask for a selected region in High Dynamic Range images (HDRI).
  • Using the example for ROI in grayscale images, create filter ROI for 3 d array images. The HDRI file cannot loose the properties. Is there a workaround?
*1.)Code used for filtering sample grayscale example: * img = imread('pout.tif');
h_im = imshow(img);
e = imellipse(gca,[55 10 120 120]);
BW = createMask(e,h_im);
h = fspecial('unsharp');
I2 = roifilt2(h,img,BW);
figure, imshow(I2);
2.*)Stuck in the solution for filtering sample HDRI example? * P = hdrread('HDR');
imshow(P);
R = tonemap(P);
figure, imshow®;
whos
imageSizeX = 3000;
imageSizeY = 1500;
imageSizeZ = 3;
centerX = 1500;
centerY= 750;
centerZ= 1.5;
[columnsInImage rowsInImage pagesInImage] = meshgrid(1:imageSizeX, 1:imageSizeY,1:imageSizeZ);
radius = 300; sphereVoxels = (rowsInImage - centerY).^2 ... + (columnsInImage - centerX).^2 + (pagesInImage - centerZ).^2 <= radius.^2;
fv = isosurface(sphereVoxels,0);
patch(fv,'FaceColor',[0 0 .7],'EdgeColor',[0 0 1]);
figure, view(90,90);
axis equal;
How do I proceed with the next step or is there a mistake in-between??? Apologize limited understand. Hope someone can help.. Thanks

채택된 답변

Image Analyst
Image Analyst 2012년 8월 24일
I don't understand your #2. Does 3D mean color or volumetric? And what does "not lose properties" mean? A filtered image will have different properties than the input image - at least SOME of them must be different or else the filter didn't do anything. And I have no idea what all that isosurface and patch stuff is for. What does that have to do with filtering color or volumetric images?
  댓글 수: 5
vidya
vidya 2012년 9월 6일
:) thanks a lot for taking time out. Ill surely try this and let you know if it works or post back, if there is any error..

댓글을 달려면 로그인하십시오.

추가 답변(1개)

DGM
DGM 2022년 11월 11일
편집: DGM 2022년 11월 11일
If you're just filtering RGB images, there's no reason to jump through any special hoops. There are a number of ways this can be done.
All of the following examples work by creating a filtered copy of the image and then they use the specified mask to compose the output from the filtered copy and the source image.
Let's say we have this image and mask:
Full-frame processing
We could filter the image in full and composite the two copies. This approach supports a lot of flexibility.
  • The base image could be grayscale (I) or color (RGB)
  • The filtered image could be I/RGB
  • The mask could be hard (binarized) or soft (antialiased or graduated)
  • Could use any filter function that will work on the image
% FILTER AND COMPOSITE LIKE A CAVEMAN
inpict = imread('peppers.png'); % RGB, uint8
mk = imread('sources/standardmods/pep/pepcircmask.png'); % BW, logical
mk = imgaussfilt(im2double(mk),10); % now it's a soft mask (class 'double')
% apply filter to create foreground image
fk = fspecial('disk',21);
FG = imfilter(inpict,fk);
% compose the output the clumsy fragile way
% everything must be floating-point
mk = im2double(mk); % mask must be unit-scale
FG = im2double(FG);
inpict = im2double(inpict);
outpict = im2uint8(mk.*FG + (1-mk).*inpict);
imshow(outpict)
That works and it's flexible, but there is a drawback. If the mask area is small compared to the image area, a lot of computations get discarded in the regions where the mask is fully transparent. This waste can be problematic if the filter function is expensive or if speed is important.
Local processing: IPT roifilt2()
One solution is to find a rectangle which circumscribes the regions defined by the mask. This subregion of the image can be extracted, filtered, composited, and inserted back into the source image. Depending on the relative geometries of the mask blob(s) and the mask itself, a lot of time can be saved by only filtering what's needed. There are a couple ways to do this.
You could use IPT roifilt2() as the OP's example. Depending on the filter function that's needed, this can be applied to RGB images. There are limitations.
  • The base image can only be grayscale. RGB inputs must be done in a loop if possible.
  • The filtered image can only be grayscale.
  • The mask can only be binarized. (All masks are binarized internally)
  • The filter function cannot require RGB input.
% FILTER USING IPT ROIFILT2()
inpict = imread('peppers.png'); % RGB, uint8
mk = imread('sources/standardmods/pep/pepcircmask.png'); % BW, logical
% a filter kernel
fk = fspecial('disk',21);
outpict = zeros(size(inpict),class(inpict));
for c = 1:size(inpict,3)
outpict(:,:,c) = roifilt2(fk,inpict(:,:,c),mk);
end
imshow(outpict)
While roifilt2() can be used on an RGB image, the inability to pass the entire RGB segment to the filter function limits what filter functions can be used, and in the case of most 2D spatial filters, tripling the overhead costs will degrade any speed advantage gained.
Local processing: MIMT roifilter()
MIMT roifilter() is similar to IPT roifilt2(), but more flexible, useful, and often faster. Much like the first example using basic compositing, roifilter() is relatively unrestricted.
  • The base image could be grayscale (I) or color (RGB)
  • The filtered image could be I/RGB
  • The mask could be hard (binarized) or soft (antialiased or graduated)
  • The filter function does not have to support RGB inputs, even if RGB inputs are used
  • Supports blobwise operation, which can be faster with some masks
  • Supports composition in sRGB or linear RGB
  • Supports shorthand for gaussian mean filters (no separate kernel required)
Let's recreate what we did with IPT roifilt2().
% FILTER USING MIMT ROIFILTER() (hard mask)
inpict = imread('peppers.png'); % RGB, uint8
mk = imread('sources/standardmods/pep/pepcircmask.png'); % BW, logical
% a filter kernel
fk = fspecial('disk',21);
% filter and compose in sRGB
outpict = roifilter(inpict,mk,fk);
imshow(outpict)
How about doing something that roifilt2() can't do? The soft-masked example from before is a lot more succinct now, and it's faster (depending on the mask).
% FILTER USING MIMT ROIFILTER() (soft mask)
inpict = imread('peppers.png'); % RGB, uint8
mk = imread('sources/standardmods/pep/pepcircmask.png'); % BW, logical
mk = imgaussfilt(im2double(mk),10); % now it's a soft mask
% a filter kernel
fk = fspecial('disk',21);
% filter and compose in sRGB
outpict = roifilter(inpict,mk,fk);
imshow(outpict)
How about something else that roifilt2() can't do? While roifilt2() can process an RGB image channelwise, that won't work if the filter function requires access to the entire RGB image segment. A good example is local color adjustment.
% FILTER USING MIMT ROIFILTER() (do something that cannot be done in a loop with roifilt2())
inpict = imread('peppers.png'); % RGB, uint8
mk = imread('sources/standardmods/pep/pepcircmask.png'); % BW, logical
mk = imgaussfilt(im2double(mk),10); % now it's a soft mask
% a function handle that requires the entire RGB segment
F = @(x) imtweak(x,'lchab',[1 1 -0.20]); % also MIMT
% filter and compose in linear RGB
outpict = roifilter(inpict,mk,F,'linear');
imshow(outpict)
Making full-frame processing more convenient
Let's take a step back to the first example of full-frame compositing. While a full-frame approach can be wasteful, it's still very flexible. There should be no issues using an RGB-only filter function as in the last roifilter() example. That said, the cumbersome and verbose manual compositing could be done a lot better. If we're using MIMT tools, doing better is very easy.
% FILTER AND COMPOSITE
inpict = imread('peppers.png'); % RGB, uint8
mk = imread('sources/standardmods/pep/pepcircmask.png'); % BW, logical
mk = imgaussfilt(im2double(mk),10); % now it's a soft mask
% create foreground
FG = imtweak(inpict,'lchab',[1 1 -0.20]);
% compose in linear RGB
outpict = replacepixels(FG,inpict,mk,'linear'); % no need to safeguard input class
imshow(outpict)
MIMT replacepixels is convenient, flexible and supports compositing in sRGB and linear RGB, just like roifilter().
While it's probably not intended, the OP's example attempts the use of a volumetric (ellipsoidal) mask for doing the ROI selection in RGB. While compositing with RGB masks (or RGBAAA images) is atypical, some MIMT tools do support it. Unfortunately, roifilter() doesn't. If that's actually needed, a full-frame composition with replacepixels is really no more complicated than with a 2D mask.
Let's say that this is our mask. Each blob grows progressively larger as we traverse the page axis of the array. If it helps to describe it in terms of primitive solids, we could say that they're intersecting approximations of cones.
Using the same two adjustment and compositing lines as in the prior example, we get this.
If you're trying to figure out what you're looking at, don't be too surprised. RGB-masked compositions aren't typically very intuitive. An ellipsoidal mask would also produce results that are probably unexpected.
For other examples on masked blurring, see the links in this answer:

Community Treasure Hunt

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

Start Hunting!

Translated by