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:
imwrite from handle, how to

Subject: imwrite from handle, how to

From: Bruno

Date: 9 Feb, 2010 15:49:04

Message: 1 of 8

Hello,

consider this:

Iseg = getframe(ImageHandle);
imwrite(Iseg.cdata, [dialog.outputPath dialog.outputFilename '.png']);

ImageHandle is a handle to a figure ready to be saved, all calculations were done.
It was done with 'Visible' off, so all the drawings were not shown.
But when getframe( ) runs, the image appears in a figure window.

How to write the image associated to ImageHandle without showing it
in a Matlab figure?
or
How to use getframe( ) quietly, that is, not showing anything on the screen?
or
How to use imwrite directly with the handle? How could one obtain an indexed image
or an array to use with imwrite, from the ImageHandle?

Marchesi.

Subject: imwrite from handle, how to

From: Matthew Whitaker

Date: 9 Feb, 2010 18:43:04

Message: 2 of 8

"Bruno " <bruno.marchesi@gmail.com> wrote in message <hks05g$4g1$1@fred.mathworks.com>...
> Hello,
>
> consider this:
>
> Iseg = getframe(ImageHandle);
> imwrite(Iseg.cdata, [dialog.outputPath dialog.outputFilename '.png']);
>
> ImageHandle is a handle to a figure ready to be saved, all calculations were done.
> It was done with 'Visible' off, so all the drawings were not shown.
> But when getframe( ) runs, the image appears in a figure window.
>
> How to write the image associated to ImageHandle without showing it
> in a Matlab figure?
> or
> How to use getframe( ) quietly, that is, not showing anything on the screen?
> or
> How to use imwrite directly with the handle? How could one obtain an indexed image
> or an array to use with imwrite, from the ImageHandle?
>
> Marchesi.

Have you considred using the print command to save it.

imageHandle = figure('Visible','off');
axesHdl = axes('parent', imageHandle);
plot(axesHdl,sin(0:0.1:2*pi))
dialog.outputPath = cd;
dialog.outputFilename = 'test';
ext = '.png';
print(['-f',int2str(imageHandle)],'-dpng',fullfile(dialog.outputPath,[dialog.outputFilename,ext]))

Hope this helps
Matt W

Subject: imwrite from handle, how to

From: Bruno

Date: 10 Feb, 2010 11:22:03

Message: 3 of 8

Hello Matt,

your code worked like a charm, isolated and applied to my project as well. Thank you.

There's still an elf around here, though. Before the print( ), there's this plot( ):

plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 1)

Well, it has always worked. Now, all the line colors still do, *except* white, which
turns black. That is, when you see it onscreen, it's white. In the file, it's black. I've tried all of them: red, green, blue, etc, they all print as they should. Even black is printed as black.

Don't mean to be picky, but we need that white guy.

Any spell I could cast here??

Marchesi

Subject: imwrite from handle, how to

From: Steven Lord

Date: 10 Feb, 2010 14:17:23

Message: 4 of 8


"Bruno " <bruno.marchesi@gmail.com> wrote in message
news:hku4sr$94h$1@fred.mathworks.com...
> Hello Matt,
>
> your code worked like a charm, isolated and applied to my project as well.
> Thank you.
>
> There's still an elf around here, though. Before the print( ), there's
> this plot( ):
>
> plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 1)
>
> Well, it has always worked. Now, all the line colors still do, *except*
> white, which
> turns black. That is, when you see it onscreen, it's white. In the file,
> it's black. I've tried all of them: red, green, blue, etc, they all print
> as they should. Even black is printed as black.
>
> Don't mean to be picky, but we need that white guy.
>
> Any spell I could cast here??

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/figure_props.html#InvertHardcopy

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: imwrite from handle, how to

From: Bruno

Date: 10 Feb, 2010 15:07:04

Message: 5 of 8

It worked. Thank you.

was:

print(['-f',int2str(handleImage)],'-dpng',...
    [dialog.outputPath dialog.outputFilename '.png'])

is:

set(handleImage,'InvertHardcopy','off');
print(['-f',int2str(handleImage)],'-dpng',...
    [dialog.outputPath dialog.outputFilename '.png'])

Prints to file white, black and other line colors ok (as they were in the figure).

Marchesi

Subject: imwrite from handle, how to

From: Bruno

Date: 10 Feb, 2010 16:22:08

Message: 6 of 8

Update: it worked at development. At deployment, there was a problem.

At the machine where Matlab is installed,
the .exe runs with Matlab opened and closed as well.

