# Assign dimensions for my image. I have an image and I need to give it a length that I know.

1 view (last 30 days)
Ali Jarekji on 3 Jan 2022
Edited: Turlough Hughes on 3 Jan 2022
The image attached is the one I have. In real life, the image has a specific length from top to bottom and it is 102. I just want to assign this value to the image length from top to bottom and thus the dimensions of the image would represent the real life dimensions.

Image Analyst on 3 Jan 2022
Run through the attached demo on spatial calibration. After that you'll see how you can draw a known distance in the image, give a known real world number for it, and have it calculate a calibration factor in millimeters (or whatever) per pixel. Then you can multiply all pixel distances by mmPerPixel to get distances in mm, and by mmPerPixel^2 to get areas in square mm from area in pixels.
Or if you know the full field of view and don't want to draw any distance interactively you can do
[rows, columns, numberOfColorChannels] = size(yourImage)
fovMm = 5; % Whatever width your FOV is in real world units.
mmPerPixel = fovMm / columns;
Normally people have square pixels, so you can use that factor for both directions. But if not, you can compute a calibration factor for the horizontal and vertical directions separately.
[rows, columns, numberOfColorChannels] = size(yourImage)
fovMmX = 5; % Whatever width your FOV is in real world units.
mmPerPixelX = fovMmX / columns;
fovMmY = 6; % Whatever height your FOV is in real world units.
mmPerPixelY = fovMmY / rows;

Turlough Hughes on 3 Jan 2022
Edited: Turlough Hughes on 3 Jan 2022
The imref2d object is useful for exactly this. Let's have a look at an example:
% Example of a Binary image
% For the example, let's assume we know the height/width in mm:
heightMM = 82;
widthMM = 100;
% Create an imref2d object, to do this we need to input the
% the size of the image, as well as the height and width of
% a pixel in millimeters.
R = imref2d(size(BW),heightMM/size(BW,1),widthMM/size(BW,2));
% Plot the results
figure
imshow(BW,R);
xlabel('x - coordinate / [mm]')
ylabel('y - coordinate / [mm]')
set(gca,'FontSize',14) Now, the dimensions of the image are represented in the world frame of reference. You can also analyse the image with datatips, and you'll see that X and Y are in terms of the real world coordinates. I suggest taking some time to understand the different coordinate systems here. You can also easily convert between intrinsic and world coordinates, let's take the following example:
% I'm using the coins' centroids as an example of data that we might want
% to convert
props = regionprops(BW,'centroid');
centroids = vertcat(props.Centroid);
xc = centroids(:,1); % these are intrinsic coordinates (pixel indices)
yc = centroids(:,2);
% Regular imshow
hf = figure; subplot(1,2,1)
hf.Position(3) = 2*hf.Position(3);
imshow(BW), title('Intrinsic Coordinates')
axis on
xlabel('x - coordinate / [px]')
ylabel('y - coordinate / [px]')
hold on, plot(xc, yc, 'r+', 'LineWidth', 2)
set(gca,'FontSize',14)
% imshow using a world frame of reference
subplot(1,2,2)
imshow(BW, R), title('World Coordinates')
axis on
xlabel('x - coordinate / [mm]')
ylabel('y - coordinate / [mm]')
% The conversion is just the following:
[xc_mm, yc_mm] = intrinsicToWorld(R, xc, yc);
% And we can show the converted centroid coordinates on the image
hold on, plot(xc_mm, yc_mm, 'r+', 'LineWidth', 2)
set(gca,'FontSize',14) 