Hi,
I'm trying to measure distance from camera to object using stereo images. I used 40 image pairs for calibration with matlab stereo calibration app. At the result of the calibration, overall mean error was 1.49 pixels. I applied all of the technics which are in the Matlab's depth estimation from stereo video tutorial.
My actual distance from camera to object is 2.97 meters. However, I found 4,32 meters as a result of my program's calculation. I guess there is something wrong in my disparity map and 3d scene reconstruction. Because my disparity map is really ambiguous and point cloud is so inadequate. I would like to get any suggestion regarding this topic. Also i tried to apply some technics such as median and wiener filter to remove noise from images which were written at this topic: http://uk.mathworks.com/matlabcentral/answers/153348-tips-and-tricks-about-3d-scene-reconstruction
These are my outputs:
This is my code:
Read and Rectify Images
imageLeft = imread('D:\stereo\imgPairLeft1.png');imageRight = imread('D:\stereo\imgPairRight1.png');imageLeft = undistortImage(imageLeft, stereoParams.CameraParameters1, ... 'OutputView', 'same');imageRight = undistortImage(imageRight, stereoParams.CameraParameters2, ... 'OutputView', 'same');[imageLeftRect, imageRightRect] = ... rectifyStereoImages(imageLeft, imageRight, stereoParams);subplot(2,2,1);imshow(stereoAnaglyph(imageLeftRect, imageRightRect));title('Rectified Images');imageLeftGray = rgb2gray(imageLeftRect);imageRightGray = rgb2gray(imageRightRect);imageLeftGrayHisteq = histeq(imageLeftGray);imageRightGrayHisteq = histeq(imageRightGray);
Compute Disparity
disparityRange = [0 80];disparityMap = disparity(imageLeftGrayHisteq, imageRightGrayHisteq, ... 'DisparityRange', disparityRange, 'BlockSize', 15);subplot(2,2,2);imshow(disparityMap, disparityRange);title('Disparity Map');colormap jet;colorbar;
Reconstruct the 3-D scence
points3D = reconstructScene(disparityMap, stereoParams);% Convert to meters and create a pointCloud object
points3D = points3D ./ 1000;ptCloud = pointCloud(points3D, 'Color', imageLeftRect);subplot(2,2,3);pcshow(ptCloud);title('Point Cloud');
Thresholding
binaryImage = imageLeftGrayHisteq > 0 & imageLeftGrayHisteq < 60;binaryImage = imfill(binaryImage, 'holes');% Assign each blob different color
labeledImage = bwlabel(binaryImage, 8); coloredLabels = label2rgb(labeledImage, 'hsv', 'k', 'shuffle');
Blob Analysis
blobMeasurements = regionprops(labeledImage, imageLeftGrayHisteq, 'all');numberOfBlobs = size(blobMeasurements, 1);% Sort the rows by Area.
[~,index] = sortrows([blobMeasurements.Area].'); blobMeasurements = blobMeasurements(index);newIndex = sortrows(index);% After sorting, last index is the largest blob
tvUnitBoundingBox = blobMeasurements(newIndex(end)).BoundingBox;
Determine the distance of tv unit to the camera. Find the centroids of tv unit.
centroid = [round(tvUnitBoundingBox(:,1) + tvUnitBoundingBox(:,3) / 2) ... round(tvUnitBoundingBox(:,2) + tvUnitBoundingBox(:,4) / 2)];% Find 3-D world coordinates of the centroids.
centroidsIdx = sub2ind(size(disparityMap), centroid(:,2), centroid(:,1));X = points3D(:, :, 1);Y = points3D(:, :, 2);Z = points3D(:, :, 3); centroids3D = [X(centroidsIdx)'; Y(centroidsIdx)'; Z(centroidsIdx)'];% Find the distances from the camera in meters
distanceFromTvUnitToCam = sqrt(sum(centroids3D .^ 2));% Display the tv unit and its distance.
label = sprintf('%02f meters', distanceFromTvUnitToCam);subplot(2,2,4);imshow(insertObjectAnnotation(imageLeftRect, 'rectangle', tvUnitBoundingBox, label));title('Detected Object');
Best Answer