At the deployment machine,
this is the first time the .exe misbehaved,
the new statement is:

set(handleImage,'InvertHardcopy','off'); % new statement
print(['-f',int2str(handleImag)],'-dpng',... % this was ok
    [dialog.outputPath dialog.outputFilename '.png'])


At the deployment machine, the .exe gives the following dump.

What should be done?

Marchesi

--------------------------------------------------------------------------------- v ----dump start
Warning: Problems in UIW_SetUpGLPrinting
> In graphics\private\render at 143
  In print at 277
  In posproc003b at 39
  In rodando at 62
??? Error using ==> print at 325
Bad data returned by HARDCOPY. Not calling IMWRITE.

Error in ==> posproc003b at 39



Error in ==> rodando at 62



MATLAB:render:badData
Warning: 1 invisible figure(s) exist at MCR termination.

If your application has terminated unexpectedly, please note that
 applications generated by the MATLAB Compiler terminate when there are no
 visible figure windows. See the documentation for WaitForFiguresToDie and
 WAITFORCALLBACKS for more information.

Warning: Class
    'graph2d.lineseries'
in use at MCR termination.

If your application has terminated unexpectedly, please note that
 applications generated by the MATLAB Compiler terminate when there are no
 visible figure windows. See the documentation for WaitForFiguresToDie and
 WAITFORCALLBACKS for more information.
--------------------------------------------------------------------------------- ^ ----dump end

Subject: imwrite from handle, how to

From: Matthew Whitaker

Date: 19 Feb, 2010 16:08:05

Message: 7 of 8

"Bruno " <bruno.marchesi@gmail.com> wrote in message <hkumfg$4nb$1@fred.mathworks.com>...
> Update: it worked at development. At deployment, there was a problem.
>
> At the machine where Matlab is installed,
> the .exe runs with Matlab opened and closed as well.
>
> At the deployment machine,
> this is the first time the .exe misbehaved,
> the new statement is:
>
> set(handleImage,'InvertHardcopy','off'); % new statement
> print(['-f',int2str(handleImag)],'-dpng',... % this was ok
> [dialog.outputPath dialog.outputFilename '.png'])
>
>
> At the deployment machine, the .exe gives the following dump.
>
> What should be done?
>
> Marchesi
>
> --------------------------------------------------------------------------------- v ----dump start
> Warning: Problems in UIW_SetUpGLPrinting
> > In graphics\private\render at 143
> In print at 277
> In posproc003b at 39
> In rodando at 62
> ??? Error using ==> print at 325
> Bad data returned by HARDCOPY. Not calling IMWRITE.
>
> Error in ==> posproc003b at 39
>
>
>
> Error in ==> rodando at 62
>
>
>
> MATLAB:render:badData
> Warning: 1 invisible figure(s) exist at MCR termination.
>
> If your application has terminated unexpectedly, please note that
> applications generated by the MATLAB Compiler terminate when there are no
> visible figure windows. See the documentation for WaitForFiguresToDie and
> WAITFORCALLBACKS for more information.
>
> Warning: Class
> 'graph2d.lineseries'
> in use at MCR termination.
>
> If your application has terminated unexpectedly, please note that
> applications generated by the MATLAB Compiler terminate when there are no
> visible figure windows. See the documentation for WaitForFiguresToDie and
> WAITFORCALLBACKS for more information.
> --------------------------------------------------------------------------------- ^ ----dump end

For deployed applications you have to use deployprint

Subject: imwrite from handle, how to

From: Bruno

Date: 23 Feb, 2010 17:24:05

Message: 8 of 8


> For deployed applications you have to use deployprint

Thank you. Indeed, that is the most apropriated function. However, it's still nogo.
Same error:

Warning: Problems in UIW_SetUpGLPrinting
> > > In graphics\private\render at 143
> > In print at 277
> > In posproc003b at 39
> > In rodando at 62
> > ??? Error using ==> print at 325
> > Bad data returned by HARDCOPY. Not calling IMWRITE.

UIW_SetUpGLPrinting has appeared some times at the newsgroup, apparently
with no solution. I decided to use getframe( ). Whilst it works at the
deploy machine (industrial PC), it has two disadvantages. Even with
set(gcf,'Visible','off');
the image is forced to appear during the capture. Secondly, if some 'always on top'
application is running, the capture can be jammed with it. Like task manager and so.

getframe(), print() and deployprint() all work in the machine with Matlab installed.
Only getframe() work at the deploy machine. I've tried a 'repair' in .net framework
and MCR installer at the deploy machine, but the code still bugs.

Marchesi

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