Reputation: 5155
I have an NxMx3 matrix of unit8s, with each (x,y,:) being a vector of RGB values. I'm trying to do pixel doubling interpolation by simply copying the east and south neighboring values over by one pixel.
eastNeighbors = [img(2:end,:,:)];
southNeighbors = [img(:,2:end,:)];
filtered = img + eastNeighbors + southNeighbors;
The problem with this is that I get a "Array dimensions must match for binary array op." error.
How can I add a row and column of [0, 0, 0] elements to eastNeighbors and southNeighbors, respectively?
Edit: Actually, shouldn't there be a way to implement this with filter2? This is basically the FIR filter out(x,y) = in(x,y) + in(x-1, y-1), correct?
Tried this:
h = [[1 1] [1 1]];
filtered = filter2(h, img);
Got "Not enough input arguments." error.
Upvotes: 2
Views: 156
Reputation: 221574
This might be what you are after -
Approach 1: Using nlfilter
for sliding-neighborhood operations
fun1 = @(x) sum([x(2,2) x(3,2) x(2,3)]);
OUTPUT_IMG = nlfilter(INPUT_IMG, [3 3], fun1);%%// INPUT_IMG is a gray image
Approach 2: Using direct summations by padding zeros with padarray
to match matrix sizes
southNeighbors = padarray(INPUT_IMG(2:end,1:end),[1 0],'post');
eastNeighbors = padarray(INPUT_IMG(1:end,2:end),[0 1],'post');
OUTPUT_IMG = INPUT_IMG + eastNeighbors + southNeighbors;
General Notes:
If you are looking for interpolation
, maybe you would want to find mean of those three pixels, so divide by 3
. Take care of the uint8
datatype as it will clip off values at 255
when summing up.
Also, you might want to keep the last row and column same as in the original image, as they don't have both east and south neighboring pixels, so use this -
OUTPUT_IMG(:,end) = INPUT_IMG(:,end);
OUTPUT_IMG(end,:) = INPUT_IMG(end,:);
Upvotes: 3
Reputation: 4549
You could do it with convn, like this:
filtered = convn(img, [0 1 0; 1 1 0; 0 0 0]);
Upvotes: 1
Reputation: 617
To pad the images like you are interested in, you can use a combination of cat() and zeros() like so:
eastNeighbors = cat(2, zeros(size(img,1), 1, size(img,3)), img(:,2:end,:));
southNeighbors = cat(1, zeros(1, size(img,2), size(img,3)), img(2:end,:,:));
filtered = img + eastNeighbors + southNeighbors;
Upvotes: 1