Assign dimensions for my image. I have an image and I need to give it a length that I know.
34 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Image Analyst
2022-1-3
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;
0 个评论
Turlough Hughes
2022-1-3
编辑:Turlough Hughes
2022-1-3
% Example of a Binary image
BW = imbinarize(imread('coins.png'));
% 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)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!