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:
extracting values from a fit result

Subject: extracting values from a fit result

From: Dave Brackett

Date: 3 Dec, 2009 14:33:19

Message: 1 of 5

Hi,

I have a bitmap containing just black and white pixels. Where a pixel is black I want to extact a fitted surface value at that coordinate. I want it to ignore where pixels are white. The value of the black pixel in the matrix is replaced each time with the extracted fitted surface value.

The below method currently runs very slowly. Is there a more efficient way?

for i=1:603 % for each pixel along axis 1
    for k=1:216 % for each pixel along axis 2
        if bmp(i,k,1)==0 % if the pixel is black
            bmp(i,k,1)=fitresult(k,i); % extract corresponding value from fitted surface
        end
    end
end

Where fitresult was created like this using an exported surface fit after using sftool
[fitresult, gof] = createSurfaceFit(x_coords, y_coords, z_coords)

Thanks for reading.

Subject: extracting values from a fit result

From: Cokelid

Date: 3 Dec, 2009 15:04:10

Message: 2 of 5

On Dec 3, 9:33 am, "Dave Brackett" <davebrack...@hotmail.com> wrote:
> Hi,
>
> I have a bitmap containing just black and white pixels. Where a pixel is black I want to extact a fitted surface value at that coordinate. I want it to ignore where pixels are white. The value of the black pixel in the matrix is replaced each time with the extracted fitted surface value.
>
> The below method currently runs very slowly. Is there a more efficient way?
>
> for i=1:603 % for each pixel along axis 1
>     for k=1:216 % for each pixel along axis 2
>         if bmp(i,k,1)==0 % if the pixel is black
>             bmp(i,k,1)=fitresult(k,i); % extract corresponding value from fitted surface
>         end
>     end
> end
>
> Where fitresult was created like this using an exported surface fit after using sftool
> [fitresult, gof] = createSurfaceFit(x_coords, y_coords, z_coords)
>
> Thanks for reading.

You access bmp as (i,k) and fitresult as (k,i) which suggests you need
to transpose one or the other so they're both the same shape. Once
you've done that it's trivial:

trans_fit = fitresult.';
is_black = bmp==0;
bmp(is_black)=trans_fit(is_black);

Justin

Subject: extracting values from a fit result

From: Dave Brackett

Date: 3 Dec, 2009 17:15:29

Message: 3 of 5

Cokelid <cokelid@googlemail.com> wrote in message <5186331c-d12f-41fe-a4dd-86f347077d42@e20g2000vbb.googlegroups.com>...
> On Dec 3, 9:33?am, "Dave Brackett" <davebrack...@hotmail.com> wrote:
> > Hi,
> >
> > I have a bitmap containing just black and white pixels. Where a pixel is black I want to extact a fitted surface value at that coordinate. I want it to ignore where pixels are white. The value of the black pixel in the matrix is replaced each time with the extracted fitted surface value.
> >
> > The below method currently runs very slowly. Is there a more efficient way?
> >
> > for i=1:603 % for each pixel along axis 1
> > ? ? for k=1:216 % for each pixel along axis 2
> > ? ? ? ? if bmp(i,k,1)==0 % if the pixel is black
> > ? ? ? ? ? ? bmp(i,k,1)=fitresult(k,i); % extract corresponding value from fitted surface
> > ? ? ? ? end
> > ? ? end
> > end
> >
> > Where fitresult was created like this using an exported surface fit after using sftool
> > [fitresult, gof] = createSurfaceFit(x_coords, y_coords, z_coords)
> >
> > Thanks for reading.
>
> You access bmp as (i,k) and fitresult as (k,i) which suggests you need
> to transpose one or the other so they're both the same shape. Once
> you've done that it's trivial:
>
> trans_fit = fitresult.';
> is_black = bmp==0;
> bmp(is_black)=trans_fit(is_black);
>
> Justin


Hi Justin, thanks for your reply.

fitresult is effectively a function which needs the x and y coordinates as input. using your steps returns this error:
"To evaluate an SFIT object you must specify either two arguments, or one argument with two columns."

Are you able to help any further with this please?

Subject: extracting values from a fit result

From: ImageAnalyst

Date: 3 Dec, 2009 18:15:49

Message: 4 of 5

Just use the find function as in this simple example:

a=magic(5)
[x_coords, y_coords] = find(a>15)

so I think you'd have
[x_coords, y_coords, z_coords] = find(bmp==0)

Subject: extracting values from a fit result

From: Tom Lane

Date: 3 Dec, 2009 21:23:46

Message: 5 of 5

>> > The below method currently runs very slowly. Is there a more efficient
>> > way?
>> >
>> > for i=1:603 % for each pixel along axis 1
>> > ? ? for k=1:216 % for each pixel along axis 2
>> > ? ? ? ? if bmp(i,k,1)==0 % if the pixel is black
>> > ? ? ? ? ? ? bmp(i,k,1)=fitresult(k,i); % extract corresponding value
>> > from fitted surface
>> > ? ? ? ? end
>> > ? ? end
>> > end

Dave, take a look at this. It avoids looping and does just one call to the
function that evaluates the fitted surface.

% display of original bmp array
figure(1)
bmp = blkdiag(ones(10),ones(20,30),ones(15));
h = image(bmp);
set(h,'cdatamapping','scaled')

% update parts of it from fitted surface
x = 55*rand(200,1); y = 45*rand(200,1); z = x/80 + y/500;
f = fit([x y],z,'lowess');
[X,Y] = find(bmp==0);
S = f(X,Y);
bmp(bmp==0) = S;

% display updated bmp array
figure(2)
h = image(bmp);
set(h,'cdatamapping','scaled')

-- Tom

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