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

조회 수: 1 (최근 30일)
Melaku Eneayehu
Melaku Eneayehu 2017년 11월 8일
댓글: Walter Roberson 2020년 12월 5일
clear all;
clc;
warning off;
a=rgb2gray(imread('red.jpg'));
figure, imshow(a);
imData = reshape(a, [], 1);
imData=double(imData);
[IDX nn]= kmeans(imData,4);
imIDX= reshape(IDX, size(a));
figure,imshow(imIDX,[]),title('index image');
  댓글 수: 7
Melaku Eneayehu
Melaku Eneayehu 2017년 11월 8일
what's your matlab version? mine was 2015a
Guillaume
Guillaume 2017년 11월 8일
Matlab version is irrelevant, reshape has always behaved the same in all versions. Similarly, I assume kmeans has always returned results the same size.
Image is irrelevant since the data is reshaped the same size it started with.
As per my answer, the code shouldn't error. So, please answer the questions in my answer so we can understand what's particular about your machine.

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

답변 (2개)

Guillaume
Guillaume 2017년 11월 8일
편집: Guillaume 2017년 11월 8일
I don't see how the code shown can result in the error stated.
Note that another way to write your first reshape is:
imData = a(:);
Either way, you're simply flatting your image into a single column. You're passing that through kmeans which is guaranteed to return a column of the same length, so you still have as many elements as the original image. You're then reshaping that column into the original image size. It's simply not possible for it to error.
Therefore, the first question is: is the code shown the one you actually use, or a simplification? If it is the actual code, then issue a dbstop if error at the command prompt, run your code, and when it breaks into the debugger because of the error, give us the result of:
size(a)
size(imData)
size(IDX)
so we can understand better what is going on.
edit: also give us the output of
which kmeans
  댓글 수: 2
Melaku Eneayehu
Melaku Eneayehu 2017년 11월 8일
sir actually am junior for matlab in this example i want to segment the image using k-means clustering, when i try this code i got this error.
Guillaume
Guillaume 2017년 11월 8일
편집: Guillaume 2017년 11월 8일
I perfectly understood what you are trying to do. Once again, do what I said in my answer. Enter dbstop if error at the command line. Run your code and when it breaks into the debugger give us the output of:
size(a)
size(imData)
size(IDX)
which kmeans
and we'll work from there.

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


Manochandar Thenralmanoharan
Manochandar Thenralmanoharan 2020년 12월 5일
imData = reshape(a,[],1); what the statement implies?
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 12월 5일
That reshapes to one column and as many rows as necessary to hold the entire array.

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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by