- Check OpenCV Installation: Ensure that OpenCV is correctly installed and that the MATLAB bindings for OpenCV are properly set up. You might need to install the mexopencv package if you haven’t already.
- Add OpenCV to MATLAB Path: Make sure that the path to the OpenCV library is added to the MATLAB path. You can do this by using the addpath function in MATLAB:
- Verify Function Availability: Confirm that the cv.getOptimalNewCameraMatrix function is available in your OpenCV installation. You can do this by checking the documentation or the list of functions in the OpenCV package.
- Update OpenCV: If you are using an older version of OpenCV, consider updating to a newer version.There have been fixes and improvements in recent versions that might resolve your issue.
- Exmaple Code Piece:
Unable to resolve the name cv.getOptimalNewCameraMatrix.
8 views (last 30 days)
Show older comments
I do not solve this problem. I am trying to solve in many ways. Please anyone help me.
I find this problem:
Unable to resolve the name cv.getOptimalNewCameraMatrix.
Error in undistortion>undistorb_images (line 92)
[newcameramtx, roi] = cv.getOptimalNewCameraMatrix(camera_matrix, dist, [width,height], 0,
[width,height]);
Error in undistortion (line 1)
undistorb_images([], []);
undistorb_images([], []);
function [tvec, rvec, camera_matrix, dist] = read_wp2c(input_name)
%input_name = "output_wp2camera.json";
raw = fileread(input_name);
input_params = jsondecode(raw);
camera_matrix = input_params.camera_matrix;
dist = input_params.dist_coefs;
tvec_json = input_params.translational_vectors;
%tvec = struct2cell(tvec_json);
rvec_json = input_params.rotational_vectors;
%rvec = struct2cell(rvec_json);
tvec = [];
rvec = [];
len = length(tvec_json);
for i = 1:len
%tvec.append(array(tvec_json(image + string(i))))
%tvec.append.tvec_json(i);
tvec = struct2cell(tvec_json(i));
%[A{:}]
%tvec.append(input_params.translational_vectors.image0);
%rvec.append(array(rvec_json(image + string(i))));
rvec = struct2cell(rvec_json(i));
end
end
function undistorb_images(inputParams, result)
%if result is None:
if isempty(result)
input_name = "output_wp2camera.json";
[tvec, rvec, camera_matrix, dist] = read_wp2c(input_name);
else
tvec = result(4);
rvec = result(3);
camera_matrix = result(1);
dist = result(2);
end
if isempty(inputParams)
image_path = "images";
else
image_path = inputParams.opencv_storage.settings.Images_Folder;
end
%image_files = [];
files = [dir(fullfile(image_path,'*.jpg')); dir(fullfile(image_path,'*.png')); dir(fullfile(image_path,'*.jpeg')); dir(fullfile(image_path,'*.PNG'))];
%files = dir(fullfile(image_path, '*.(jpg|png)'));
L = length(files);
% kk=0;
% for i=1:L
% file=files(i).name;
% image_files = [image_files, file]
%
%
% end
%disp(image_files)
% for f = dir(image_path)
% %ext = image_path(split(lower(f)));
% %disp(f)
% %if f.endsWith([".jpg",".jpeg",".png", ".PNG"])
% % image_files.append(f);
% %end
% end
image_file_name = [];
if ~isempty(files)
for i=1:L
file=files(i).name;
image_file_name = [image_file_name,file];
%disp(image_file_name)
image = imread(image_path + "\" + file);
%disp(image);
% [imagePoints, boardSize] = detectCheckerboardPoints(image_file_name);
% squareSize = 29;
% worldPoints = generateCheckerboardPoints(boardSize, squareSize);
%
% I = readimage(image, 1);
% imageSize = [size(I,1), size(I,2)];
% [params, ~, estimationErrors] = estimateCameraParameters(imagePoinsts, worldPoints, 'ImageSize');
[height, width] = size(image);
%disp(string(height) + " " + string(width))
%[newCameraMatrix,w] = cv.getOptimalNewCameraMatrix(cameraMatrix, dist, [width,height]);
[newcameramtx, roi] = cv.getOptimalNewCameraMatrix(camera_matrix, dist, [width,height], 0, [width,height]);
%dst = cv2.undistort(image, camera_matrix, dist, None, newcameramtx)
% imageFileNames{i} = fullfile(matlabroot,'toolbox','vision','visiondata','calibration','webcam',image_file_name);
% [imagePoints,~,imagesUsed] = detectCheckerboardPoints(imageFileNames, 'PartialDetections', false);
% imageFileNames = imageFileNames(imagesUsed);
% for i = 1:numel(imageFileNames)
% I = imread(imageFileNames{i});
% subplot(2, 2, i);
% imshow(I);
% hold on;
% plot(imagePoints(:,1,i),imagePoints(:,2,i),'ro');
% end
[mapx, mapy] = cv.initUndistortRectifyMap(camera_matrix, dist, None, newcameramtx, [width,height], 5);
dst = cv.remap(image, mapx, mapy, INTER_LINEAR);
x, y, w, h = roi;
dst = dst(y:y+h, x:x+w);
[height, width] = size(dst);
print(string(height) + " " + string(width));
imwrite("undistortion/" + file, dst);
end
end
end
0 Comments
Answers (1)
prabhat kumar sharma
on 26 Jul 2024
Hello Md,
It looks like you’re encountering an issue with the cv.getOptimalNewCameraMatrix function in MATLAB. This error typically occurs when MATLAB cannot find the specified function, which might be due to a missing or incorrect path to the OpenCV library.
Here are a few steps you can take to resolve this issue:
% Example usage of cv.getOptimalNewCameraMatrix
camera_matrix = [fx, 0, cx; 0, fy, cy; 0, 0, 1];
dist = [k1, k2, p1, p2, k3];
image_size = [width, height];
[newcameramtx, roi] = cv.getOptimalNewCameraMatrix(camera_matrix, dist, image_size, 0);
I hope it helps!
0 Comments
See Also
Categories
Find more on Code Generation, GPU, and Third-Party Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!