Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
problems in image processing

Subject: problems in image processing

From: stephanie borg

Date: 7 Apr, 2011 23:56:04

Message: 1 of 23

I have these 2 programms for image processing however they are giving me an error. I guess it has to do with the reshape function. can somebody help me please?
As images I will load 4 photos , each containging 6(vertical) by 10 (horizontal) small sketches
MAIN FILE:

I3D1 = imread('IMG_0001.jpg');
I3D2 = imread('IMG_0002.jpg');
I3D3 = imread('IMG_0003.jpg');
I3D4 = imread('IMG_0004.jpg');

%These are used for testing the ANN

% I3D4 = imread('3Dopr4.jpg');
% I3D5 = imread('3Dopr5.jpg');

size3Dr = input('Enter number of training sets');
size3Dc = input('Enter number of different symbols');

sizer = size3Dr; %Assuming equal traning sets
sizec = size3Dc;

noimages = sizer/6;

%% Binarization of images of factory m/c symbols

Igray1 = rgb2gray(I3D1);
Ibw3D1 = im2bw(Igray1,graythresh(Igray1));

Igray2 = rgb2gray(I3D2);
Ibw3D2 = im2bw(Igray2,graythresh(Igray2));

Igray3 = rgb2gray(I3D3);
Ibw3D3 = im2bw(Igray3,graythresh(Igray3));

Igray4 = rgb2gray(I3D4);
Ibw3D4 = im2bw(Igray3,graythresh(Igray4));

I3Dtotal(:,:,1) = Ibw3D1;
I3Dtotal(:,:,2) = Ibw3D2;
I3Dtotal(:,:,3) = Ibw3D3;
I3Dtotal(:,:,4) = Ibw3D4;

rfactor = 1;
crpval = 0;

for i = 1 : noimages
    
    I = I3Dtotal(:,:,i);
        
    img = sym_rec_img_preprocess(I, size3Dr, size3Dc, noimages);

    for cnt = 1:((size3Dr*size3Dc)/noimages)
        bw2 = img_crop(img{cnt}, rfactor, crpval);
        charvec = img_resize(not(bw2));
        out3D(:,cnt,i) = charvec;
    end

end


THE SECOND FILE : SYM_REC_IMG_PREPROCESS

function img = sym_rec_img_preprocess(I, sizer, sizec, noimages)

%Noise removal

Ibw = bwareaopen(not(I),10,8);

se = strel('disk',2);

Iedge2 = imdilate(Ibw, se);

Tempel = double(Iedge2);

[BWnr, nnr] = bwlabel(Tempel);

Iprops = regionprops(BWnr);

Ibox = [Iprops.BoundingBox];
Ibox = reshape(Ibox,[4 (((sizer*sizec)/noimages)+1)]);

Ic = [Iprops.Centroid];
Ic = reshape(Ic,[2 (((sizer*sizec)/noimages)+1)]);
%Ic = reshape(Ic,[2 ((sizer*sizec)/noimages)+1]);
Ic = Ic';
Ic(:,3) = (mean(Ic.^2,2)).^(1/2);
Ic(:,4) = 1:(((sizer*sizec)/noimages)+1);

Ic2 = sortrows(Ic,2);

for cnt = 1:(sizer/noimages)
    Ic2((cnt-1)*(sizec-1)+cnt:cnt*sizec,:) = sortrows(Ic2((cnt-1)*(sizec-1)+cnt:cnt*sizec,:),4);
end

Ic3 = Ic2(:,1:2);
ind = Ic2(:,4);

for cnt = 1:((sizer*sizec)/noimages)
    img{cnt} = imcrop(Ibw,Ibox(:,ind(cnt)));
end

Subject: problems in image processing

From: ImageAnalyst

Date: 8 Apr, 2011 00:47:57

Message: 2 of 23

Did I not find it, or did you forget to post the error? All I can
suggest is to learn how to use the debugger, and make sure
(sizer*sizec)/noimages)+1 is an integer. Extract out that part into
its own variable if that will help you figure out what it is.

Subject: problems in image processing

From: Matt Fig

Date: 8 Apr, 2011 01:16:04

Message: 3 of 23

The more information people have, the better they can help you. Describe in detail the error, and the sizes of all your images. The more relevent detail you leave out, the less likely someone can help you!

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 01:19:04

Message: 4 of 23

Sorry i didn't understand what you said. the error it gives me is:

To RESHAPE the number of elements must not change.

Error in ==> sym_rec_img_preprocess at 33
Ibox = reshape(Ibox,[4 (((sizer*sizec)/noimages)+1)]);

Error in ==> sym_rec_main2 at 78
    img = sym_rec_img_preprocess(I, size3Dr, size3Dc, noimages);

Subject: problems in image processing

From: Nasser M. Abbasi

Date: 8 Apr, 2011 01:23:56

