How can I save a patch as an image?

13 views (last 30 days)
Thomas
Thomas on 13 Nov 2013
Commented: Walter Roberson on 13 Nov 2013
I'm working on a project that evolves a population of polygons using differential evolution to approximate an image. I have an objective function, or "fitness function" that I'm minimizing based on the difference between the original image and the image approximated using polygons at each pixel of the image. The problem I am encountering occurs when I try to save the patch I create as an MxNx3 matrix representing the image.
function [fitness]=fitnessFunction5(dnaPolygonCollection,y)%,generatedImage)
%fitnessFunction(sourceImage,generatedImage)
%a function used to calculate the fit of an approximated image
global sourceImage numVertices numPolygons;
[imgY,imgX,numberOfColorChannelsA] = size(sourceImage);
generatedImage=...
internalDrawRealPolygonImage(dnaPolygonCollection, imgX, imgY,...
numVertices, numPolygons);
deltaRGB=(abs(double(sourceImage)-double(generatedImage)));
fitness=sum(sum(sum(deltaRGB)));
end
function [ newImage ]=...
internalDrawRealPolygonImage( dnaPolygonCollection, imgX, imgY,...
numVertices, numPolygons )
resizedDNA=reshape(dnaPolygonCollection,(4+(2*numVertices)),numPolygons);
resizedDNA=resizedDNA';
XVertices=zeros(numPolygons,numVertices);
YVertices=zeros(numPolygons,numVertices);
colorData=floor(resizedDNA(:,1:3)*255);
colorData2=resizedDNA(:,1:3);
alphaData=resizedDNA(:,4);
XVertices=floor(resizedDNA(:,5:2:end)*imgX);
YVertices=floor(resizedDNA(:,6:2:end)*imgY);
XVertices=XVertices';
YVertices=YVertices';
p=patch('XData',XVertices,'YData',YVertices,...
'FaceColor','flat','FaceVertexCData',colorData2,...
'AlphaDataMapping','none','FaceVertexAlphaData',alphaData,'EdgeAlpha',0);
I = getframe(gcf);
newImage=imresize(I.cdata, [imgY,imgX]);
delete(p);
cla;
end
the image created by saving the color information from the call to getframe(gcf) does not change, so the fitness value never changes. Is there something wrong with the way I am attempting to retrieve the color information from the current figure? Why is the image not updated?
  1 Comment
Walter Roberson
Walter Roberson on 13 Nov 2013
Experiment with putting a drawnow() before the getframe(). It should not be necessary in theory but in practice sometimes it is. Sometimes it is not long enough and you need to pause() a short bit instead.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!