Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to do Griddata only within bounds of original image?

Subject: how to do Griddata only within bounds of original image?

From: James Ramm

Date: 16 Oct, 2010 19:09:04

Message: 1 of 4

I have an image which looks some thing like this:

----------------------------------------------------------------------------------------------> X
| CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
| CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
| CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
| CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
| CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
| CCCCCCCCCCCCCCCCCCCCCCCCCCCC
| CCCCCCCCCCCCCCCCCCCCCCCC
| CCCCCCCCCCCCCCCCCCCC
|
Y

Where C would be colour data. I wish to interpolate the data to a finer grid, but whilst staying within the current bounds of the colour data (thus preserving the shape).
Doing:

xl = linspace(min(X),max(X),res);
yli = linspace(min(Y),max(Y),res);
[XI YI] = meshgrid(xl,yl);
CI = griddata(X,Y,C,XI,YI);

Procduces a rectangular grid so that the resulting image looks like:

------------------------------------------------------------------------------------->XI
| CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI
| CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI
| CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI
| CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI
| CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI
| CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI
| CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI
| CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI CI
|
YI

One solution could be to 'NaN-out' unwanted values by using the original x,y grid, but I am unsure of how to do this and wondered if there is a more elegant solution?

Subject: how to do Griddata only within bounds of original image?

From: ImageAnalyst

Date: 17 Oct, 2010 19:05:07

Message: 2 of 4

Just mask it out, something like this
mask = cast(originalImage>0, class(originalImage));
maskedImage = originalImage .* mask;

Subject: how to do Griddata only within bounds of original image?

From: James Ramm

Date: 18 Oct, 2010 14:14:05

Message: 3 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c7997fbf-8b7d-41d7-8a5a-e8f0a87e7499@g8g2000yqa.googlegroups.com>...
> Just mask it out, something like this
> mask = cast(originalImage>0, class(originalImage));
> maskedImage = originalImage .* mask;

Hi
By orginalImage I assume you mean the (original) colour data. This would not work as 'originalImage' is always > 0. My point is that the original image is not a rectangular grid with NaN or 0 where there are no data (thus giving it its shape). It is 3 vectors, x,y,z defining only those points where there are data and then reshaped into matrices that are plotted with pcolor.
I wish to then resample this image to a finer grid, whilst remaining inside the bounds.
The only way I know how to resample in matlab would be to create a grid using meshgrid on finely spaced vectors between min(x,y) and max(x,y). This of course will then give a rectangular grid which results in a image going outside the bounds of my original image (after using griddata).
Its a problem I simply cant figure out. Am grateful for help!

Subject: how to do Griddata only within bounds of original image?

From: Chris

Date: 24 Nov, 2010 03:53:04

Message: 4 of 4

Hi James,

I found this worked for a similar problem when using the 'nearest' method, it may work for other interpolations as well:

1. edit or make a copy of the griddata.m file
2. at the end of the function 'nearest', add the following lines (or similar)

in = dt.pointLocation(xi,yi);
zi(isnan(in)) = nan;

The pointLocation function is the key, it basically detects whether a point is inside or outside the triangulation of the input points.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us