Movie redraws contour lines - image

I made a contourfm plot with white contour lines.
I used 'LineColor', 'white' but in a movie, the white lines propagate after each image instead of redrawing. The colors on the image itself redraws automatically. What am I doing wrong?
Also, when viewing the image, it shows up small on the top left hand corner. A similar script before did not cause this issue. Anyone know why?
% Create movie
nFrames = 893; % Number of frames
for k = 1:nFrames % Create movie
% Eqdconic script
% Define figure and axes
fg1 = figure(1);
axesm('MapProjection','eqdconic', 'MapParallels', [], 'MapLatLimit',[-79.625 -59.625],'MapLonLimit',[190.625 250.625])
framem on; gridm on; mlabel on; plabel on; hold all;
% Plot data
frame = dataSST_movie(:,:,k);
image = contourfm(Y,X,frame, 'LineColor', 'white'); % Change contour lines to white
mov(k) = getframe(gcf);
end
close(gcf)
% % Save as AVI file
movie2avi(mov, 'SST_20110101to20130611_0.25grid.avi', 'compression', 'none', 'fps', 4);
The image = contourfm() part is what I'm talking about; is there some command like redraw that I need to use?

Just use clf before end. As for the size issue, check MapLatLimit and MapLonLimit. If they are correct, it should work.

Related

PIL: Imageobject.save() after drawing completely corrupts images and smurfs the ouput

I have these two functions in my program:
def depict_ph_increase(x,y,color, imobject):
program_print(color)
draw = PIL.ImageDraw.Draw(imobject)
draw.text((x, y),color,(255,255,255))
imobject.save('tmp-out.gif')
im_temp = PIL.Image.open("tmp-out.gif")#.convert2byte()
im_temp = im_temp.resize((930, 340), PIL.Image.ANTIALIAS)
MAP_temp = ImageTk.PhotoImage(im_temp)
map_display_temp = Label(main, image=MAP_temp)
map_display_temp.image = MAP_temp # keep a reference!
map_display_temp.grid(row=4,column=2, columnspan=3)
def read_temp_pixels(temperature_file, rngup, rngdown):
temp_image_object = PIL.Image.open(temperature_file)
(length, width) = get_image_size(temp_image_object)
(rngxleft, rngxright) = rngup
(rngyup,rngydown) = rngdown
print 'the length and width is'
print length, width
hotspots = 5;
for hotspot in range(0,hotspots):
color = "#ffffff"
while color == "#ffffff" or color == "#000000" or color == "#505050" or color == "#969696":
yc = random.randint(rngxleft, rngxright)
xc = random.randint(rngyup,rngydown)
color = convert_RGB_HEX(get_pixel_color(temp_image_object, xc, yc))
depict_ph_increase(xc,yc,color, temp_image_object)
The bottom one calls the top one. Their job is to read in this image:
It then randomly selects a few pixels, grabs their colors, and writes the hex values of the colors on top. But, when it redisplays the image, it gives me this garbage:
Those white numbers up near the upper right corner are the hex values its drawing. Its somehow reading the values from the corrupted image, despite the fact that I don't collect the values until AFTER I actually call the ImageDraw() method. Can someone explain to me why it is corrupting the image?
Some background--the get_pixel_color() function is used several other times in the program and is highly accurate, its just reading the pixel data from the newly corrupted image somehow. Furthermore, I do similar image reading (but not writing) at other points in my code.
If there is anything I can clarify, or any other part of my code you want to see, please let me know. You can also view the program in its entirety at my github here: https://github.com/jrfarah/coral/blob/master/src/realtime.py It should be commit #29.
Other SO questions I have examined, to no avail: Corrupted image is being saved with PIL
Any help would be greatly appreciated!
I fixed the problem by editing this line:
temp_image_object = PIL.Image.open(temperature_file)
to be
temp_image_object = PIL.Image.open(temperature_file).convert('RGB')

Define 2 plotted lines in an image as variables inside a function in Matlab?