Message: 5 of 23

On 4/7/2011 6:19 PM, stephanie borg wrote:
> Sorry i didn't understand what you said. the error it gives me is:
>
> To RESHAPE the number of elements must not change.
>
> Error in ==> sym_rec_img_preprocess at 33
> Ibox = reshape(Ibox,[4 (((sizer*sizec)/noimages)+1)]);
>
> Error in ==> sym_rec_main2 at 78
> img = sym_rec_img_preprocess(I, size3Dr, size3Dc, noimages);


may be this example below will help give you an idea why you get
this error message

-------------------------
A=zeros(4,4);
reshape(A,[5 5])

??? Error using ==> reshape
To RESHAPE the number of elements must not change.
---------------------------

--Nasser

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 01:53:05

Message: 6 of 23

but they are the same value
Ibox = [Iprops.BoundingBox]; %Ibox = 244
Ibox = reshape(Ibox,[4 (((sizer*sizec)/noimages)+1)]); %sizer 24 sizec 10 noimages 4

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 01:54:05

Message: 7 of 23

but they are the same value
Ibox = [Iprops.BoundingBox]; %Ibox = 244
Ibox = reshape(Ibox,[4 (((sizer*sizec)/noimages)+1)]); %sizer 24 sizec 10 noimages 4

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 01:55:19

Message: 8 of 23

but they are the same value

Ibox = [Iprops.BoundingBox]; %Ibox = 244
Ibox = reshape(Ibox,[4 (((sizer*sizec)/noimages)+1)]); %sizer = 24 sizec = 10 noimages =4

Subject: problems in image processing

From: Nasser M. Abbasi

Date: 8 Apr, 2011 02:10:57

Message: 9 of 23

On 4/7/2011 6:53 PM, stephanie borg wrote:
> but they are the same value
> Ibox = [Iprops.BoundingBox]; %Ibox = 244
> Ibox = reshape(Ibox,[4 (((sizer*sizec)/noimages)+1)]); %sizer 24 sizec 10 noimages 4


Ok, then it is strange you get the error

"To RESHAPE the number of elements must not change."

At that point. May be this error is coming from a different line then.

good luck,

--Nasser

Subject: problems in image processing

From: Matt Fig

Date: 8 Apr, 2011 02:12:04

Message: 10 of 23

Your code reads 4 images at the start. Please answer both of these questions:

What are the sizes of ALL 4 OF THOSE IMAGES?
What are you inputting for these lines:

size3Dr = input('Enter number of training sets');
size3Dc = input('Enter number of different symbols');

This information is needed to figure out what is going on with the code.

Subject: problems in image processing

From: ImageAnalyst

Date: 8 Apr, 2011 02:15:03

Message: 11 of 23

stephanie:

Look at these lines:
    [BWnr, nnr] = bwlabel(Tempel);
    Iprops = regionprops(BWnr);
    Ibox = [Iprops.BoundingBox];
    Ibox = reshape(Ibox,[4 (((sizer*sizec)/noimages)+1)]);
Don't you realize that lbox has nothing whatsoever to do with
noimages? In fact, lbox will be 4*nnr elements long and that ONLY
depends on the blobs found in your one image that you labeled. And
that image is the (badly named) I. And "I" has nothing at all to do
with noimages. Who knows what sizer*sizec)/noimages)+1 is but, like I
suggested in my first reply, I'm very certain it has nothing to do
with the number of elements in lbox since it has noimages in it, which
we just said is irrelevant. And it's probably a fraction to boot!
That's why I first said to extract out that expression into its own
variable and look at it when you're stopped in the debugger. I do
this stuff all day long for a living and I'm virtually certain that is
the cause of your problem.

If you're trying to crop out sub-images, then take a look at my demo
where I do that:
http://www.mathworks.com/matlabcentral/fileexchange/25157

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 02:18:04

Message: 12 of 23

> What are the sizes of ALL 4 OF THOSE IMAGES? all 1664 x 1096
> What are you inputting for these lines:
>
> size3Dr = input('Enter number of training sets'); 24 (6 rows x 4 images)
> size3Dc = input('Enter number of different symbols');10 ( 10 symbols in each row)
>

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 02:32:04

Message: 13 of 23

sorry but i can't understand you..as you can see im not really into matlab. i didnt do this program myself, i found i found it online and i read it should work...now i dont know what to do...

Subject: problems in image processing

From: ImageAnalyst

Date: 8 Apr, 2011 02:43:30

Message: 14 of 23

On Apr 7, 10:32 pm, "stephanie borg" <borg.s...@gmail.com> wrote:
> sorry but i can't understand you..as you can see im not really into matlab. i didnt do this program myself, i  found i found it online and i read it should work...now i dont know  what to do...

