下一个代码获得了我拍摄的葡萄的图像(称为:'full_img')并计算葡萄面积:
RGB = imread(full_img);
GRAY = rgb2gray(RGB);
threshold = graythresh(GRAY);
originalImage = im2bw(GRAY, threshold);
originalImage = bwareaopen(originalImage,250);
SE = strel('disk',10);
IM2 = imclose(originalImage,SE);
originalImage = IM2;
labeledImage = bwlabel(originalImage, 8); % Label each blob so we can make measurements of it
blobMeasurements = regionprops(labeledImage, originalImage, 'all');
numberOfBlobs = length(blobMeasurements);
pixperinch=get(0,'ScreenPixelsPerInch'); %# find resolution of your display
dpix=blobMeasurements(numberOfBlobs).Area; %# calculate distance in pixels
dinch=dpix/pixperinch; %# convert to inches from pixels
dcm=dinch*2.54; %# convert to cm from inches
blobArea = dcm; % Get area.
如果我通过不同的摄像机在相同条件下拍摄相同的葡萄(从相同的距离和相同的闪电拍摄),我会得到相同的结果吗?(如果我有5个Mega Pixel和12 Mega Pixel的相机怎么办?)。
不,不会。您使用dpix/pixperinch从图像坐标到世界坐标。通常,这是错误的。如果您知道Pixperinch,它将仅适用于特定图像(单独使用)。为了获得图像中对象的几何特性(例如长度,区域等),必须使用摄像机矩阵和逆射击转换后将图像像素回到笛卡尔空间中的图像像素,以获得笛卡尔坐标(沿校准镜头的镜头失真,这是一个非线性问题)。然后,您可以执行计算。您的代码即使在同一相机中也无法正常工作。请参阅此信息。