Function [Yupper, Ylower] = Segmentation(A)
A=imread('g16.BMP');
AR=A(:,:,1);
[rows, columns] = size(AR);
avgs = mean(AR(50:165,:), 2);
avgs2 = mean(AR(165:315,:), 2);
[~,ind]= max(abs(diff(avgs)));
[~,ind2]= max(abs(diff(avgs2)));
figure, image(AR,'CDataMapping','scaled'); colormap('gray'); hold on;
Yupper=plot([1 size(AR,2)], [ind+50 ind+50], 'r', 'LineWidth', 2);
Ylower=plot([1 size(AR,2)], [ind2+165 ind2+165], 'g', 'LineWidth', 2);
end
Above is code used to segment grey scale images based on large instensity changes,I'm looking to declare the 2 plotted lines as variables so I can carry out future processing. However, If I type in whos , the lines appear as follows in the table;
YL 1x1 112 matlab.graphics.chart.primitive.Line
YU 1x1 112 matlab.graphics.chart.primitive.Line
if I remove the the plot commands I receive errors associated with brackets etc and if I manipulate the code any further it seems to mess up the correctly plotted data, can anyone help?
If what you want is to delete specific data for a figure you can do it accessing the Xdata and Ydata properties of the figure handle. You can do it in Matlab R2014a or older by:
set(YL,'XData',[])
set(YL,'YData',[])
or in Matlab R2014b or newer by:
YL.XData=[];
YL.YData=[];
Example in R2013b:
clear;clc;
y=rand(20,1)*20;
x=1:20;
img=rand(20,20);
figure
hold on
image(img,'CDataMapping','scaled'); colormap('gray');
hp=plot(x,y);
% emulating your code delay
pause(2);
set(hp,'XData',[])
set(hp,'YData',[])

Writing a greyscale video using Videowriter/avifile

I am writing a function that generates a movie mimicking a particle in a fluid. The movie is coloured and I would like to generate a grayscaled movie for the start. Right now I am using avifile instead of videowriter. Any help on changing this code to get grayscale movie? Thanks in advance.
close all;
clear variables;
colormap('gray');
vidObj=avifile('movie.avi');
for i=1:N
[nx,ny]=coordinates(Lx,Ly,Nx,Ny,[x(i),-y(i)]);
[xf,yf]=ndgrid(nx,ny);
zf=zeros(size(xf))+z(i);
% generate a frame here
[E,H]=nfmie(an,bn,xf,yf,zf,rad,ns,nm,lambda,tf_flag,cc_flag);
Ecc=sqrt(real(E(:,:,1)).^2+real(E(:,:,2)).^2+real(E(:,:,3)).^2+imag(E(:,:,1)).^2+imag(E(:,:,2)).^2+imag(E(:,:,3)).^2);
clf
imagesc(nx/rad,ny/rad,Ecc);
writetif(Ecc,i);
if i==1
cl=caxis;
else
caxis(cl)
end
axis image;
axis off;
frame=getframe(gca);
cdata_size = size(frame.cdata);
data = uint8(zeros(ceil(cdata_size(1)/4)*4,ceil(cdata_size(2)/4)*4,3));
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
frame.cdata = data;
vidObj = addframe(vidObj,frame);
end
vidObj = close(vidObj);
For your frame data, use rgb2gray to convert a colour frame into its grayscale counterpart. As such, change this line:
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
To these two lines:
frameGray = rgb2gray(frame.cdata);
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = ...
cat(3,frameGray,frameGray,frameGray);
The first line of the new code will convert your colour frame into a single channel grayscale image. In colour, grayscale images have all of the same values for all of the channels, which is why for the second line, cat(3,frameGray,frameGray,frameGray); is being called. This stacks three copies of the grayscale image on top of each other as a 3D matrix and you can then write this frame to your file.
You need to do this stacking because when writing a frame to file using VideoWriter, the frame must be colour (a.k.a. a 3D matrix). As such, the only workaround you have if you want to write a grayscale frame to the file is to replicate the grayscale image into each of the red, green and blue channels to create its colour equivalent.
BTW, cdata_size(3) will always be 3, as getframe's cdata structure always returns a 3D matrix.
Good luck!

Print image to pdf without margin using Matlab

