Finding pixel width in matlab - matlab

In order to find the pixel width, how can I do that in matlab?
EDIT I need the width in say for example mm
Thanks.

Try using the ScreenPixelsPerInch property of the root object.
pixelsPerInch = get(0, 'ScreenPixelsPerInch');
mmPerInch = 25.4;
mmPerPixel = mmPerInch / pixelsPerInch;

Related

Dynamically setting a 'targetSize' for centerCropWindow2d()

Following the example from the documentation page of the centerCropWindow2d function, I am trying to dynamically crop an image based on a 'scale' value that is set by the user. In the end, this code would be used in a loop that would scale an image at different increments, and compare the landmarks between them using feature detection and extraction methods.
I wrote some test code to try and isolate 1 instance of this user-specified image cropping,
file = 'frameCropped000001.png';
image = imread(file);
scale = 1.5;
scaled_width = scale * 900;
scaled_height = scale * 635;
target_size = [scaled_width scaled_height];
scale_window = centerCropWindow2d(size(image), target_size);
image2 = imcrop(image, scale_window);
figure;
imshow(image);
figure;
imshow(image2);
but I am met with this error:
Error using centerCropWindow2d (line 30)
Expected input to be integer-valued.
Error in testRIA (line 20)
scale_window = centerCropWindow2d(size(image), target_size);
Is there no way to do use this function the way I explained above? If not, what's the easiest way to "scale" an image without just resizing it [that is, if I scale it by 0.5, the image stays the same size but is zoomed in by 2x].
Thank you in advance.
I didn't take into account that the height and width for some scales would NOT be whole integers. Since Matlab cannot crop images that are inbetween whole pixel numbers, the "Expected input to be integer-valued." popped up.
I solved my issue by using Math.floor() on the 'scaled_width' and 'scaled_height' variables.

Applying temporal median filter to a video

I want to apply Temporal Median Filter to a depth map video to ensure temporal consistency and prevent the flickering effect.
Thus, I am trying to apply the filter on all video frames at once by:
First loading all frames,
%%% Read video sequence
numfrm = 5;
infile_name = 'depth_map_1920x1088_80fps.yuv';
width = 1920; %xdim
height = 1088; %ydim
fid_in = fopen(infile_name, 'rb');
[Yd, Ud, Vd] = yuv_import(infile_name,[width, height],numfrm);
fclose(fid_in);
then creating a 3-D depth matrix (height x width x number-of-frames),
%%% Build a stack of images from the video sequence
stack = zeros(height, width, numfrm);
for i=1:numfrm
RGB = yuv2rgb(Yd{i}, Ud{i}, Vd{i});
RGB = RGB(:, :, 1);
stack(:,:,i) = RGB;
end
and finally applying the 1-D median filter along the third direction (time)
temp = medfilt1(stack);
However, for some reason this is not working. When I try to view each frame, I get white images.
frame1 = temp(:,:,1);
imshow(frame1);
Any help would be appreciated!
My guess is that this is actually working but frame1 is of class double and contains values, e.g. between 0 and 255. As imshow represents double images by default on a [0,1] scale, you obtain a white, saturated image.
I would therefore suggest:
caxis auto
after imshow to fix the display problem.
Best,

Get current figure size in MATLAB

Simple question: How do you get the current figure size in MATLAB?
Example:
figure(1)
[width, height] = ****some function I'm not aware of****
Googling this always returns how to change the window size but not how to just get the current window size.
Any help is appreciated.
Cheers
pos = get(gcf, 'Position'); %// gives x left, y bottom, width, height
width = pos(3);
height = pos(4);

segment image using pixel coordinates matlab

This question maybe pretty basic, so please bear with me. I have 4 pixel coordinats and an image. I want to segment the image part within this 4 points alone and make a new image. Can you please tell me the easiest way to do this?
Look at roipoly using r and c inputs in addition to input image I.
Assuming you have a coordinate list xcoord matching with ycoord and want to have the smallest square that contains your pixels:
myImage = rand(100)
xcoord = [12 16 22 82];
ycoord = [24 70 12 34];
mySegment = myImage(xcoord(min):xcoord(max),ycoord(min):ycoord(max))

get center coordinate image using matlab

eg I want to find the color values ​​of the center of this image
how can i do this?
you mean
img = imread('img.jpg');
color = img(round(end/2), round(end/2), :)
?