MATLAB Answers

Dot indexing is not supported for variables of this type.

조회 수: 34(최근 30일)
Md Shahidul Islam
Md Shahidul Islam 2021년 8월 17일
편집: Adam Danz 2021년 8월 17일
I faced this problem here. Please help me. I already try with give solution .
image_files = [];
for f = isfolder(image_path)
if f.endsWith([".jpg",".jpeg",".png", ".PNG"]) //problem is here
Dot indexing is not supported for variables of this type.
Error in undistortion>undistorb_images (line 51)
if f.endsWith([".jpg",".jpeg",".png", ".PNG"])
  댓글 수: 2
Md Shahidul Islam
Md Shahidul Islam 2021년 8월 17일
Yes, I am trying to recursively search directories and build a list of paths to image files. Here is my full code:
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 = struct2cell(tvec_json(i));
%rvec.append(array(rvec_json(image + string(i))));
rvec = struct2cell(rvec_json(i));
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);
tvec = result(4);
rvec = result(3);
camera_matrix = result(1);
dist = result(2);
if isempty(inputParams)
image_path = "D:\matlab_code\Calibration_Task\images\";
image_path = inputParams.opencv_storage.settings.Images_Folder;
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);
for i=1:L
% 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(image_files)
for image_file = image_files
image = imread(image_path + "/" + image_file);
[height, width] = size(image);
print(str(height) + " " + str(width))
[newcameramtx, roi] = getOptimalNewCameraMatrix(camera_matrix, dist, [width,height], 0, [width,height]);
% dst = cv2.undistort(image, camera_matrix, dist, None, newcameramtx)
[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(str(height) + " " + str(width));
imwrite("undistortion/" + image_file, dst);

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2021년 8월 17일
편집: Adam Danz 2021년 8월 17일
> I am trying to recursively search directories and build a list of paths to image files
% Define the main directory
image_path = 'C:\Users\name\Documents';
% list accepted extensions (with leading dot, case ignored)
acceptedExt = [".jpg",".jpeg",".png"];
Recursively search main directory (and sub directories) for files with listed extensions. fileList contains a cell array of full paths to matched files. The extension match ignores case.
d = dir(fullfile(image_path,'**\*.*'));
d([d.isdir]) = []; % remove directories from list
[~,ext] = regexp({}','.*(\..*$)','match','once','tokens');
isAccepted = ismember(lower([ext{:}]), lower(acceptedExt));
d(~isAccepted) = []; % remove files that do not have accepted ext.
% list full paths to accepted files
fileList = fullfile({d.folder},{})';
  댓글 수: 2
Adam Danz
Adam Danz 2021년 8월 17일
Sure, to display the number of accepted files for each extension, replace the ismember line with the line below and add the second block of code to the end.
[isAccepted,extIdx] = ismember(lower([ext{:}]), lower(acceptedExt));
% Count number of matches for each acceptedExt
unqExtIdx = unique(extIdx(isAccepted));
count = histcounts(extIdx(isAccepted), 'BinMethod','integers');
disp(compose('%s: %.0f', acceptedExt', count'))
Example output:
ans =
4×1 cell array
{'.jpg: 388'}
{'.jpeg: 0' }
{'.png: 0' }
{'.gif: 1' }

댓글을 달려면 로그인하십시오.

추가 답변(0개)





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by