forked from ChuiJianFeng/Zebrafish-Analyzing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_calibrate_images.m
123 lines (112 loc) · 7.01 KB
/
generate_calibrate_images.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
% Auto-generated by cameraCalibrator app on 30-May-2023
%-------------------------------------------------------
% Define images to process
imageFileNames = {'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\1.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\2.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\3.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\4.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\5.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\6.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\7.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\8.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\9.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\10.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\11.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\12.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\13.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\14.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\15.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\16.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\17.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\18.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\19.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\20.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\21.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\22.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\23.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\24.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\25.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\26.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\27.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\28.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\29.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\30.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\31.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\32.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\33.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\34.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\35.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\36.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\37.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\38.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\39.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\40.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\41.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\42.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\43.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\44.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\45.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\46.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\47.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\48.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\49.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\50.png',...
'D:\Jian\Zebrafish-tracking-analysis-behavior\calibration\water\multi\51.png',...
};
% Detect calibration pattern in images
detector = vision.calibration.monocular.CheckerboardDetector();
[imagePoints, imagesUsed] = detectPatternPoints(detector, imageFileNames);
imageFileNames = imageFileNames(imagesUsed);
% Read the first image to obtain image size
originalImage = imread(imageFileNames{1});
[mrows, ncols, ~] = size(originalImage);
% Generate world coordinates for the planar pattern keypoints
squareSize = 9; % in units of 'millimeters'
worldPoints = generateWorldPoints(detector, 'SquareSize', squareSize);
% Calibrate the camera
[cameraParams, imagesUsed, estimationErrors] = estimateCameraParameters(imagePoints, worldPoints, ...
'EstimateSkew', false, 'EstimateTangentialDistortion', false, ...
'NumRadialDistortionCoefficients', 2, 'WorldUnits', 'millimeters', ...
'InitialIntrinsicMatrix', [], 'InitialRadialDistortion', [], ...
'ImageSize', [mrows, ncols]);
% View reprojection errors
h1=figure; showReprojectionErrors(cameraParams);
% Visualize pattern locations
h2=figure; showExtrinsics(cameraParams, 'CameraCentric');
% Display parameter estimation errors
displayErrors(estimationErrors, cameraParams);
% For example, you can use the calibration data to remove effects of lens distortion.
undistortedImage = undistortImage(originalImage, cameraParams);
%%
% plot image detect points
figure(1),
originalImage = imread(imageFileNames{7});
imshow(originalImage)
hold on
plot(imagePoints(:,1,7),imagePoints(:,2,7),'go', 'MarkerSize',12,'LineWidth',2.1)
plot(cameraParams.ReprojectedPoints(:,1,7),cameraParams.ReprojectedPoints(:,2,7),'r+', 'MarkerSize',10,'LineWidth',2.5)
saveas(gcf, 'detect_point.jpg')
%%
% ------------------------
for i=1:10
name = sprintf('%05d', i);
frame = imread(strcat('../calibrate/multi/Zebrafish_', name,'.png'));
undistortedImage = undistortImage(frame, cameraParams);
% if i==1
% imshow(undistortedImage);
% [xv,yv] = ginput(2);
% y1_plus = xv(1);
% y2_plus = xv(2);
% x1_plus = yv(1);
% x2_plus = yv(2);
% end
% undistortedImage = undistortedImage(yv(1):yv(2),xv(1):xv(2),:);
imwrite(undistortedImage, strcat('../calibrate/multi/cal/Zebrafish_',num2str(i),'.png'))
end
%%
originalImage = imread(imageFileNames{7});
imshow(originalImage);
project_ = cameraParams.ReprojectedPoints;
% See additional examples of how to use the calibration data. At the prompt type:
% showdemo('MeasuringPlanarObjectsExample')
% showdemo('StructureFromMotionExample')