How to emphase some region on a spherical surface - matlab

Introduction
I'm trying to emphase some region on a spherical surface saying that this region should be colored as not transparent (alpha = 1.0) and other parts of the sphere should be colored as semi-transparent (alpha = 0.5).
Problem
Considering WAlpha(Data >= DummyValue) = 1.0 and WAlpha(Data < DummyValue) = 0.5, the following command does not work as expected:
surf(X, Y, Z, Data, 'AlphaData', WAlpha, 'FaceAlpha', 'interp');
It draws all non-selected region as fully-transparent:
Note
I have no issue when setting 'FaceAlpha' to scalar value (i.e its not an issue with my graphic card):
surf(X, Y, Z, Data, 'AlphaData', WAlpha, 'FaceAlpha', 0.5);
Source code
Here is the link to the very short and dummy code I created to reproduce the issue: link
Please let me know if you have any other idea for emphasing selected region rather than using transparency.

Here is quick test:
%# surface data
Z = membrane;
%# alpha-transparency matrix
A = ones(size(Z))*0.3; %# transparent by default
A(abs(Z)>0.5) = 1; %# make certain region opaque
%# plot
figure('Renderer','opengl')
surf(Z, 'AlphaData',A, 'AlphaDataMapping','none', ...
'FaceAlpha','interp', 'EdgeColor','none')
Result:

Ooops, found it...
One needs to change the Alim property on axes object because it is improperly set to [min(WAlpha) max(WAlpha)] when setting AlphaData instead of keeping [0 1]. So the command is:
surf(X, Y, Z, Data, 'AlphaData', WAlpha, 'FaceAlpha', 'interp');
alim([0 1]);

Related

MATLAB-Patch between prediction bounds?

I have some data fitted to an exponential function (y1) using the curve fitting app to calculate the coefficients. I then imported the model and the prediction intervals (p11; a 1441x2 matrix). (t and Asiii are imported datasets, just generated some data to explain the code)
S=[0.95,0.87,0.83,0.73,0.62,0.51,0.41,0.31,0.22,0.04,0.002,0,0,0,0];
t=[1,3,5,10,20,30,40,50,60,90,120,180,240,360,1440];
hold on
s2=scatter(t,Asiii,250,[0.1216, 0.8, 0.0157],'^','filled');
x = 0:1440;
y1 = 1.016*exp(-0.02349*x)
p1=plot(x,y1,'Color',[0.1216, 0.8, 0.0157],...
'LineWidth',1.5);
p11 = predint(model,x,0.95,'observation','on');
plot(x,p11,'--','Color',[0.4235, 0.9608, .6],...
'LineWidth',1.5);
%Patch
patch([x;x],[p11(:,1)';p11(:,2)'],'g');
%Fill
fill(x, p11,p11,'facecolor', 'red', 'edgecolor', 'none', 'facealpha', 0.4);
I want to have the area between the prediciton bounds/confidence intervals filled with a different color, but it seems I can only get a solid black shading using 'patch'.
I also tried using the 'fill' function but the polygons go all the way to the top of y-axis. Is there a way to adjust the Edge thickness/color?

Camera view angle (Vertical and Horizontal)

I am using this code simulate real camera to capture 3D object:
patch(ax, Object3D, 'FaceColor', 'flat');
camva(ax, rad2deg(VerticalAOV)); % Set the camera field of view
camup(ax, [0 -1 0]);
campos(ax, [CameraPosition.X CameraPosition.Y CameraPosition.Z]); % Put the camera at the origin
camtarget(ax, [CameraPosition.X CameraPosition.Y CameraPosition.Z] + [0 0 1]); % The camera looks along the +Z axis
camproj(ax,'perspective');
axis image;
axis off;
WidthResolution = SensorWidthResolution/(ax.Position(3));
Image = export_fig('temp.jpg',ax, sprintf('-r%f', WidthResolution),'-nocrop');
[ImageHeight, ImageWidth, Channels] = size(Image);
Image = imcrop(Image,[(ImageWidth-Width)/2, (ImageHeight-Height)/2, Width-1, Height-1]);
The problems are:
I used VerticalAOV as input for camva. I chose VerticalAOV based on this image. However, this is illogical. To get real view, one should provide both Horizontal and Vertical Angle.
[ImageHeight, ImageWidth, Channels] = size(Image); returns wrong
values. ImageWidth and ImageHeight does not provide the correct
values. It is clear that one of the dimessions would wrong since only
one dimension of the AOV was provided. However, why both of them are
not correct?
The crop I made is to correct the dimensions of the image. However,
it seems to be not useful step (I checked the result using ground
truth data I have and they did not match).
P.S. export_fig can be found here: https://github.com/altmany/export_fig