I'm trying to use the answers I found in these questions:
How to save a plot into a PDF file without a large margin around
Get rid of the white space around matlab figure's pdf output
External source
to print a matlab plot to pdf without having the white margins included.
However using this code:
function saveTightFigure( h, outfilename, orientation )
% SAVETIGHTFIGURE(H,OUTFILENAME) Saves figure H in file OUTFILENAME without
% the white space around it.
%
% by ``a grad student"
% http://tipstrickshowtos.blogspot.com/2010/08/how-to-get-rid-of-white-margin-in.html
% get the current axes
ax = get(h, 'CurrentAxes');
% make it tight
ti = get(ax,'TightInset');
set(ax,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);
% adjust the papersize
set(ax,'units','centimeters');
pos = get(ax,'Position');
ti = get(ax,'TightInset');
set(h, 'PaperUnits','centimeters');
set(h, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
set(h, 'PaperPositionMode', 'manual');
set(h, 'PaperPosition',[0 0 pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
% save it
%saveas(h,outfilename);
if( orientation == 1)
orient portrait
else
orient landscape
end
print( '-dpdf', outfilename );
end
Results in this output:
As you can see the 'PaperSize' seems to be set not properly. Any idea of possible fixes?
NOTE
If I change the orientation between landscape and portrait the result is the same, simply the image is chopped in a different way.
However if I save the image with the saveas(h,outfilename); instruction the correct output is produced.
Why is this? And what is the difference between the two saving instructions?
Alltogether the answers you mentioned offer a lot of approaches, but most of them didn't worked for me neither. Most of them screw up your papersize when you want to get the tight inset, the only which worked for me was:
set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
I finally wrote a function, where I specify the exact height and width of the output figure on paper, and the margin I want (or just set it to zero). Be aware that you also need to pass the axis handle. Maybe this functions works for you also.
function saveFigure( fig_handle, axes_handle, name , height , width , margin)
set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
set(fig_handle, 'Units','centimeters','PaperUnits','centimeters')
% the last two parameters of 'Position' define the figure size
set(fig_handle,'Position',[-margin -margin width height],...
'PaperPosition',[0 0 width+margin height+margin],...
'PaperSize',[width+margin height+margin],...
'PaperPositionMode','auto',...
'InvertHardcopy', 'on',...
'Renderer','painters'... %recommended if there are no alphamaps
);
saveas(fig_handle,name,'pdf')
end
Edit: if you use painters as renderer saveas and print should produce similar results. For jpegs print is preferable as you can specify the resolution.

How can I modify an image without showing it?

I have a code that loads an image to a plot, draws a rectangle on it an after this saves the image into a png file:
figure('Visible', 'off');
imshow(im)
hold on
for n=1:size(windowCandidates,1)
rectangle('Position',[x,y,w,h],'EdgeColor','g','LineWidth',2)
end
f=getframe;
[img_bound,map]=frame2im(f);
imwrite(img_bound, strcat(directory, 'name.', 'png'));
hold off
How can I do the same without displaying it in a figure? Just modifying it and saving, I dont want the user to see all this process)
Thanks!
You can make a figure invisible with:
figure('Visible', 'off');
And then just write it out as Matlab fig via:
saveas(gcf, 'path/to/filename');
or using the print command to png is this case
print('-dpng', 'path/to/filename');
Similar question with good answers and explanations else where on stackoverflow
Update
Thanks to Steve for pointing to this undocumented matlab function
function so;
close all;
im = imread('cameraman.tif');
hfig = figure('Visible', 'off'), imshow(im, 'Border', 'tight');
for n=1:2
rectangle('Position', [20*n, 20*n, 50, 50], 'EdgeColor', 'g', 'LineWidth', 2)
hold on;
end
F = im2frame(zbuffer_cdata(gcf));
imwrite(F.cdata, 'test.png');
% Function copied from
% http://www.mathworks.com/support/solutions/en/data/1-3NMHJ5/?solution=1
% -3NMHJ5
%
function cdata = zbuffer_cdata(hfig)
% Get CDATA from hardcopy using zbuffer
% Need to have PaperPositionMode be auto
orig_mode = get(hfig, 'PaperPositionMode');
set(hfig, 'PaperPositionMode', 'auto');
cdata = hardcopy(hfig, '-Dzbuffer', '-r0');
% Restore figure to original state
set(hfig, 'PaperPositionMode', orig_mode);

Resources