Implementing Filter on an Image but avoiding the corners

9 views (last 30 days)
Hi,
I am trying to find the laplacian of a photo, but the corners of the image are blacked out (0's), and whenever I use imfilter it shows the difference between these corners and the actual image data as being very large, while the actual laplacian values of the image aren't as clear. How do I get around this?
  • Tried removing these from the image, but matrices must be rectangular, so I can't just "remove" those values
  • I have images at varying levels of exposure, so leaving the edges at 0 means the high exposure images have issues with the corners and setting the corner as 255 means the low exposure images have issues. Setting it in the middle means both ends have poor corner laplacian values
  • When using imfitler, there is a 'replicate' option, that replicates the image values around the ends of the image, is there anyway to take advantage of this when it comes to the corners? I feel like the solution could work somewhat similarly to that option.
Unsure how to proceed, any help is really appreciated!

Answers (3)

DGM
DGM on 6 Aug 2021
Edited: DGM on 7 Aug 2021
If you want to fill in the corner pixels like the 'replicate' edge treatment does with edges, you can do that by inpainting.
Consider the following examples using an oversize block of pixels to demonstrate the subtle differences:
If you can generate a binary mask representing the pixels you want to inpaint, you can then use inpaintCoherent()
% inpaint NW corner of image
inpict = imread('cameraman.tif');
mask = false(size(inpict));
mask(1:50,1:50) = 1;
A = inpaintCoherent(inpict,mask);
If you don't have a new enough version to have that, you can use inpaint_nans() from the File Exchange, and instead of using a binary mask, use a copy of the image with the corner pixels set to NaN.
% inpaint NW corner of image
inpict = imread('cameraman.tif');
mask = false(size(inpict));
mask(1:50,1:50) = 1;
nanpict = im2double(inpict);
nanpict(mask) = NaN;
B = inpaint_nans(nanpict,2);
Although inpaintCoherent() has more convenient usage, I don't really think it's nearly as useful as John's inpaint_nans().
That said, if we're only talking about single pixels, I doubt the difference matters. It would probably suffice to just fill them with the average of their neighbors. For this example, I'm just going to consider the same 50x50 block of pixels. You'd have to apply this to each corner.
% inpaint NW corner of image
inpict = imread('cameraman.tif');
mask = false(size(inpict));
mask(1:50,1:50) = 1;
% dilate mask to select the pixels adjacent to this region
neighbors = imdilate(mask,ones(2)) & ~mask;
C = inpict;
C(mask) = mean(C(neighbors));
  6 Comments
Ethan Levy
Ethan Levy on 10 Aug 2021
Sure, I think a max() would actually be a good idea! I will comment to the original post one of the least and highest exposure photos.
Again, I appreciate all the help!
Image Analyst
Image Analyst on 10 Aug 2021
@Ethan Levy, uh, okay. But when will you post the example images? It's been 3 hours and there is still no example image(s) attached.

Sign in to comment.


Image Analyst
Image Analyst on 10 Aug 2021
Ethan, you can make a mask of the dark portions and simply replace the output in the mask area with the original gray scale image pixels
outputImage(mask) = grayImage(mask);
Or if you want to smear the edge of the mask (i.e. good border pixels) through the mask to fill it in (inpaint it), you can use regionfill().
outputImage = regionfill(grayImage, mask);

Image Analyst
Image Analyst on 10 Aug 2021
Your corners could be dark due to lens shading or a filter such as a polarizer. It might help to brighten them up by computing the average radial profile. Then assuming the gray world assumption, fit the profile to a really smooth function from the center, like a quadratic. Then divide by that function to "flatten the image". It might work acceptably. Like DGM said, attach your image that you keep forgetting to attach if you want more help.
Attached are some canned demos, not related to your problem but just for computing the average radial profile in general. They'd need to be adapted.

Community Treasure Hunt

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

Start Hunting!