Is there a way to control distortion in Matlab's 3D viewer?

The background of this problem relates to my attempt to combine output from a ray tracer with Matlab's 3d plotters. When doing ray tracing, there is no need to apply a perspective transformation to the rendered image. You see this in the image below. Basically, the intersections of the rays with the viewport will automatically adjust for the perspective scaling.
Suppose I've gone and created a ray-traced image (so I am given my camera, my focal length, viewport dimensions, etc.). How do I create exactly the same view in Matlab's 3d plotting environment?
Here is an example:
clear
close all
evec = [0 200 300]; % Camera position
recw = 200; % cm width of box
recl = 200; % cm length of box
h = 150; % cm height of box
% Create the front face rectangle
front = zeros(3,5);
front(:,1) = [-recw/2; 0; -recl/2];
front(:,2) = [recw/2; 0; -recl/2];
front(:,3) = [recw/2; h; -recl/2];
front(:,4) = [-recw/2; h; -recl/2];
front(:,5) = front(:,1);
% Back face rectangle
back = zeros(3,5);
back(:,1) = [-recw/2; 0; recl/2];
back(:,2) = [recw/2; 0; recl/2];
back(:,3) = [recw/2; h; recl/2];
back(:,4) = [-recw/2; h; recl/2];
back(:,5) = back(:,1);
% Plot the world view
figure(1);
patch(front(1,:), front(2,:), front(3,:), 'r'); hold all
patch(back(1,:), back(2,:), back(3,:), 'b');
plot3(evec(1), evec(2), evec(3), 'bo');
xlabel('x'); ylabel('y'); zlabel('z');
title('world view'); view([-30 40]);
% Plot the camera view
figure(2);
patch(front(1,:), front(2,:), front(3,:), 'r'); hold all
patch(back(1,:), back(2,:), back(3,:), 'b');
xlabel('x'); ylabel('y'); zlabel('z');
title('Camera view');
campos(evec);
camup([0 1 0]); % Up vector is y+
camproj('perspective');
camtarget([evec(1), evec(2), 0]);
title('camera view');
Now you see the world view
and the camera view
I know how to adjust the camera position, the camera view angle, and orientation to match the output from my ray tracer. However, I do not know how to adjust Matlab's built-in perspective command
camproj('perspective')
for different distortions.
Note: within the documentation, there is the viewmtx command, which allows you to output a transformation matrix corresponding to a perspective distortion of a certain angle. This is not quite what I want. I want to do things in 3D and through Matlab's OpenGL viewer. In essence, I want a command like
camproj('perspective', distortionamount)
so I can match up the amount of distortion in Matlab's viewer with the distortion from the ray tracer. If you use the viewmtx command to create the 2D projections, you will not be able to use patch' orsurf' and keep colours and faces intact.
The MATLAB perspective projection works just like your raytracer. You don't need any transformation matrices to it use it. Perspective distortion is determined entirely by the camera position and direction of projection.
In the terminology of the raytracer diagram above, if the CameraPosition matches your raytracer's pinhole coordinates and the vector between CameraPosition and CameraTarget is perpendicular to your raytracer's viewport, the perspective distortion will also match. The rest is just scaling and alignment.

python matplotlib styles: How to make custom colored markers + lines using matlab style format string

