Username Remember Me?
Password   forgot password?
   
   
Classification of images
Posted: 03 June 2009 07:48 PM   [ Ignore ]  
Newbie
Rank
Total Posts:  5
Joined  2009-05-19

Hi,

I am trying to classify an image and I´m finding the following problem:

>> fo=imread(’Pac2_03_part1.tif’); % rgb image, each pixel is a object
>> fr=imread(’Pac2_03_rot_part1.tif’); % labeled image
>> fobj=im2obj(fo); % dataset without labels
>> ff=im2feat(fr,fobj)
??? Error using ==> im2feat
Image size and dataset object size do not match

I want to add labels in the dataset (fobj), and soon afterwards you classify it.

thanks

Profile
 
 
Posted: 03 June 2009 08:26 PM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
RankRankRankRank
Total Posts:  236
Joined  2008-04-26

Hi kesede,

I think there is a problem with dataset sizes. By im2obj, you convert an image band into an object (data sample) in a dataset. Each pixel becomes a feature.
By im2feat, you convert an image into a dataset where each pixel is a sample and each band (color) a feature.
Because of different interpretation (and sample/feature sizes) you cannot directly mix both datasets.

Here is an example of adding labels to an image stored in a dataset:

>> im=imread('~/Desktop/roadsign09.bmp');
>> 
a=im2feat(im)
412160 by 3 dataset with 0 classes[]

>> getfeatsize(a) % 3 feature three color bands
ans 
=
     
3
>> getobjsize(a) % object size refers to the image matrix dimensions
ans 
=
   
560   736

lets create labels by thresholding the blue channel:
>> 
lab=double(im(:,:,3)>200);

put labels into the dataset (note that we reshape the lab matrix into the column vector):
>> 
a2=setlabels(a,lab(:))
412160 by 3 dataset with 2 classes[162030  250130]

to visualize the resulting dataset in PRTools:
>> 
show(a2)
the last band shows the labels.

we can overlay the labels over the image and use interactive sdimage plot in PRSD Studio:
% (
we use sdrelab to convert numerical lablist into strings)
>> 
sdimage(sdrelab(a2))

Blue color represents the class 0 and red class 1.

Hope it helps,

Pavel

