MATLAB Answers


Error using reshape To RESHAPE the number of elements must not change.

Mohannad Nasir さんによって質問されました 2018 年 10 月 19 日
最新アクティビティ Guillaume
さんによって コメントされました 2018 年 10 月 19 日
how can i fix this error ? Error using reshape To RESHAPE the number of elements must not change.

  2 件のコメント

Stephen Cobeldick
2018 年 10 月 19 日
"how can i fix this error ?"
Simple: don't try to reshape an array into a size that has a different number of elements.
2018 年 10 月 19 日
Debug your code and you'll easily see where it is going wrong.

サインイン to comment.

2 件の回答

回答者: Guillaume
2018 年 10 月 19 日

LocalMean must have size(V,1)^2 elements for your reshape to work. As the error message tells you, it hasn't. Since we have no idea what V is and thus how LocalMean is related to it, we can't tell you how to fix it.
Clearly, your code is not behaving the way you expect so you should check what it actually does by using the debugger. Step through your code line by line, see where it behaves differently frm what you expected and fix that.

  3 件のコメント

Mohannad Nasir 2018 年 10 月 19 日
This is the whole code v is defined up there:
img = im2double(imread('drive_01_test.png'));
mask = imread('drive_01_test_mask.png');
B = imcomplement(mat2gray(img(:,:,3)));
Final = mat2gray(B);
V = double(Final);
V = fakepad(V,mask);
W = 15;
step = 2;
segmentedimgR = im_seg(img,mask,W,step);
bw = imbinarize(segmentedimgR,'adaptive','Sensitivity',0.3).*im2double(mask);
bw = bwareaopen(bw,10);
BackgroundMask = ~bw & logical(mask);
PaddedV = padarray(BackgroundMask,[10 10],'replicate','both');
Vcol = im2col(PaddedV,[21 21],'sliding');
PaddedV = padarray(BackgroundMask,[10 10],'replicate','both');
Bcol = im2col(PaddedV,[21 21],'sliding');
ColMat = Vcol.*double(Bcol);
OneSum = sum(ColMat);
NonZeroMask = ColMat > 0;
TwoSum = sum(NonZeroMask);
LocalSum = OneSum./(TwoSum+eps);
LocalMean = (reshape(LocalSum,[size(V,1),size(V,2)]));
figure; imshow(LocalMean, []);
Image Analyst
2018 年 10 月 19 日
Wow, that's some complicated code. Just simply use conv2() or imfilter(), and not use im2col() and padarray() and all that other stuff and you should be fine. Upload both your images if you still have a problem.
2018 年 10 月 19 日
There's not a single comment in your code so we have no idea what it's trying to do.
I notice that the code you've posted is also different from the one in your screenshot. In particular, you've changed the size of the matrix you're trying to reshape to, so hopefully you've already found one bug. I'm afraid you'll have to find the rest of the bugs yourself. Again, use the debugger.
Considering you're doing some padding on some of the variables used to calculate LocalMean and possibly do some padding on V (no idea what fakepad does) have you check that the padding match?
There's clearly a bug in your calculation of LocalMean since you use the exact same code to calculate Vcol and Bcol (both based on BackgroundMask) so your ColMat is simply Vcol.^2 and Bcol is not needed. Not that this would affect the size of LocalMean.

サインイン to comment.

回答者: madhan ravi
2018 年 10 月 19 日

An example to illustrate:
a= rand(4,13) %your vector which needs to be reshaped
reshape(a,m,n) % keep in mind m times n should be equal to numel(a) - meaning number of elements

  0 件のコメント

サインイン to comment.

Translated by