getframe - different output on different computers (using plot)

3 views (last 30 days)
Hi!
I'm creating a program where i want to draw a kind of hourglass shape and then save this as a binary image. This shape is to be drawn based on coordinates from an image. The problem I have come across is that the thickness of the drawn shape in the output image (binaryThinLine below) differs depending on which computer I run it on. I believe that the problem occurs when I save it using getframe, but I am not sure.
This is the code I'm using:
set(groot, 'defaultFigureUnits', 'pixels');
set(groot, 'defaultFigurePosition', [100 100 480 853]);
set(groot,'defaultFigureVisible','off');
figure
imshow(frame)
hold on;
plot(x, y, 'b', 'LineWidth', thickness_black, 'LineJoin', 'miter', 'Color', [0 0 1 0.25]); % Plot outline with blue color and thicker line width
hold off;
% Choosing axes
xlim([0, width(frame)]);
ylim([0, height(frame)]);
axis off;
thinLineFrame = getframe(gca);
[thinLineImage, Map] = frame2im(thinLineFrame);
thinLineImage_blue = thinLineImage(:,:,1);
binaryThinLine = imbinarize(thinLineImage_blue, 0.9); % Creating a binary image.
%binaryThinLine=imresize(binaryThinLine,[480 853]);
When defaultFigureVisible is set to off, binaryThinLine becomes the same size on both computers. However, when it is set to on, they become different sizes and have to be resized.
So, my question is: how can I make sure that binaryThinLine keeps the same thickness on both computers, while also having the same dimension (480x853)? Thank you in advance!

Answers (1)

Suraj Kumar
Suraj Kumar on 23 May 2024
Hi Johanna,
When "defaultFigureVisible" is set to "off", the image maintains the same size on both computers. This is because MATLAB generates the figure in the background without displaying it on the screen, ensuring the figure is created with the exact dimensions specified in the code. As a result, it remains unaffected by external factors like screen resolution or window scaling.
To achieve consistent dimensions and line thickness across different computers, using "plot" with a specified "LineWidth" ensures the thickness of the shape remains uniform. And setting the dimensions of the frame , the figure properties ensure the output image has consistent dimensions (480x853 pixels).
You can refer the below code snippet for better understanding:
frame = 255 * ones(853, 480, 3, 'uint8');
figure;
imshow(frame);
hold on;
plot(x, y, 'b', 'LineWidth', 2, 'LineJoin', 'miter', 'Color', [0 0 1 0.25]);
hold off;
% Axes and frame capture
xlim([0, size(frame, 2)]);
ylim([0, size(frame, 1)]);
axis off;
thinLineFrame = getframe(gca);
[thinLineImage, ~] = frame2im(thinLineFrame);
% Image processing
thinLineImage_blue = thinLineImage(:,:,1);
binaryThinLine = imbinarize(thinLineImage_blue, 0.9);
And convert the binary image to uint8 format and save using “imwrite” function:
binaryThinLine_uint8 = uint8(binaryThinLine) * 255;
imwrite(binaryThinLine_uint8, 'binaryThinLine.png');
You can refer the following documentation for better understanding:
  1. https://www.mathworks.com/help/images/ref/imbinarize.html
  2. https://www.mathworks.com/help/matlab/ref/imwrite.html
Hope this helps!

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!