I am using MATLAB-style formating to change the style of plot lines, like
mystyle = '-r.'
ax1.plot(x1, y1, mystyle)
ax2.plot(x2, y2, mystyle)
...
axN.plot(xN, yN, mystyle)
which draws red line + red dot markers. But how do I specify grey color for lines + markers using MATLAB string?
If it is not feasible, what mystyle should look like so that I can control style of all plots sharing it?
Simply define all properties separately. As grey doesn't exist as predefined color, you need to use an normalized RGB-vector, like [0.2 0.2 0.2]
x = 1:42;
y = sin(x);
plot(x, y, 'color',[0.2 0.2 0.2],'LineStyle','-','Marker','.')
if you want multiple plots with the same style you can save it an cell array and access it with an comma-separated-list:
grey = [0.2 0.2 0.2];
myStyle = {'color',grey,'LineStyle','-','Marker','.','MarkerEdgeColor',grey,'MarkerFaceColor',grey}
plot(x, y, myStyle{:})
Try:
plot(xdata,ydata,'.-','Color',[.5 .5 .5])

Make background of annotation box in image semi-transparent

I am currently working on adding annotations to satellite images in MATLAB. Since the color underneath each text field can vary quite a lot, I want to use a background color under the text to make it easier to see and read.
However, when I do this, a lot of the terrain gets obscured. I though of trying to make the background color for each text box semi transparent, but hit a dead end trying to come up with a solution.
Any ideas? I was hoping for some UI-element where I could just set the 'facealpha' to maybe 0.5. I also need the text to support being rotated (as can be seen in the examples below).
Below is some example code and the resulting image. The workspace with the satellite data can also be found in the link:
Example workspace
figure(1);clf
imagesc(xx,yy,Map);
hold on
plot(xInspection,yInspection,'g.-')
% # Two ways of making a rotated text annotation.
% # Cant make background semi-transparent
testAnno= annotation('textarrow',[0.5 0.5],[0.5 0.5], ...
'string','textarrow annotation', ...
'HeadStyle','none','LineStyle', 'none',...
'TextRotation',asin(directionVec(1))*180/pi,...
'TextBackgroundColor',[0.7 0.7 0.7]);
testText = text(mean(xInspection),mean(yInspection),'text annotation', ...
'rotation',asin(directionVec(1))*180/pi, ...
'HorizontalAlignment','right', ...
'color',[0 0 0], ...
'backgroundcolor',[0.7 0.7 0.7], ...
'fontsize',8);
It doesn't look like either annotation or text return HgObjects that have BackgroundAlpha properties (they might exist but I wasn't able to find them using getundoc or by trying various different hacks).
I was able to get something working by drawing the background myself. Here is a simple proof of concept:
f = figure;
tObj = text(.5, .5, 'text object', 'FontSize', 20);
set(gca,'XLimMode', 'manual', 'YLimMode', 'manual'); % prevent the axes from resizing automatically
p = get(tObj, 'Extent'); %Get the outer position of the text
% now create a patch around the text object
pObj = patch([p(1) p(1) p(1)+p(3) p(1)+p(3)], [p(2) p(2)+p(4) p(2)+p(4) p(2)], 'r');
uistack(tObj, 'top'); % put the text object on top of the patch object
set(pObj , 'FaceAlpha', .2); % set the alpha of the patch face to .2
%Rotate the objects
set(tObj, 'Rotation', 20);
rotate(pObj, [0 0 1], 20);
I am afraid the only way you can do this is by not setting any color to your annotations, and then placing a patch in the background of each annotation. So something like this:
% Use completely transparent annotations
hA = annotation('textarrow', ..., 'TextBackgroundColor', 'none')
% Place a transparent patch exactly in the background of your annotation
hP = patch(X, Y, 'white', 'EdgeColor', 'none', 'FaceColor', 'white', ...
'alpha', 0.3)
% Ensure that your annotation is on top
uistack(hA, 'top')
But of course the big problem is to determine the correct coordinates of the patch (X and Y). Rotating is easy by simply multiplying your coordinates by a rotation matrix. However, finding the length and height of the patch and its central location is not that easy. You might be able to find some useful functions for this at Matlab central...