Index in position 3 exceeds array bounds (must not exceed 1)
1 次查看(过去 30 天)
显示 更早的评论
Could sombody help me to resolve this error?
--------------------------------------------------------------------------------------------------------------------------
% CleanUp
clc;
clear;
close all;
%Read Photo RGB and DSM
orph = imread('BE_ORTHO_27032011_315145_56865.tif');
figure, imshow (orph,[]);
%Define Region of interest
roi_Building = drawpolygon('Color','r');
roi_street = drawline('Color','y');
roi_tree = drawfreehand('Color','g');
roi_car = drawpolygon('Color','b');
%%Seperate channels RGB
op_red = orph(:,:,1); % Red channel
op_green = orph(:,:,2); % Green channel
op_blue = orph(:,:,3); % Blue channel
%%Value of pixels for ROI in all layers
[r, c, ~] = size(orph);
mask_building = createMask(roi_Building, r, c);
mask_street = createMask(roi_street, r, c);
mask_tree = createMask(roi_tree, r, c);
mask_Car = createMask(roi_car, r, c);
building_pixels = [op_red(mask_building), op_green(mask_building), op_blue(mask_building)];
street_pixels = [op_red(mask_street), op_green(mask_street), op_blue(mask_street)];
tree_pixels = [op_red(mask_tree), op_green(mask_tree), op_blue(mask_tree)];
building_Car = [op_red(mask_Car), op_green(mask_Car), op_blue(mask_Car)];
%Buildings
[r, c, p] = size(orph); %Row, Column and layers (size of Image)
mask3_building = repmat(createMask(roi_Building, r, c), [1, 1, 3]); %Repeat copies of array
masked_building = zeros(r, c, p, 'like', orph);
masked_building(mask3_building) = orph(mask3_building);
%Street
mask3_street = repmat(createMask(roi_street, r, c), [1, 1, 3]);
masked_street = zeros(r, c, p, 'like', orph);
masked_street(mask3_street) = orph(mask3_street);
%Tree
mask3_tree = repmat(createMask(roi_tree, r, c), [1, 1, 3]);
masked_tree = zeros(r, c, p, 'like', orph);
masked_tree(mask3_tree) = orph(mask3_tree);
%Car
mask3_car = repmat(createMask(roi_car, r, c), [1, 1, 3]);
masked_car = zeros(r, c, p, 'like', orph);
masked_car(mask3_car) = orph(mask3_car);
%%Arithmetic average for each Class
Class_mean1 = mean(masked_building(mask3_building));
Class_mean3 = mean(masked_street(mask3_street));
Class_mean2 = mean(masked_tree(mask3_tree));
Class_mean4 = mean(masked_car(mask3_car));
%Distance between every Pixel in Picture to average for each Class
i=1:10000;
j=1:10000;
orph =[i,j]; %Image RGB
Di_Building = sqrt((orph(:,:,1)-Class_mean1(1,1)).^2+(orph(:,:,2)-Class_mean1(2,1)).^2+(orph(:,:,3)-Class_mean1(3,1)).^2);
Di_ground = sqrt((orph(:,:,1)-Class_mean3(1,1)).^2+(orph(:,:,2)-Class_mean3(2,1)).^2+(orph(:,:,3)-Class_mean3(3,1)).^2);
Di_Vegetation = sqrt((orph(:,:,1)-Class_mean2(1,1)).^2+(orph(:,:,2)-Class_mean2(2,1)).^2+(orph(:,:,3)-Class_mean2(3,1)).^2);
Di_cars = sqrt((orph(:,:,1)-Class_mean4(1,1)).^2+(orph(:,:,2)-Class_mean4(2,1)).^2+(orph(:,:,3)-Class_mean4(3,1)).^2);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1083250/image.jpeg)
0 个评论
采纳的回答
Torsten
2022-7-30
编辑:Torsten
2022-7-30
"orph" has dimension 1 x 20000 because you overwrite the image by the command orph = [i,j].
Then you try to access orph(:,:,2) and orph(:,:,3) which do not exist.
10 个评论
Torsten
2022-7-30
After
Class_mean1 = mean(masked_building(mask3_building));
Class_mean3 = mean(masked_street(mask3_street));
Class_mean2 = mean(masked_tree(mask3_tree));
Class_mean4 = mean(masked_car(mask3_car));
insert the lines
size(Class_mean1)
size(Class_mean2)
size(Class_mean3)
size(Class_mean4)
If they are not (3x1), the references to these variables in
Di_Building = sqrt((orph(:,:,1)-Class_mean1(1,1)).^2+(orph(:,:,2)-Class_mean1(2,1)).^2+(orph(:,:,3)-Class_mean1(3,1)).^2);
Di_ground = sqrt((orph(:,:,1)-Class_mean3(1,1)).^2+(orph(:,:,2)-Class_mean3(2,1)).^2+(orph(:,:,3)-Class_mean3(3,1)).^2);
Di_Vegetation = sqrt((orph(:,:,1)-Class_mean2(1,1)).^2+(orph(:,:,2)-Class_mean2(2,1)).^2+(orph(:,:,3)-Class_mean2(3,1)).^2);
Di_cars = sqrt((orph(:,:,1)-Class_mean4(1,1)).^2+(orph(:,:,2)-Class_mean4(2,1)).^2+(orph(:,:,3)-Class_mean4(3,1)).^2);
will make MATLAB error.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!