[ Edited: 03 June 2009 08:36 PM by pavel]
Image Attachments
roadsign.jpeg
Profile
 
 
Posted: 04 June 2009 06:29 PM   [ Ignore ]   [ # 2 ]  
Newbie
Rank
Total Posts:  5
Joined  2009-05-19

Thank you very much for the help, Pavel. I tested and the function setlabel() worked. However, i still didn’t get to classify my image. Look what i m doing:

%read original image for training
foTrein=imread(’Pac2_03_part1.tif’);
%make dataset without label
dsTrein=im2feat(foTrein);
%read labeled image
fr=imread(’Pac2_03_rot_part1.tif’);
%removes noise of the labeled image
fr=TiraRuido(fr);
%generate matrix of the labels
Labels = reshape(fr,size(fr,1)*size(fr,2),1);
% add the labels in the image´s dataset
dsTreinLab=setlabels(dsTrein,Labels);
%classify the image using UDC (Uncorrelated discriminat Classifier)
classif_udc=udc(dsTreinLab);

%read original image for testing
foTeste=imread(’Pac3_02_part1.tif’);
%it builds dataset without labels
dsTeste=im2feat(foTeste);
%use of the classifier udc generated in training
TestClass_udc=dsTeste*classif_udc;
% trying show classified image
figure, show(TestClass_udc);

I thank for any help, because I don’t find a lot of people for here working with PRTools with whom I can change ideas.

PS.: my images are tif and didn’t get to post

[ Edited: 04 June 2009 06:36 PM by kesede]
Profile
 
 
Posted: 04 June 2009 06:48 PM   [ Ignore ]   [ # 3 ]  
Administrator
Avatar
RankRankRankRank
Total Posts:  236
Joined  2008-04-26

Hi kesede,

no problem. TestClass_udc is a dataset with a column for each class. Each of the columns contains estimated class-conditional probability density. This is called “soft-output” of the classifier. In order to turn this soft output into crisp decisions (i.e. to really assign pixels in test image into classes you trained), you can apply labeld on the TestClass_udc dataset:

>> dec=TestClass_udc*labeld;
>> 
figureshow(reshape(dec,TestClass_udc.objsize))

You can make the decision and reshape in one step:

>> dec=classim(TestClass_udc);
>> 
figureshow(dec)

Does it help?

Pavel

Profile
 
 
Posted: 05 June 2009 05:18 PM   [ Ignore ]   [ # 4 ]  
Newbie
Rank
Total Posts:  5
Joined  2009-05-19

Hello Pavel (again)!! His help was fundamental. Thank you very much!!! I don’t just understand because the code below results in same images, in my case

% suggestion of Pavel
dec=TestClass_qdc*labeld;
figure, show(reshape(dec,TestClass_qdc.objsize)), title(’Image after labeld’);

dec=classim(TestClass_qdc);
figure, show(dec), title(’Image after classim’);

Profile
 
 
Posted: 05 June 2009 05:34 PM   [ Ignore ]   [ # 5 ]  
Administrator
Avatar
RankRankRankRank
Total Posts:  236
Joined  2008-04-26

Hi Kesede,

yes. I was just trying to show how you can use classim to avoid the manual reshape of vector of decisions into image of decisions.

Keep well,

Pavel

Profile
 
 
Posted: 22 June 2009 09:37 PM   [ Ignore ]   [ # 6 ]  
Newbie
Rank
Total Posts:  5
Joined  2009-05-19

Hi, Pavel!! I have some images and want add them in a single dataset (idem for labels). I tryed to use im2feat(), but not worked. Can you help myself?

thank you!!
Kesede

Profile
 
 
Posted: 22 June 2009 10:26 PM   [ Ignore ]   [ # 7 ]  
Moderator
RankRankRankRank
Total Posts:  250
Joined  2008-11-08

Hello Kesede,

May be the following example helps:

>> im1 = repmat([0:63],64,1);
>> im2 = repmat([0:63]’,1,64);
>> im3 = repmat([zeros(1,32) 63*ones(1,32)],64,1);
>> a = im2feat({im1,im2,im3})
>> b = im2feat({im3,im1,im2})
>> figure; show(a)
>> figure; show(b)
>> figure; show(a+b)
>> showfigs

Best regards,

Bob

Profile
 
 
Posted: 24 July 2009 08:04 PM   [ Ignore ]   [ # 8 ]  
Newbie
Rank
Total Posts:  5
Joined  2009-05-19

Hi, Bob!! I have a directory of images of data and another of labels images, and I want to accumulate (data and labels) in a simple dataset, through a looping. I tried this:

%reads the directory of training images, generating vector of names
ImTR=dir(’F:\Imagens\TreinamentoRot\*.tif’); %labels
ImTO=dir(’F:\Imagens\Treinamento\*.tif’); % originals
dsTrein=dataset(); % creates empty dataset

for i=1:size(ImTO,1)
%reads original image
im=imread(ImTO(i).name);
%creates dataset without labels
dsTrein=im2feat(im,dsTrein);
%reads label image
frTrein=imread(ImTR(i).name);
%generates labels vector
LabelsTrein = reshape(frTrein,size(frTrein,1)*size(frTrein,2),1);
%joins the labels vector to the dataset
dsTreinLab=setlabels(dsTrein,LabelsTrein);
end

but, the PRTools returns me this:

??? Error using ==> im2feat
Image size and dataset object size do not match

Error in ==> classifica at 21
dsTrein=im2feat(im,dsTrein);

Can you help myself?

Thanks a lot

Profile
 
 
Posted: 24 July 2009 11:52 PM   [ Ignore ]   [ # 9 ]  
Moderator
RankRankRankRank
Total Posts:  250
Joined  2008-11-08

Hi Kesede,

If you label the images then they are objects and the command im2obj should be used.
Im2feat interprets images as features and pixels as objects. So it needs as many labels
as pixels.

To simplify procedures like the above and to handle large sets of images, the datafile
construct has been created. Perhaps it is also of use here.

Regards,

Bob

Profile