I want to find PCA of an image... but when i run the code i get following error..
Error using - Integers can only be combined with integers of the same class, or scalar doubles. Error in pca (line 26) data = data - repmat(mn,1,N);
here is the code....'
function [signals,PC,V] = pca()
I = imread('C:\Users\div\Pictures\Picture1.png');
I = rgb2gray( I);
[irow, icol] = size(I);
data = reshape(I',irow*icol,1);
% [sR ,sC ,eR ,eC] = deal(1, 3, 2, 4);
% Compute the sum over the region using the integral image.
% PCA1: Perform PCA using covariance.
% data - MxN matrix of input data
% (M dimensions, N trials)
% signals - MxN matrix of projected data
% PC - each column is a PC
% V - Mx1 matrix of variances
[M,N] = size(data);
% subtract off the mean for each dimension
mn = mean(data,2);
data = data - repmat(mn,1,N);
% calculate the covariance matrix
covariance = 1 / (N-1) * data * data;
% find the eigenvectors and eigenvalues
[PC, V] = eig(covariance);
% extract diagonal of matrix as vector
V = diag(V);
% sort the variances in decreasing order
[junk, rindices] = sort(-1*V);
V = V(rindices); PC = PC(:,rindices);
% project the original data set
signals = PC * data;

 採用された回答

Image Analyst
Image Analyst 2013 年 5 月 24 日

0 投票

That is a dangerous way that you used size(). See Steve's blog: http://blogs.mathworks.com/steve/2011/03/29/even-more-information-about-the-size-function/
A common way to deal with your error is to just convert everything to double. But if you do then you need to use [] to see your images or else they will show up as all white because double images are expected to be in the range 0-1.
imshow(doubleImage, []);
The [] will adjust the display to handle whatever range your array may have, and not require it to be in the 0-1 range.

4 件のコメント

divya
divya 2013 年 5 月 24 日
function [signals,PC,V] = pca()
I = imread('C:\Users\div\Pictures\Picture1.png');
data = rgb2gray( I);
data=double(data);
[irow, icol] = size(I);
data = reshape(I,irow*icol,1);
imshow(data);
% [sR ,sC ,eR ,eC] = deal(1, 3, 2, 4);
% Compute the sum over the region using the integral image.
% imageArray = reshape(imageArray',9,1)
% PCA1: Perform PCA using covariance.
% data - MxN matrix of input data
% (M dimensions, N trials)
% signals - MxN matrix of projected data
% PC - each column is a PC
% V - Mx1 matrix of variances
[M,N] = size(data);
% subtract off the mean for each dimension
mn = mean(data,2);
double(data)- double(repmat(mn,1,N));
% calculate the covariance matrix
covariance = 1 / (N-1) * (data) * (data);
% find the eigenvectors and eigenvalues
[PC, V] = eig(covariance);
% extract diagonal of matrix as vector
V = diag(V);
% sort the variances in decreasing order
[junk, rindices] = sort(-1*V);
V = V(rindices); PC = PC(:,rindices);
% project the original data set
signals = PC * data; after resolving that i get another error
_*Error using * Inner matrix dimensions must agree.
Error in pca (line 33) covariance = 1 / (N-1) * (data) * (data);*_
please anybody can tell me how to exactly find the pca of image pixel data
Image Analyst
Image Analyst 2013 年 5 月 24 日
Maybe you mean
covariance = data .^2 ./ (N-1);
I'm not sure. Use the dot if you want element by element operations.
divya
divya 2013 年 5 月 24 日
Undefined function 'eig' for input arguments of type 'uint8'.
Error in pca (line 46) [PC, V] = eig(covariance);
still getting this error.....:(
Image Analyst
Image Analyst 2013 年 5 月 24 日
So cast it to double, or do
which eig

サインインしてコメントする。

その他の回答 (3 件)

yagnesh
yagnesh 2013 年 5 月 24 日

0 投票

try data = double(data)- double(repmat(mn,1,N));
farahnaz
farahnaz 2014 年 3 月 22 日

0 投票

please use this cod :
function [signals,PC,V] = pca() I=imread('G:\Users\farahnaz\Downloads\018.jpg'); b= im2double(I); imshow(b, []); [irow, icol] = size(b); data = reshape(b',irow*icol,1);
[M,N] = size(data);
mn = mean(data,2);
data = data - repmat(mn,1,N);
covariance = cov(data)';
[PC, V] = eig(covariance);
V = diag(V);
[junk, rindices] = sort(-1*V);
V = V(rindices); PC = PC(:,rindices);

4 件のコメント

Shaveta Arora
Shaveta Arora 2016 年 1 月 31 日
how to show the image after this using principal components. Please answer!!
Image Analyst
Image Analyst 2016 年 1 月 31 日
With
imshow(pcimage, [])
Make sure you get your PC image as a 2D image.
Shaveta Arora
Shaveta Arora 2016 年 1 月 31 日
pls elaborate about the pcimage variable. how to get PC image as 2D
Image Analyst
Image Analyst 2016 年 1 月 31 日
I don't see how he's getting PC images. There aren't any. You can see my attached demo if you want.

サインインしてコメントする。

Shaveta Arora
Shaveta Arora 2016 年 2 月 1 日

0 投票

actually I want to recover the image using principal components after the following instruction:
% project the original data set
signals = PC * data;
pls help me in recovering the image.

カテゴリ

ヘルプ センター および File ExchangeDimensionality Reduction and Feature Extraction についてさらに検索

タグ

タグが未入力です。

質問済み:

2013 年 5 月 23 日

回答済み:

2016 年 2 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by