How can I expand the webcam´s window? MATLAB - matlab

I have followed this tutorial Acquire Images from Webcams but when I make the preview of the image with
preview(cam) the image appears small and i want the image in fullscreem.
I have proved with set(gcf,'units','normalized','outerposition',[0 0 1 1]) but it doesnt work.
How can I expand the preview image? Thanks a lot

Finally I found a solution. I hope it is useful for someone
%% Create a webcam object called cam
cam = webcam;
cam.Resolution = '1280x720';
dimensions = get(0, 'ScreenSize'); %your screem
figure('Toolbar','none',...
'Menubar', 'none',...
'NumberTitle','Off',...
'Position', [1 1 dimensions(3) dimensions(4)], ... % Position, pixels[x, y, width, height]
'Name','My Preview Window');
hImage = image(zeros(720, 1280, 3));
preview(cam, hImage);

Related

How do you make a MATLAB's `uifigure` appear in the center of the screen?

One can easily use the Position property to place a uifigure in the specified location of the screen. E.g., fig = uifigure('Position',[1,1,300,300]);. Is there any way to place it immediately on the center of screen.
There is a movegui command which is helpful for this task. However, it does this work in two steps (first, displays the figure, then moves it). This results in a not smooth experience for the user.
We need to get the screen size to determine the center. The code below will create a figure at the center of the screen.
% width and height of the figure
width = 300;
height = 300;
% screen size
sz = get( 0, 'ScreenSize');
% center position
x = mean( sz( [1, 3]));
y = mean( sz( [2, 4]));
fig = uifigure( 'Position', [x - width/2, y - height/2, width, height])
Use
movegui(app.UIFigure,'center')

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

Export/Rasterize alpha shape to bitmap

I build an alpha shape from some points (example given in code) and want to export the shape to a raster graphics format. I need the shape only, not the plot markings (axis, scales ect).
I need only the resulting triangle on white ground as a bitmap.
Scale needs to be 1 unit = 1 pixel.
x = [0 10 20 30 30 30 15];
y = [0 0 0 0 15 30 15];
shape = alphaShape (x',y');
plot (shape, 'FaceColor', 'black');
I have not found anything on how to export shapes or how to rasterize them. Is there any way to do that?
Run the following code after yours.
imgwidth = max(1, ceil(max(x) - min(x)));
imgheight = max(1, ceil(max(y) - min(y)));
ax = gca;
ax.Visible = 'off';
ax.XTickMode = 'manual';
ax.YTickMode = 'manual';
ax.ZTickMode = 'manual';
ax.XLimMode = 'manual';
ax.YLimMode = 'manual';
ax.ZLimMode = 'manual';
ax.Position = ax.OuterPosition;
af = gcf;
figpos = getpixelposition(af);
resolution=get(0, 'ScreenPixelsPerInch');
set(af, 'paperunits','inches', ....
'papersize',[imgwidth imgheight]/resolution, ....
'paperposition',[0 0 [imgwidth imgheight]/resolution]);
print(af,'out.png','-dpng',['-r',num2str(resolution)],'-opengl')
Things done:
Fetch data range and convert to image dimensions.
Turn off axes and ticks.
Minimize/remove padding space surrounding the actual content.
Map 1 unit in data into 1 pixel in output image.
Things not done:
Guarantee aspect ratio. (should work, though)
This screenshot shows non-unity aspect ratio output:
References
Mathworks - Save Figure at Specific Size and Resolution
MATLAB Central - saving a figure at a set resolution
Mathworks - print
Mathworks - Save Figure with Minimal White Space

Draw a rectangular bounding box around a person in an image

I want to make a bounding box around a person in an image. I tried different methods but I couldn't get the solution that I want.
Here's the image I am using:
Here's the code I have written so far:
bw = im2bw(test, graythresh(test));
bw2 = imfill(bw,'holes');
imshow(bw2);
figure;
L = bwlabel(bw2);
imshow(label2rgb(L, #jet, [.7 .7 .7]))
figure;
imshow(I1);
R = regionprops(L, 'BoundingBox');
rectangle('Position', R(1).BoundingBox);
Your problem actually isn't drawing the bounding box - it's locating the person inside the image, which you haven't quite done properly. If you don't do this correctly, then you won't be able place the correct bounding box around the person. This is what I have done to locate the person in the image, then drawing a bounding box around this person. This is assuming that your image is stored in im:
Notice that the intensity distribution of the person is darker than most of the scene. As such, I'm going to threshold the image by choosing any pixels less than intensity 65 to be white while the other pixels black.
I clear any white pixels that surround the image border
I perform a regionprops call extracting the BoundingBox and Area properties.
I search through all of the areas and find the BoundingBox with the largest Area.
I use this BoundingBox and draw it on our image.
Therefore:
%// Step #1
im_thresh = im < 65;
%// Step #2
im_thresh2 = imclearborder(im_thresh);
%// Step #3
rp = regionprops(im_thresh2, 'BoundingBox', 'Area');
%// Step #4
area = [rp.Area].';
[~,ind] = max(area);
bb = rp(ind).BoundingBox;
%// Step #5
imshow(im);
rectangle('Position', bb, 'EdgeColor', 'red');
This is what we get:
Bear in mind that this isn't perfect. You may have to play around with the threshold to get a more accurate bounding box, but this should be enough for you to start with.
Good luck!
You can also use vision.PeopleDetector in the Computer Vision System Toolbox:
im = imread('bnJzI.png');
detector = vision.PeopleDetector('ClassificationModel', 'UprightPeople_96x48', 'ClassificationThreshold', 2.5);
bbox = step(detector, im);
im2 = insertObjectAnnotation(im, 'rectangle', bbox, 'person');
imshow(im2);

Colouring specific pixels in an image

Say I have an image. How can I colour some specific pixels in that image using MATLAB?
Thanks.
RGB Pixels
I'd suggest working with an RGB image, so that you can easily represent color and gray pixels. Here's an example of making two red blocks on an image:
img = imread('moon.tif');
imgRGB = repmat(img,[1 1 3]);
% get a mask of the pixels you want and set an RGB vector to those pixels...
colorMask = false(size(imgRGB,1),size(imgRGB,2));
colorMask(251:300,151:200,:) = true; % two discontiguous blocks
colorMask(50:100,50:100,:) = true;
redPix = permute([255 0 0],[1 3 2]);
imgRGB(repmat(colorMask,[1 1 3])) = repmat(redPix, numel(find(colorMask)),1);
AlphaData image property
Another cool way of doing this is with an image's AlphaData property. See this example on a MathWorks blog. This essentially turns color on or off in certain parts of the image by making the gray image covering the color image transparent. To work with a gray image, do like the following:
img = imread('moon.tif');
influenceImg = abs(randn(size(img)));
influenceImg = influenceImg / (2*max(influenceImg(:)));
imshow(img, 'InitialMag', 'fit'); hold on
green = cat(3, zeros(size(img)), ones(size(img)), zeros(size(img)));
h = imshow(green); hold off
set(h, 'AlphaData', influenceImg)
See the second example at the MathWorks link.