----------------------------------------------------
Then keep reading it over and over again until you do understand,
because that is the source of your problem. You must realize that you
are trying to make an array of 4 rows by sizer*sizec)/noimages)+1
columns and that does not match the number of bounding boxes that you
have because noimages has ABSOLUTELY NOTHING to do with the number of
objects in your image that you labeled. Let's say your image I has 42
blobs in it. Then you will have 42 bounding boxes. Now I don't care
if you have 4 images, 113 images, 173 images or a million images- that
has nothing to do with the fact that image "I" has exactly 42 bounding
boxes in it.

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 02:50:07

Message: 15 of 23

okk i will try to understand later cause its 5 am here and im tired...thanks but!

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 12:30:20

Message: 16 of 23

> ----------------------------------------------------
> Then keep reading it over and over again until you do understand,
> because that is the source of your problem. You must realize that you
> are trying to make an array of 4 rows by sizer*sizec)/noimages)+1
> columns and that does not match the number of bounding boxes that you
> have because noimages has ABSOLUTELY NOTHING to do with the number of
> objects in your image that you labeled. Let's say your image I has 42
> blobs in it. Then you will have 42 bounding boxes. Now I don't care
> if you have 4 images, 113 images, 173 images or a million images- that
> has nothing to do with the fact that image "I" has exactly 42 bounding
> boxes in it.

-------------------------------
I understand what you are saying, however for sizer i will insert 24 and for sizec i insert 10 therefore with ((sizer*sizec)/noimages)+1 i will get the number of bounding boxex in 1 photo

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 18:11:05

Message: 17 of 23

 I understand what you are saying, however for sizer i will insert 24 and for sizec i insert 10 therefore with ((sizer*sizec)/noimages)+1 i will get the number of bounding boxex in 1 photo

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 18:20:05

Message: 18 of 23

yes I understand what you are saying, however for sizer i will insert 24 and for sizec i insert 10 therefore with ((sizer*sizec)/noimages)+1 i will get the number of bounding boxex in 1 photo

Subject: problems in image processing

From: ImageAnalyst

Date: 8 Apr, 2011 18:50:21

Message: 19 of 23

On Apr 8, 2:20 pm, "stephanie borg" <borg.s...@gmail.com> wrote:
> yes I understand what you are saying, however for sizer i will insert 24 and for sizec i insert 10 therefore with ((sizer*sizec)/noimages)+1 i will get the number of bounding boxex in 1 photo
-----------------------------------------------------------------------------------------------------------------------------------
No, sorry but I don't agree with that at all. "I" is a 2D image -
it's a slice taken from your 3D image. So let's say that sizer is 24
and sizec is 10, and noimages was 4, so (24*10)/4 + 1 = 61. So what
makes you think that each of those images will have exactly 61 objects
(61 bounding boxes) in them? Maybe image 1 has 13 blobs in it and
image 2 has 38 blobs in it, and image 3 has 149 blobs in it, and image
4 has 182 blobs in it. Again, why do you think they should all have
exactly 61 blobs???
ImageAnalyst

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 19:23:04

Message: 20 of 23

well i am doing a student project not an advanced one. i made sure that each sketch is a one connected component and that there are no marks on the paper. or this doesn't have to do with what you're saying?

Subject: problems in image processing

From: ImageAnalyst

Date: 8 Apr, 2011 19:35:09

Message: 21 of 23

On Apr 8, 3:23 pm, "stephanie borg" <borg.s...@gmail.com> wrote:
> well i am doing a student project not an advanced one.  i made sure that each sketch is a one connected component and that there are no marks on the paper. or this doesn't have to do with what you're saying?

-------------------------------------------------------------------------
That doesn't matter - your code won't work unless you have 61
connected components on each paper. Time to upload your images
somewhere so someone can fix your code. Pick your favorite file
hosting web site and then tell us the URL. I'm leaving soon so I
can't get to it until Sunday night at the earliest, but maybe someone
else can.
ImageAnalyst

Subject: problems in image processing

From: stephanie borg

Date: 8 Apr, 2011 19:51:20

Message: 22 of 23

ok i did thanks. i did a new message though

Subject: problems in image processing

From: gnk432@gmail.com

Date: 5 Oct, 2012 06:15:39

Message: 23 of 23

hii....im doing project i gat output also....i used this code in my project....but i d't know these code expanation plz may i know these code explanation ...plz give me reply.....
function lett = edu_imgresize(bw2)
% This function will take the cropped binary image and change it to 5 x 7 % character representation in single vector.
bw_7050=imresize(bw2,[70,50]);
for cnt=1:7
for cnt2=1:5
Atemp=sum(bw_7050((cnt*10-9:cnt*10),(cnt2*10-9:cnt2*10)));
lett((cnt-1)*5+cnt2)=sum(Atemp);
 end end lett=((100-lett)/100); lett=lett';.....

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us