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:
divide 1280-by-960 image into 9-by-9

Subject: divide 1280-by-960 image into 9-by-9

From: areeba khan

Date: 26 May, 2011 18:01:05

Message: 1 of 27

i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
??? Error using ==> mat2cell
Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
this is my code
i=imread('E:\cancer images\case 091610\20X1.jpg');
a=rgb2gray(i);
b=imadjust(a);
v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
mat1=b(725:733,231:239);
c=mat1;
for t=1:size(c,2)
    for u=1:size(c,1)
        y=xcorr(c(:),b(:));
       if y>=0.7
           disp('Tumor detected')
           else
           disp('tumor not found')
       end
    end
end
please tell me if i am doing right?
tell me some way how i can divide the real image 1280-by-960 into 9-by-9?

Subject: divide 1280-by-960 image into 9-by-9

From: Jeff

Date: 26 May, 2011 18:29:20

Message: 2 of 27

"areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> ??? Error using ==> mat2cell
> Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> this is my code
> i=imread('E:\cancer images\case 091610\20X1.jpg');
> a=rgb2gray(i);
> b=imadjust(a);
> v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> mat1=b(725:733,231:239);
> c=mat1;
> for t=1:size(c,2)
> for u=1:size(c,1)
> y=xcorr(c(:),b(:));
> if y>=0.7
> disp('Tumor detected')
> else
> disp('tumor not found')
> end
> end
> end
> please tell me if i am doing right?
> tell me some way how i can divide the real image 1280-by-960 into 9-by-9?

Well, you're getting an error, so I'm going to go out on a limb and say you're not doing it right. ;)

If you had an 18x18 pixel image, and wanted to break it into 4 9x9 cells, you could use the mat2cell command:
b= magic(18);
c = mat2cell(b, [9 9], [9 9]);

But what you want is probably more along the lines of the blockproc function (http://www.mathworks.com/help/toolbox/images/ref/blockproc.html).

Subject: divide 1280-by-960 image into 9-by-9

From: areeba khan

Date: 26 May, 2011 18:53:02

Message: 3 of 27

"Jeff " <jea@gene.dot.com> wrote in message <irm660$ok7$1@newscl01ah.mathworks.com>...
> "areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> > i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> > ??? Error using ==> mat2cell
> > Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> > this is my code
> > i=imread('E:\cancer images\case 091610\20X1.jpg');
> > a=rgb2gray(i);
> > b=imadjust(a);
> > v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> > mat1=b(725:733,231:239);
> > c=mat1;
> > for t=1:size(c,2)
> > for u=1:size(c,1)
> > y=xcorr(c(:),b(:));
> > if y>=0.7
> > disp('Tumor detected')
> > else
> > disp('tumor not found')
> > end
> > end
> > end
> > please tell me if i am doing right?
> > tell me some way how i can divide the real image 1280-by-960 into 9-by-9?
>
> Well, you're getting an error, so I'm going to go out on a limb and say you're not doing it right. ;)
>
> If you had an 18x18 pixel image, and wanted to break it into 4 9x9 cells, you could use the mat2cell command:
> b= magic(18);
> c = mat2cell(b, [9 9], [9 9]);
>
> But what you want is probably more along the lines of the blockproc function (http://www.mathworks.com/help/toolbox/images/ref/blockproc.html).


i m not getting your point. as far as i get it, the use of magic command doesn't seem to be relevant to my task...can u please guide me how can i break the image (960-by-1280) into a 9-by-9 matrix?

Subject: divide 1280-by-960 image into 9-by-9

From: areeba khan

Date: 26 May, 2011 18:58:04

Message: 4 of 27

"Jeff " <jea@gene.dot.com> wrote in message <irm660$ok7$1@newscl01ah.mathworks.com>...
> "areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> > i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> > ??? Error using ==> mat2cell
> > Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> > this is my code
> > i=imread('E:\cancer images\case 091610\20X1.jpg');
> > a=rgb2gray(i);
> > b=imadjust(a);
> > v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> > mat1=b(725:733,231:239);
> > c=mat1;
> > for t=1:size(c,2)
> > for u=1:size(c,1)
> > y=xcorr(c(:),b(:));
> > if y>=0.7
> > disp('Tumor detected')
> > else
> > disp('tumor not found')
> > end
> > end
> > end
> > please tell me if i am doing right?
> > tell me some way how i can divide the real image 1280-by-960 into 9-by-9?
>
> Well, you're getting an error, so I'm going to go out on a limb and say you're not doing it right. ;)
>
> If you had an 18x18 pixel image, and wanted to break it into 4 9x9 cells, you could use the mat2cell command:
> b= magic(18);
> c = mat2cell(b, [9 9], [9 9]);
>
> But what you want is probably more along the lines of the blockproc function (http://www.mathworks.com/help/toolbox/images/ref/blockproc.html).

as far as i get it, the use of magic command doesn't seem to be relevant for my task..can u please guide me how can i divide/break the 960-by-1280 image into 9-by-9 cells?

Subject: divide 1280-by-960 image into 9-by-9

From: Florin Neacsu

Date: 26 May, 2011 19:12:05

Message: 5 of 27

"areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> ??? Error using ==> mat2cell
> Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> this is my code
> i=imread('E:\cancer images\case 091610\20X1.jpg');
> a=rgb2gray(i);
> b=imadjust(a);
> v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> mat1=b(725:733,231:239);
> c=mat1;
> for t=1:size(c,2)
> for u=1:size(c,1)
> y=xcorr(c(:),b(:));
> if y>=0.7
> disp('Tumor detected')
> else
> disp('tumor not found')
> end
> end
> end
> please tell me if i am doing right?
> tell me some way how i can divide the real image 1280-by-960 into 9-by-9?

Hello,

May I suggest something ? Please reformulate your question. Looking at your words and then at your code is very confusing !

Are you trying to analyze a 9x9 chunk of the big matrix ? What are you going to do at the borders? Where are you using "v" in your code , after that attempt of allocation? Why are you doing 81 times the same operation ? ...

Regards,
Florin

Subject: divide 1280-by-960 image into 9-by-9

From: Jeff

Date: 26 May, 2011 19:50:14

Message: 6 of 27

"areeba khan" wrote in message <irm7rs$lq$1@newscl01ah.mathworks.com>...
> "Jeff " <jea@gene.dot.com> wrote in message <irm660$ok7$1@newscl01ah.mathworks.com>...
> > "areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> > > i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> > > ??? Error using ==> mat2cell
> > > Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> > > this is my code
> > > i=imread('E:\cancer images\case 091610\20X1.jpg');
> > > a=rgb2gray(i);
> > > b=imadjust(a);
> > > v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> > > mat1=b(725:733,231:239);
> > > c=mat1;
> > > for t=1:size(c,2)
> > > for u=1:size(c,1)
> > > y=xcorr(c(:),b(:));
> > > if y>=0.7
> > > disp('Tumor detected')
> > > else
> > > disp('tumor not found')
> > > end
> > > end
> > > end
> > > please tell me if i am doing right?
> > > tell me some way how i can divide the real image 1280-by-960 into 9-by-9?
> >
> > Well, you're getting an error, so I'm going to go out on a limb and say you're not doing it right. ;)
> >
> > If you had an 18x18 pixel image, and wanted to break it into 4 9x9 cells, you could use the mat2cell command:
> > b= magic(18);
> > c = mat2cell(b, [9 9], [9 9]);
> >
> > But what you want is probably more along the lines of the blockproc function (http://www.mathworks.com/help/toolbox/images/ref/blockproc.html).
>
> as far as i get it, the use of magic command doesn't seem to be relevant for my task..can u please guide me how can i divide/break the 960-by-1280 image into 9-by-9 cells?

Did you look at the blockproc function? Particularly the example at:
http://www.mathworks.com/help/toolbox/images/f7-12726.html#brcu20d-1
You can call your comparison metric as the function, but getting your raw image and index into the same image may be tricky.

Failing that, you'll first need to figure out what you want to do with the remainders, since 9 is not evenly divisible into 960 nor 1280. I would suggest using padarray.

After you have something that is evenly divisible, take a look at the code below. It performs an arbitrary operation, imwrite, where you would want to put your comparison:

grayimg = rgb2gray(imagein);

padimg = padarray(grayimg, [3 4], 0, 'post'); %modify these values to make your image dimensions divisible by 9

height = size(padimg,1); %height of full image
width = size(padimg,2); %width of full image

n_width = 9;
m_height = 9;

for x = 1:m_height:height
    for y=1:n_width:width
        image = bigmont(y:y+m_height-1, x:x+n_width-1);
        imwrite(image,strcat(num2str(x),'^',num2str(y),'.jpg'),'jpg', 'Quality',80);
    end

end
 

Subject: divide 1280-by-960 image into 9-by-9

From: Roger Stafford

Date: 26 May, 2011 20:42:02

Message: 7 of 27

"areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> ??? Error using ==> mat2cell
> Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> this is my code
> i=imread('E:\cancer images\case 091610\20X1.jpg');
> a=rgb2gray(i);
> b=imadjust(a);
> v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> mat1=b(725:733,231:239);
> c=mat1;
> for t=1:size(c,2)
> for u=1:size(c,1)
> y=xcorr(c(:),b(:));
> if y>=0.7
> disp('Tumor detected')
> else
> disp('tumor not found')
> end
> end
> end
> please tell me if i am doing right?
> tell me some way how i can divide the real image 1280-by-960 into 9-by-9?
- - - - - - - - - -
  In my opinion it is unwise to partition your matrix b into separate non-overlapping 9 by 9 blocks. Doing things that way runs the high risk of possibly missing the particular 9 by 9 portion where correlation with your reference matrix is highest, and with something as vital as cancer you don't want to miss important clues.

  Using 'xcorr' with c(:) and b(:) as you have done is also highly inappropriate since you would be correlating c with the wrong 9-element groups within b. I would think that a single call on 'xcorr2' with arguments b and c (not reshaped to vectors) is far more suited to your needs. However, its output needs to be normalized to make it easier to interpret. You should make a careful study of the 'xcorr2' documentation. There is an example given there of how normalization can be done, but if you find that too difficult to achieve, you should ask about it here.

Roger Stafford

Subject: divide 1280-by-960 image into 9-by-9

From: ImageAnalyst

Date: 27 May, 2011 02:25:57

Message: 8 of 27

areeba khan:
Cross correlation is not a reliable way to do pattern matching. You
think that correlating your template over your image will give a spike
where your template is located in your main image. I know you think
that and I know you've been told that, and so was I, . . . and I even
thought that until I learned that it is not true. Heck, even the
MATLAB help gives this incorrect statement: "xcorr2 is the two-
dimensional version of xcorr. It has its maximum value when the two
matrices are aligned so that they are shaped as similarly as
possible." That is not true in general, only for certain cases. The
code below demonstrates a case where it's not true:

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

% Script to demonstrate that the max value of a cross correlation
% is not always where the template is located.
clc;
clear all;
workspace;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Check that user has the Signal Processing Toolbox installed.
hasRequiredToolbox = license('test', 'signal_toolbox');
if ~hasRequiredToolbox
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Signal
Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end

% Define image 1
I1 = [1 2 3 100 100
     11 5 8 100 100
      4 6 13 100 100
     10 12 15 100 100
    100 100 100 100 100]

%Define image 2 = I1(2:4, 1:3)
I2 = [11 5 8
       4 6 13
      10 12 15]

% First do the regular cross correlation.
c = xcorr2(I1, I2)

% I2 is aligned with I1 at row 4, column 3
% of the cross correlation matrix.
% Remember the cross correlation matrix is bigger than either one.
% Let's see if the max is really at row 4, column 3,
% where you think it should be.
[maxRow maxCol] = find(c == max(c(:)))
% Nope, it's not. Hmmmmm.....

% OK that didn't turn out as expected.
% So now let's try "normalized" cross correlation.
% Normalize each image.
i1n = (I1 - mean2(I1)) / std(I1(:))
i2n = (I2 - mean2(I2)) / std(I2(:))
% Now do the cross correlation with the normalized versions.
cn = xcorr2(i1n, i2n)
% See if it is max at the aligned spot.
[maxRow maxCol] = find(cn == max(cn(:)))
% Nope, it's not either.
% What have we learned here?

====================================
I1 =
     1 2 3 100 100
    11 5 8 100 100
     4 6 13 100 100
    10 12 15 100 100
   100 100 100 100 100


I2 =
    11 5 8
     4 6 13
    10 12 15


c =
          15 42 79 1556 2730
2200 1000
         178 239 345 2972 4692
3200 1400
         211 290 530 3921 6095
4800 2500
         290 497 900 4097 6190
4800 2500
        1662 2984 4185 6069 7103
4800 2500
        1380 2046 2590 3307 3765
2600 1500
         800 1300 2400 2400 2400
1600 1100

maxRow =
     5

maxCol =
     5

i1n =
   -1.1550 -1.1339 -1.1127 0.9393 0.9393
   -0.9435 -1.0704 -1.0070 0.9393 0.9393
   -1.0916 -1.0493 -0.9012 0.9393 0.9393
   -0.9646 -0.9223 -0.8589 0.9393 0.9393
    0.9393 0.9393 0.9393 0.9393 0.9393


i2n =
    0.4377 -1.1380 -0.3502
   -1.4006 -0.8754 0.9629
    0.1751 0.7003 1.4881


cn =
   -1.7189 -2.4962 -2.6522 0.4200 1.8607 0.8222 0.1644
   -2.5162 -2.3344 -0.8744 3.9718 3.5199 -1.3155 -1.1511
   -2.1285 -0.8192 0.1963 4.3092 1.5055 -1.9733 -0.7400
   -2.1562 -0.6544 0.6444 4.1462 1.4111 -1.9733 -0.7400
    0.8511 3.6214 4.5832 5.4054 1.7129 -1.9733 -0.7400
    1.2422 1.5029 -0.3052 -0.9885 -3.0070 -2.7955 -0.9044
   -0.3289 -1.3977 -0.9866 -0.9866 -0.9866 -0.6578 0.4111

maxRow =
     5

maxCol =
     4

Subject: divide 1280-by-960 image into 9-by-9

From: Roger Stafford

Date: 27 May, 2011 04:19:04

Message: 9 of 27

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5d9de233-1504-487b-9663-61aea7de2bf8@s9g2000yqm.googlegroups.com>...
> areeba khan:
> Cross correlation is not a reliable way to do pattern matching. .....
> .......
> % Define image 1
> I1 = [1 2 3 100 100
> 11 5 8 100 100
> 4 6 13 100 100
> 10 12 15 100 100
> 100 100 100 100 100]
>
> %Define image 2 = I1(2:4, 1:3)
> I2 = [11 5 8
> 4 6 13
> 10 12 15]
> ........
> % Now do the cross correlation with the normalized versions.
> ........
> cn =
> -1.7189 -2.4962 -2.6522 0.4200 1.8607 0.8222 0.1644
> -2.5162 -2.3344 -0.8744 3.9718 3.5199 -1.3155 -1.1511
> -2.1285 -0.8192 0.1963 4.3092 1.5055 -1.9733 -0.7400
> -2.1562 -0.6544 0.6444 4.1462 1.4111 -1.9733 -0.7400
> 0.8511 3.6214 4.5832 5.4054 1.7129 -1.9733 -0.7400
> 1.2422 1.5029 -0.3052 -0.9885 -3.0070 -2.7955 -0.9044
> -0.3289 -1.3977 -0.9866 -0.9866 -0.9866 -0.6578 0.4111
> ........
- - - - - - - - - -
  Hello ImageAnalyst. In the second of your examples here you have used 'xcorr2' on two matrices which have been normalized. However, there is a very important difference between doing things this way, and performing the kind of normalization that is described in the documentation for 'xcorr2', (even though they apparently don't actually perform that in the 'xcorr2' results, though there is an easy way of using 'xcorr2' so as to accomplish it.)

  To show what I mean, in your example above with I1 and I2, to get the entry at (3,3) they (Mathworks) describe how they would/could do normalization:

 (1*11+2*5+3*8+11*4+5*6+8*13+4*10+6*12+13*15) /
 sqrt(1^2+2^2+3^2+11^2+5^2+8^2+4^2+6^2+13^1) /
 sqrt(11^2+5^2+8^2+4^2+6^2+13^2+10^2+12^2+15^2) = 0.83748

The whole inner 3 x 3 set would be as follows.

c(3:5,3:5) =

   0.83748031821396 0.75076516439463 0.82775681371927
   1.00000000000000 0.77989724776256 0.83915561746709
   0.79629825385399 0.89956433917556 0.89238597884516

(Note that seven of these have involved the "100" entries.)

  As you see, the entry at (4,3) where I2 is identical to I1(2:4,1:3) is now maximum at 1.0000 while all others are less than 1. The entry at (5,4) which was the maximum at 5.4054 in your example is now down to a modest 0.899564, well below 1. It can be proved that 1 can only be achieved by identical matches. All values must lie between -1 and +1, (as any self-respecting correlation value ought to.)

  This is why, in my previous article in this thread, I recommended normalization to Areeba in the event that 'xcorr2' were to be used. I had this kind of normalization in mind.

Roger Stafford

Subject: divide 1280-by-960 image into 9-by-9

From: Roger Stafford

Date: 27 May, 2011 05:08:04

Message: 10 of 27

"Roger Stafford" wrote in message <irn8no$32h$1@newscl01ah.mathworks.com>...
> ...... It can be proved that 1 can only be achieved by identical matches. .....
- - - - - - - - -
  Where I said, "It can be proved that a 1 can only be achieved by identical matches", that is not correct. A correct statement is that a 1 can only be achieved by proportional matrices - that is, when all ratios between corresponding elements are equal.

Roger Stafford

Subject: divide 1280-by-960 image into 9-by-9

From: areeba khan

Date: 27 May, 2011 13:48:05

Message: 11 of 27

"Jeff " <jea@gene.dot.com> wrote in message <irmatm$gfc$1@newscl01ah.mathworks.com>...
> "areeba khan" wrote in message <irm7rs$lq$1@newscl01ah.mathworks.com>...
> > "Jeff " <jea@gene.dot.com> wrote in message <irm660$ok7$1@newscl01ah.mathworks.com>...
> > > "areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> > > > i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> > > > ??? Error using ==> mat2cell
> > > > Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> > > > this is my code
> > > > i=imread('E:\cancer images\case 091610\20X1.jpg');
> > > > a=rgb2gray(i);
> > > > b=imadjust(a);
> > > > v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> > > > mat1=b(725:733,231:239);
> > > > c=mat1;
> > > > for t=1:size(c,2)
> > > > for u=1:size(c,1)
> > > > y=xcorr(c(:),b(:));
> > > > if y>=0.7
> > > > disp('Tumor detected')
> > > > else
> > > > disp('tumor not found')
> > > > end
> > > > end
> > > > end
> > > > please tell me if i am doing right?
> > > > tell me some way how i can divide the real image 1280-by-960 into 9-by-9?
> > >
> > > Well, you're getting an error, so I'm going to go out on a limb and say you're not doing it right. ;)
> > >
> > > If you had an 18x18 pixel image, and wanted to break it into 4 9x9 cells, you could use the mat2cell command:
> > > b= magic(18);
> > > c = mat2cell(b, [9 9], [9 9]);
> > >
> > > But what you want is probably more along the lines of the blockproc function (http://www.mathworks.com/help/toolbox/images/ref/blockproc.html).
> >
> > as far as i get it, the use of magic command doesn't seem to be relevant for my task..can u please guide me how can i divide/break the 960-by-1280 image into 9-by-9 cells?
>
> Did you look at the blockproc function? Particularly the example at:
> http://www.mathworks.com/help/toolbox/images/f7-12726.html#brcu20d-1
> You can call your comparison metric as the function, but getting your raw image and index into the same image may be tricky.
>
> Failing that, you'll first need to figure out what you want to do with the remainders, since 9 is not evenly divisible into 960 nor 1280. I would suggest using padarray.
>
> After you have something that is evenly divisible, take a look at the code below. It performs an arbitrary operation, imwrite, where you would want to put your comparison:
>
> grayimg = rgb2gray(imagein);
>
> padimg = padarray(grayimg, [3 4], 0, 'post'); %modify these values to make your image dimensions divisible by 9
>
> height = size(padimg,1); %height of full image
> width = size(padimg,2); %width of full image
>
> n_width = 9;
> m_height = 9;
>
> for x = 1:m_height:height
> for y=1:n_width:width
> image = bigmont(y:y+m_height-1, x:x+n_width-1);
> imwrite(image,strcat(num2str(x),'^',num2str(y),'.jpg'),'jpg', 'Quality',80);
> end
>
> end
>

thank you Jeff..this might help me but can you please tell me what does bigmont do? as i m new to MATLAB and uptill now i have not found any such command..infact not in the help..i would be grateful if you please explain it..also please tell me can i take anyother value of quality

Subject: divide 1280-by-960 image into 9-by-9

From: areeba khan

Date: 27 May, 2011 13:58:02

Message: 12 of 27

"Florin Neacsu" wrote in message <irm8m5$58n$1@newscl01ah.mathworks.com>...
> "areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> > i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> > ??? Error using ==> mat2cell
> > Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> > this is my code
> > i=imread('E:\cancer images\case 091610\20X1.jpg');
> > a=rgb2gray(i);
> > b=imadjust(a);
> > v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> > mat1=b(725:733,231:239);
> > c=mat1;
> > for t=1:size(c,2)
> > for u=1:size(c,1)
> > y=xcorr(c(:),b(:));
> > if y>=0.7
> > disp('Tumor detected')
> > else
> > disp('tumor not found')
> > end
> > end
> > end
> > please tell me if i am doing right?
> > tell me some way how i can divide the real image 1280-by-960 into 9-by-9?
>
> Hello,
>
> May I suggest something ? Please reformulate your question. Looking at your words and then at your code is very confusing !
>
> Are you trying to analyze a 9x9 chunk of the big matrix ? What are you going to do at the borders? Where are you using "v" in your code , after that attempt of allocation? Why are you doing 81 times the same operation ? ...
>
> Regards,
> Florin

hello Florin,
yes, i think you got my point.
Actually, I have an image of size 960-by-1280 and i need to break this matrix into 9-by-9 so that i can compare or rather correlate it with a reference matrix of size 9-by-9 (given as mat1 in the code). As far as i know, the command mat2cell is used to break a big matrix into smaller cells that is why i m using this for the aforesaid purpose. i need to compare all the cells (obtained after breaking b, the big matrix, into smaller cells) with mat1. i know i m making some blunder but i hope now my query is clear to you.can you please guide me of some proper way to break a big matrix into smaller chunks and correlating it with the reference matrix?

thanks,
areeba

Subject: divide 1280-by-960 image into 9-by-9

From: areeba khan

Date: 27 May, 2011 14:23:05

Message: 13 of 27

"Roger Stafford" wrote in message <irmduq$qe2$1@newscl01ah.mathworks.com>...
> "areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> > i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> > ??? Error using ==> mat2cell
> > Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> > this is my code
> > i=imread('E:\cancer images\case 091610\20X1.jpg');
> > a=rgb2gray(i);
> > b=imadjust(a);
> > v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> > mat1=b(725:733,231:239);
> > c=mat1;
> > for t=1:size(c,2)
> > for u=1:size(c,1)
> > y=xcorr(c(:),b(:));
> > if y>=0.7
> > disp('Tumor detected')
> > else
> > disp('tumor not found')
> > end
> > end
> > end
> > please tell me if i am doing right?
> > tell me some way how i can divide the real image 1280-by-960 into 9-by-9?
> - - - - - - - - - -
> In my opinion it is unwise to partition your matrix b into separate non-overlapping 9 by 9 blocks. Doing things that way runs the high risk of possibly missing the particular 9 by 9 portion where correlation with your reference matrix is highest, and with something as vital as cancer you don't want to miss important clues.
>
> Using 'xcorr' with c(:) and b(:) as you have done is also highly inappropriate since you would be correlating c with the wrong 9-element groups within b. I would think that a single call on 'xcorr2' with arguments b and c (not reshaped to vectors) is far more suited to your needs. However, its output needs to be normalized to make it easier to interpret. You should make a careful study of the 'xcorr2' documentation. There is an example given there of how normalization can be done, but if you find that too difficult to achieve, you should ask about it here.
>
> Roger Stafford

thank you Mr. Stafford..i'll also try this way and if i find any difficulty i'll surely be asking you here.. but what makes me confused right now is that do i still need to break the big matrix into smaller cells or should i do it the way you are suggesting?

Subject: divide 1280-by-960 image into 9-by-9

From: ImageAnalyst

Date: 27 May, 2011 15:15:16

Message: 14 of 27

On May 27, 1:08 am, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "Roger Stafford" wrote in message <irn8no$32...@newscl01ah.mathworks.com>...
> >   ......  It can be proved that 1 can only be achieved by identical matches.  .....
>
> - - - - - - - - -
>   Where I said, "It can be proved that a 1 can only be achieved by identical matches", that is not correct.  A correct statement is that a 1 can only be achieved by proportional matrices - that is, when all ratios between corresponding elements are equal.
>
> Roger Stafford

-------------------------------------------------------------------------------------------------
Roger:
Wow, kind of tricky and unintuitive. Not sure I follow completely. I
didn't see any explanation in xcorr2's help about how to normalize
your input arrays before calling xcorr2, unless you're referring to
this terse entry:
    "The normalized cross-correlation of the (2,4) output element is
     585/sqrt(sum(dot(I1p,I1p))*sum(dot(I2,I2))) = 0.8070"
Is that what you did? Is that their "explanation"? I don't really
see from the above how that tells me to normalize my input arrays. If
that is what you need to do to fix up your inputs to get a spike at
the alignment spot, then I bet it will be overlooked by the vast
majority of people who will just blindly apply the correlation and
expect to see a spike at the "right" place. How would you fix up my
code to give a spike (value of 1.000) at the proper 4,3 location?
ImageAnalyst

Subject: divide 1280-by-960 image into 9-by-9

From: Jeff

Date: 27 May, 2011 15:48:04

Message: 15 of 27

"areeba khan" wrote in message <iroa2l$pr5$1@newscl01ah.mathworks.com>...
> "Jeff " <jea@gene.dot.com> wrote in message <irmatm$gfc$1@newscl01ah.mathworks.com>...
> > "areeba khan" wrote in message <irm7rs$lq$1@newscl01ah.mathworks.com>...
> > > "Jeff " <jea@gene.dot.com> wrote in message <irm660$ok7$1@newscl01ah.mathworks.com>...
> > > > "areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> > > > > i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> > > > > ??? Error using ==> mat2cell
> > > > > Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> > > > > this is my code
> > > > > i=imread('E:\cancer images\case 091610\20X1.jpg');
> > > > > a=rgb2gray(i);
> > > > > b=imadjust(a);
> > > > > v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> > > > > mat1=b(725:733,231:239);
> > > > > c=mat1;
> > > > > for t=1:size(c,2)
> > > > > for u=1:size(c,1)
> > > > > y=xcorr(c(:),b(:));
> > > > > if y>=0.7
> > > > > disp('Tumor detected')
> > > > > else
> > > > > disp('tumor not found')
> > > > > end
> > > > > end
> > > > > end
> > > > > please tell me if i am doing right?
> > > > > tell me some way how i can divide the real image 1280-by-960 into 9-by-9?
> > > >
> > > > Well, you're getting an error, so I'm going to go out on a limb and say you're not doing it right. ;)
> > > >
> > > > If you had an 18x18 pixel image, and wanted to break it into 4 9x9 cells, you could use the mat2cell command:
> > > > b= magic(18);
> > > > c = mat2cell(b, [9 9], [9 9]);
> > > >
> > > > But what you want is probably more along the lines of the blockproc function (http://www.mathworks.com/help/toolbox/images/ref/blockproc.html).
> > >
> > > as far as i get it, the use of magic command doesn't seem to be relevant for my task..can u please guide me how can i divide/break the 960-by-1280 image into 9-by-9 cells?
> >
> > Did you look at the blockproc function? Particularly the example at:
> > http://www.mathworks.com/help/toolbox/images/f7-12726.html#brcu20d-1
> > You can call your comparison metric as the function, but getting your raw image and index into the same image may be tricky.
> >
> > Failing that, you'll first need to figure out what you want to do with the remainders, since 9 is not evenly divisible into 960 nor 1280. I would suggest using padarray.
> >
> > After you have something that is evenly divisible, take a look at the code below. It performs an arbitrary operation, imwrite, where you would want to put your comparison:
> >
> > grayimg = rgb2gray(imagein);
> >
> > padimg = padarray(grayimg, [3 4], 0, 'post'); %modify these values to make your image dimensions divisible by 9
> >
> > height = size(padimg,1); %height of full image
> > width = size(padimg,2); %width of full image
> >
> > n_width = 9;
> > m_height = 9;
> >
> > for x = 1:m_height:height
> > for y=1:n_width:width
> > image = bigmont(y:y+m_height-1, x:x+n_width-1);
> > imwrite(image,strcat(num2str(x),'^',num2str(y),'.jpg'),'jpg', 'Quality',80);
> > end
> >
> > end
> >
>
> thank you Jeff..this might help me but can you please tell me what does bigmont do? as i m new to MATLAB and uptill now i have not found any such command..infact not in the help..i would be grateful if you please explain it..also please tell me can i take anyother value of quality

My apologies. I went through and renamed some of the variables to more generic names, and missed that one. Replace "bigmont" with "padimg".

Subject: divide 1280-by-960 image into 9-by-9

From: Florin Neacsu

Date: 27 May, 2011 17:04:04

Message: 16 of 27

"areeba khan" wrote in message <iroala$ric$1@newscl01ah.mathworks.com>...
> "Florin Neacsu" wrote in message <irm8m5$58n$1@newscl01ah.mathworks.com>...
> > "areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> > > i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... bt my program is giving this error
> > > ??? Error using ==> mat2cell
> > > Number of input vector arguments, 9, does not match the input matrix's number of dimensions, 2.
> > > this is my code
> > > i=imread('E:\cancer images\case 091610\20X1.jpg');
> > > a=rgb2gray(i);
> > > b=imadjust(a);
> > > v=MAT2CELL(b,1,2,3,4,5,6,7,8,9);
> > > mat1=b(725:733,231:239);
> > > c=mat1;
> > > for t=1:size(c,2)
> > > for u=1:size(c,1)
> > > y=xcorr(c(:),b(:));
> > > if y>=0.7
> > > disp('Tumor detected')
> > > else
> > > disp('tumor not found')
> > > end
> > > end
> > > end
> > > please tell me if i am doing right?
> > > tell me some way how i can divide the real image 1280-by-960 into 9-by-9?
> >
> > Hello,
> >
> > May I suggest something ? Please reformulate your question. Looking at your words and then at your code is very confusing !
> >
> > Are you trying to analyze a 9x9 chunk of the big matrix ? What are you going to do at the borders? Where are you using "v" in your code , after that attempt of allocation? Why are you doing 81 times the same operation ? ...
> >
> > Regards,
> > Florin
>
> hello Florin,
> yes, i think you got my point.
> Actually, I have an image of size 960-by-1280 and i need to break this matrix into 9-by-9 so that i can compare or rather correlate it with a reference matrix of size 9-by-9 (given as mat1 in the code). As far as i know, the command mat2cell is used to break a big matrix into smaller cells that is why i m using this for the aforesaid purpose. i need to compare all the cells (obtained after breaking b, the big matrix, into smaller cells) with mat1. i know i m making some blunder but i hope now my query is clear to you.can you please guide me of some proper way to break a big matrix into smaller chunks and correlating it with the reference matrix?
>
> thanks,
> areeba

Hello,

You should know that what matlab refers to as cell is not a "cell" as in a smaller component of the big matrix. Try this to see the difference :

>a=[1,2;3,4];
>b={1,2;3,4}
>whos a b

You can get you chunk of matrix by doing A(ii:ii+8,jj:jj+8).
Regarding the other issue, I must subscribe to the other previous thoughts in this thread. Analyzing small pieces is not a good idea. If you do not overlay the small pads you can be sure to have errors and if you overlay them you are doing too many calculations. I'm suggesting you should reconsider your approach.

Regards,
Florin

Subject: divide 1280-by-960 image into 9-by-9

From: areeba khan

Date: 27 May, 2011 21:38:04

Message: 17 of 27

"Jeff " <jea@gene.dot.com> wrote in message <iroh3k$io4$1@newscl01ah.mathworks.com>...
> "areeba khan" wrote in message <iroa2l$pr5$1@newscl01ah.mathworks.com>...
> > "Jeff " <jea@gene.dot.com> wrote in message <irmatm$gfc$1@newscl01ah.mathworks.com>...
> > > grayimg = rgb2gray(imagein);
> > >
> > > padimg = padarray(grayimg, [3 4], 0, 'post'); %modify these values to make your image dimensions divisible by 9
> > >
> > > height = size(padimg,1); %height of full image
> > > width = size(padimg,2); %width of full image
> > >
> > > n_width = 9;
> > > m_height = 9;
> > >
> > > for x = 1:m_height:height
> > > for y=1:n_width:width
> > > image = bigmont(y:y+m_height-1, x:x+n_width-1);
> > > imwrite(image,strcat(num2str(x),'^',num2str(y),'.jpg'),'jpg', 'Quality',80);
> > > end
> > >
> > > end
> > >
> My apologies. I went through and renamed some of the variables to more generic names, and missed that one. Replace "bigmont" with "padimg".

Jeff,
thanks for the code..but its reducing the image rather then dividing it..secondly its giving an error

??? Index exceeds matrix dimensions

i try to do it other way round..meanwhile if you have answer to this then please let me know..since i am new to matlab so i am definitely facing such problems..
thanks

Subject: divide 1280-by-960 image into 9-by-9

From: Jeff

Date: 27 May, 2011 23:08:04

Message: 18 of 27

"areeba khan" wrote in message <irp5js$htg$1@newscl01ah.mathworks.com>...
> "Jeff " <jea@gene.dot.com> wrote in message <iroh3k$io4$1@newscl01ah.mathworks.com>...
> > "areeba khan" wrote in message <iroa2l$pr5$1@newscl01ah.mathworks.com>...
> > > "Jeff " <jea@gene.dot.com> wrote in message <irmatm$gfc$1@newscl01ah.mathworks.com>...
> > > > grayimg = rgb2gray(imagein);
> > > >
> > > > padimg = padarray(grayimg, [3 4], 0, 'post'); %modify these values to make your image dimensions divisible by 9
> > > >
> > > > height = size(padimg,1); %height of full image
> > > > width = size(padimg,2); %width of full image
> > > >
> > > > n_width = 9;
> > > > m_height = 9;
> > > >
> > > > for x = 1:m_height:height
> > > > for y=1:n_width:width
> > > > image = bigmont(y:y+m_height-1, x:x+n_width-1);
> > > > imwrite(image,strcat(num2str(x),'^',num2str(y),'.jpg'),'jpg', 'Quality',80);
> > > > end
> > > >
> > > > end
> > > >
> > My apologies. I went through and renamed some of the variables to more generic names, and missed that one. Replace "bigmont" with "padimg".
>
> Jeff,
> thanks for the code..but its reducing the image rather then dividing it..secondly its giving an error
>
> ??? Index exceeds matrix dimensions
>
> i try to do it other way round..meanwhile if you have answer to this then please let me know..since i am new to matlab so i am definitely facing such problems..
> thanks

The "Index exceeds matrix dimensions" is probably because you need to change the "3" and "4" in:
padimg = padarray(grayimg, [3 4], 0, 'post');
to give you an image with dimensions evenly divisible by 9.

For example, you could make your image 963x1267 by using:
padimg = padarray(grayimg, [3 7], 0, 'post');

As far as "reducing the image"...the variable IMAGE is a 9x9 fragment of PADIMAGE that moves across the image, in a typewriter fashion, through PADIMAGE as the X and Y loops increment. Instead of calling IMWRITE, you could assign it to individual cells in a cell array: (http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-98.html#br1zng6-1). Does this make sense?

Subject: divide 1280-by-960 image into 9-by-9

From: Roger Stafford

Date: 28 May, 2011 01:00:04

Message: 19 of 27

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <19b3ec1e-ebf0-4b21-bf3c-6c3cddd59791@z13g2000yqg.googlegroups.com>...
> Roger:
> Wow, kind of tricky and unintuitive. Not sure I follow completely. I
> didn't see any explanation in xcorr2's help about how to normalize
> your input arrays before calling xcorr2, unless you're referring to
> this terse entry:
> "The normalized cross-correlation of the (2,4) output element is
> 585/sqrt(sum(dot(I1p,I1p))*sum(dot(I2,I2))) = 0.8070"
> Is that what you did? Is that their "explanation"? I don't really
> see from the above how that tells me to normalize my input arrays. If
> that is what you need to do to fix up your inputs to get a spike at
> the alignment spot, then I bet it will be overlooked by the vast
> majority of people who will just blindly apply the correlation and
> expect to see a spike at the "right" place. How would you fix up my
> code to give a spike (value of 1.000) at the proper 4,3 location?
> ImageAnalyst
- - - - - - - - - - -
  Hello ImageAnalyst and Areeba.

  I agree with you, ImageAnalyst, that the write-up on 'xcorr2' leaves much to be desired. Among other things the equation where they define C(i,j) has the indices going the wrong way. As i and j increase, they are actually moving backward in B, contrary to their claim in expression, B(m+i,n+j). Worst of all, they don't seem to provide for normalization even though they describe it. It isn't a matter of normalizing the input to 'xcorr2'. Normalization ought to be performed as an integral part of the processing within 'xcorr2', because the normalization requirements change with every shift in relative location between the two input arrays. In its absence the method below is a way of accomplishing this normalization even though it is rather cumbersome. It would be much more efficient if it were provided for as an option.

  Nevertheless I feel that 'xcorr2' could be very useful for your purposes, Areeba. Using your original notation, if 'b' is the adjusted 1280 by 960 image and 'mat1' is the reference matrix, you can do this:

 C = xcorr2(b,mat1) ./ ...
      sqrt(xcorr2(b.^2,ones(size(mat1))).*xcorr2(ones(size(b)),mat1.^2));
 C = C(9:1280,9:960);

  This finds the correlations, which will be numbers between +1 and -1, between 'mat1' and every 9 by 9 block within the array 'b'. The second operation removes any correlations in which 'mat1' extends out past the edges of 'b' where padded zeros were added. The documentation designates these remaining correlations as the "valid" ones. What you will be looking for are correlations that are fairly close to +1 among these.

  There are necessarily fewer elements in C than in 'b'. C(1,1) would be the correlation of the 9 by 9 'mat1' centered over the (5,5) element in 'b'. At the other extreme C(1272,952) is centered over (1276,956) in 'b'.

  I claim there is an advantage in this method in that every 9 by 9 block is tested, not just a particular random partitioning of the image. If the cancer pattern you are trying to match happens to fall half way between partitioned blocks or at the corners of four of them, you could easily miss it. In my opinion it is prudent to test every possible 9 by 9 block. Of course, that does mean a much lengthier processing time than would be spent with fewer blocks.

Roger Stafford

Subject: divide 1280-by-960 image into 9-by-9

From: ImageAnalyst

Date: 28 May, 2011 01:49:04

Message: 20 of 27

On May 27, 9:00 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
[snip]
> the normalization requirements change with every shift in relative location between the two input arrays....[snip] Using your original notation, if 'b' is the adjusted 1280 by 960 image and 'mat1' is the reference matrix, you can do this:
>
>  C = xcorr2(b,mat1) ./ ...
>       sqrt(xcorr2(b.^2,ones(size(mat1))).*xcorr2(ones(size(b)),mat1.^2));
[snip]

> Roger Stafford
----------------------------------------------------------------------------------
Roger:
I can't say that I followed how your formula allowed the arrays to
change normalization at every shift location, so I just decided to try
it with my data . . . and it worked (well mostly).

% Define image 1
I1 = [1 2 3 100 100
     11 5 8 100 100
      4 6 13 100 100
     10 12 15 100 100
    100 100 100 100 100]

%Define image 2 = I1(2:4, 1:3)
I2 = [11 5 8
       4 6 13
      10 12 15]

% First do the regular MATLAB cross correlation.
c = xcorr2(I1, I2)

% Now use Roger's formula for normalized cross correlation:
b=I1;
mat1 = I2;
C = xcorr2(b,mat1) ./ ...
     sqrt(xcorr2(b.^2,ones(size(mat1))).* ...
xcorr2(ones(size(b)),mat1.^2))

% I2 is aligned with I1 at row 4, column 3
% of the cross correlation matrix.
% Remember the cross correlation matrix is bigger than either one.
% Let's see if the max is really at row 4, column 3,
% where you think it should be.
[maxRow maxCol] = find(C == max(C(:)));

for k = 1:length(maxRow)
fprintf('Found a max at row=%d, col=%d\n',...
maxRow(k), maxCol(k));
end

Results:

I1 =

     1 2 3 100 100
    11 5 8 100 100
     4 6 13 100 100
    10 12 15 100 100
   100 100 100 100 100


I2 =

    11 5 8
     4 6 13
    10 12 15


c =

          15 42 79 1556 2730
2200 1000
         178 239 345 2972 4692
3200 1400
         211 290 530 3921 6095
4800 2500
         290 497 900 4097 6190
4800 2500
        1662 2984 4185 6069 7103
4800 2500
        1380 2046 2590 3307 3765
2600 1500
         800 1300 2400 2400 2400
1600 1100


C =

    1.0000 0.9778 0.9749 0.7180 0.8912 0.9959 1.0000
    0.8119 0.8118 0.8775 0.7980 0.8923 0.9300 0.9191
    0.8393 0.7905 0.8375 0.7508 0.8278 0.9321 0.9376
    0.8802 0.9181 1.0000 0.7799 0.8392 0.9321 0.9376
    0.7721 0.8135 0.7963 0.8996 0.8924 0.9321 0.9376
    0.8996 0.8387 0.7147 0.7928 0.8092 0.9239 0.9062
    1.0000 0.9744 0.9562 0.9562 0.9562 0.9363 1.0000

Found a max at row=1, col=1
Found a max at row=7, col=1
Found a max at row=4, col=3
Found a max at row=1, col=7
Found a max at row=7, col=7

So it does find the max at row 4, column 3 like it "is supposed to"
but it seems to get a 1.0000 at every corner also. Not sure why that
happens (off the top of my head), but if it's always at the 4 corners,
you could make sure to always just ignore those positions, for example
by cropping them off, which is what you did.

I think your solution is not all that obvious so perhaps you should
add it to the FAQ because I think a lot of people are puzzled when the
cross correlation does not produce the expected "spike" at the
expected location.

I agree with you that the documentation should be improved and that
this type of correlation should be made an option.
ImageAnalyst

Subject: divide 1280-by-960 image into 9-by-9

From: Roger Stafford

Date: 28 May, 2011 03:14:04

Message: 21 of 27

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <fd737d21-b2f1-48c5-8c89-299e0a76492b@b42g2000yqi.googlegroups.com>...
> Roger:
> I can't say that I followed how your formula allowed the arrays to
> change normalization at every shift location, so I just decided to try
> it with my data . . . and it worked (well mostly).
> ..........
> So it does find the max at row 4, column 3 like it "is supposed to"
> but it seems to get a 1.0000 at every corner also. Not sure why that
> happens (off the top of my head), but if it's always at the 4 corners,
> you could make sure to always just ignore those positions, for example
> by cropping them off, which is what you did.
>
> I think your solution is not all that obvious so perhaps you should
> add it to the FAQ because I think a lot of people are puzzled when the
> cross correlation does not produce the expected "spike" at the
> expected location.
>
> I agree with you that the documentation should be improved and that
> this type of correlation should be made an option.
> ImageAnalyst
- - - - - - - - - - - -
  In searching for the "spike" you speak of, ImageAnalyst, you shouldn't include the part of C that has been influenced by the padding with zeros off the edges. In the case of your I1 and I2, the "valid" (as Mathworks designated them) correlations are in the central portion:

C(3:5,3:5) =

    0.8375 0.7508 0.8278
    1.0000 0.7799 0.8392
    0.7963 0.8996 0.8924

These correspond to the nine possible ways of fitting your 3 x 3 I2 array entirely inside the 5 x 5 I1 array. As you see, in this region the match at C(4,3) dominates everything else.

  The 1's at the corners of the full C matrix are inevitable, since a single pair of numbers is being compared for correlation and since they are indeed "proportional" to each other, the correlation is 1 (or -1.) That is, you get a*b/sqrt(a^2*b^2) which always gives +1 or -1. This is an indication of how those border areas are not of as much significance as those unaffected by zero padding. The farther out they are, the worse they get.

  It's a little hard to explain the theory of that formula I gave you, but I'll try. If you recall, without any normalization you get the sum of products of two series of numbers, a1, a2, ..., an and b1, b2, ..., bn as:

 a1*b1 + a2*b2 + ... + an*bn

Of course these numbers change at every step of the correlation computation. To normalize this sum of products you must divide it by

 sqrt( (a1^2 + a2^2 + ... + an^2) * (b1^2 + b2^2 + ... + bn^2) )

and these quantities will also be changing at every step. Notice that each of the factors of sums inside the square root can itself be looked upon as another unnormalized correlation:

 (a1^2)*1 + (a2^2)*1 + ... + (an^2)*1

That is, we need the unnormalized correlation between a1^2, a2^1, ,,, an^2 and 1, 1, ..., 1. The same applies to the b's.

  So the strategy is to run three 'xcorr2' correlations in parallel, so to speak. After obtaining them, all we do is take the element-by-element products, quotients, and square roots to get the normalized correlations we desire. It's unsightly but it works.

  It can surely be simplified. In a case like yours when one array is smaller in both sizes than the other and when we chop off the correlation portion influenced by zero padding, then one of these above sums of products inside the square root remains constant, so actually the number of 'xcorr2' calls then needed could be pared down to only two with careful programming.

Roger Stafford

Subject: divide 1280-by-960 image into 9-by-9

From: Roger Stafford

Date: 28 May, 2011 19:52:04

Message: 22 of 27

"areeba khan" wrote in message <irm4h1$jke$1@newscl01ah.mathworks.com>...
> i am working on a project detection of skin cancer using image processing..i have found a reference matrix of 9-by-9 of tumourous cell now through looping i want 2 compare this reference matrix with the image... ............
- - - - - - - - - - - -
  To ImageAnalyst and Areeba: I just ran across a matlab function which computes a normalized cross correlation of two-dimensional arrays. It is called 'normxcorr2' and is to be found in the Image Processing Toolbox. The documentation can be read at:

 http://www.mathworks.com/help/toolbox/images/ref/normxcorr2.html

  This normalization differs from the one I have described in this thread in that it also subtracts mean values in each frame before computing the correlation. However its values are similarly bounded by -1 and +1. You might like to experiment with this function, Areeba.

  I have one more observation to make about your problem, Areeba. All these various kinds of correlation measurements are in my opinion very crude methods of making pattern comparisons.

  You are apparently trying to do automatic pattern recognition of small cancer spots in nine by nine frames of grey level pixels. In my opinion the only proper way to achieve such an ambitious goal as this is to develop very complex comparison logic, probably by means of neural network learning procedures which can be implemented in the neural network toolbox.

  However doing this requires far more than a single, simple template to compare images with. It needs an entire extended learning period with a large and representative sample of how nine by nine cancer spots would look under a variety of conditions, locations, orientations, and contrast levels. Most particularly the learning process must be exposed to the full variety that exists among different kinds of cancer patterns themselves.

Roger Stafford

Subject: divide 1280-by-960 image into 9-by-9

From: ImageAnalyst

Date: 28 May, 2011 22:44:50

Message: 23 of 27

On May 28, 3:52 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
>   To ImageAnalyst and Areeba: I just ran across a matlab function which computes a normalized cross correlation of two-dimensional arrays.  It is called 'normxcorr2' and is to be found in the Image Processing Toolbox.  The documentation can be read at:
>
>  http://www.mathworks.com/help/toolbox/images/ref/normxcorr2.html

> Roger Stafford

-------------------------------------------------------------
Yes, you're right. Good find! That will be handy to know. I never
noticed it because it's not a "See Also" under xcorr2, possibly
because they're in different toolboxes (Signal vs. Image). But when
you type "correlation" into the help, the normxcorr2 function doesn't
show up until the 9th one down (R2011a), and it's not even mentioned
at all when you go to the top entry on correlation.

There is actually quite a nice demo using it in there: "Registering an
Image Using Normalized Cross-Correlation" which does what I think
areeba wants to do in early explorations. However I very much agree
with Roger that this method ultimately will not be very good at
detecting cancerous locations. It may work for a few locations where
the cancer pretty much exactly looks like the template, but won't be
robust enough in general to be a useful algorithm in the real-world
practical sense. However, if it's just for limited demo purposes,
like a student homework project, then it may fit the bill as long as
you list potential disadvantages and limitations of it in the
conclusions/summary.

Subject: divide 1280-by-960 image into 9-by-9

From: areeba khan

Date: 29 May, 2011 12:09:05

Message: 24 of 27

"Jeff " <jea@gene.dot.com>
> > > > > grayimg = rgb2gray(imagein);
> > > > >
> > > > > padimg = padarray(grayimg, [3 4], 0, 'post'); %modify these values to make your image dimensions divisible by 9
> > > > >
> > > > > height = size(padimg,1); %height of full image
> > > > > width = size(padimg,2); %width of full image
> > > > >
> > > > > n_width = 9;
> > > > > m_height = 9;
> > > > >
> > > > > for x = 1:m_height:height
> > > > > for y=1:n_width:width
> > > > > image = bigmont(y:y+m_height-1, x:x+n_width-1);
> > > > > imwrite(image,strcat(num2str(x),'^',num2str(y),'.jpg'),'jpg', 'Quality',80);
> > > > > end
> > > > >
> > > > > end

> The "Index exceeds matrix dimensions" is probably because you need to change the "3" and "4" in:
> padimg = padarray(grayimg, [3 4], 0, 'post');
> to give you an image with dimensions evenly divisible by 9.
>
> For example, you could make your image 963x1267 by using:
> padimg = padarray(grayimg, [3 7], 0, 'post');
>
> As far as "reducing the image"...the variable IMAGE is a 9x9 fragment of PADIMAGE that moves across the image, in a typewriter fashion, through PADIMAGE as the X and Y loops increment. Instead of calling IMWRITE, you could assign it to individual cells in a cell array: (http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-98.html#br1zng6-1). Does this make sense?

Jeff,
i had done the same way as you are refering to i.e. i had changed 4 with 7 in:
padimg line...and got the error i told you (index exceed matrix dimension)...
and thank you..now i got about the variable image point..

do i need to reduce my image size? if yes then how?

Subject: divide 1280-by-960 image into 9-by-9

From: ImageAnalyst

Date: 29 May, 2011 13:32:05

Message: 25 of 27

On May 29, 8:09 am, "areeba khan" <arybak...@hotmail.com> wrote:
> do i need to reduce my image size? if yes then how?
---------------------------------------------------------------------
No. In fact, Roger and I don't know why you're continuing down this
path of processing sub-images in steps of 9 when both he and I
recommended against it. Can you explain why that would be better than
sliding over one pixel at a time? What happens if the region you're
looking for is half way on one 9x9 chunk and half way on its neighbor?

Subject: divide 1280-by-960 image into 9-by-9

From: Jeff

Date: 30 May, 2011 15:56:04

Message: 26 of 27

"areeba khan" wrote in message <irtd11$267$1@newscl01ah.mathworks.com>...
> "Jeff " <jea@gene.dot.com>
> > > > > > grayimg = rgb2gray(imagein);
> > > > > >
> > > > > > padimg = padarray(grayimg, [3 4], 0, 'post'); %modify these values to make your image dimensions divisible by 9
> > > > > >
> > > > > > height = size(padimg,1); %height of full image
> > > > > > width = size(padimg,2); %width of full image
> > > > > >
> > > > > > n_width = 9;
> > > > > > m_height = 9;
> > > > > >
> > > > > > for x = 1:m_height:height
> > > > > > for y=1:n_width:width
> > > > > > image = bigmont(y:y+m_height-1, x:x+n_width-1);
> > > > > > imwrite(image,strcat(num2str(x),'^',num2str(y),'.jpg'),'jpg', 'Quality',80);
> > > > > > end
> > > > > >
> > > > > > end
>
> > The "Index exceeds matrix dimensions" is probably because you need to change the "3" and "4" in:
> > padimg = padarray(grayimg, [3 4], 0, 'post');
> > to give you an image with dimensions evenly divisible by 9.
> >
> > For example, you could make your image 963x1267 by using:
> > padimg = padarray(grayimg, [3 7], 0, 'post');
> >
> > As far as "reducing the image"...the variable IMAGE is a 9x9 fragment of PADIMAGE that moves across the image, in a typewriter fashion, through PADIMAGE as the X and Y loops increment. Instead of calling IMWRITE, you could assign it to individual cells in a cell array: (http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-98.html#br1zng6-1). Does this make sense?
>
> Jeff,
> i had done the same way as you are refering to i.e. i had changed 4 with 7 in:
> padimg line...and got the error i told you (index exceed matrix dimension)...
> and thank you..now i got about the variable image point..
>
> do i need to reduce my image size? if yes then how?

Then you'll need to do some troubleshooting. Make sure the image PADARRAY is being changed to the correct dimensions. If it is, then check at what value x and y are when you get the error.

OR....

You could change your approach so that you don't have to worry about getting 9x9 image fragments, and will probably give you a more robust result. The approach outlined by ImageAnalyst and Roger (again, outlined here: http://www.mathworks.com/products/demos/image/cross_correlation/imreg.html ) may be a bit more complex, conceptually, but would be well worth the time to try.

Subject: divide 1280-by-960 image into 9-by-9

From: areeba khan

Date: 30 May, 2011 19:57:04

Message: 27 of 27

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <8669ccdd-00e3-41e9-91f8-038617fdf66d@g12g2000yqd.googlegroups.com>...
> On May 29, 8:09 am, "areeba khan" <arybak...@hotmail.com> wrote:
> > do i need to reduce my image size? if yes then how?
> ---------------------------------------------------------------------
> No. In fact, Roger and I don't know why you're continuing down this
> path of processing sub-images in steps of 9 when both he and I
> recommended against it. Can you explain why that would be better than
> sliding over one pixel at a time? What happens if the region you're
> looking for is half way on one 9x9 chunk and half way on its neighbor?

Image Analyst,
i had already done this.. though this approach is working but it was very much time consuming (perhaps the size of image is large)...that is why i am a bit hesitant towards it..i don't know but somewhere i have in my mind (i may be wrong though) that the 9x9 method could be a bit fast...
I know your and Roger's point of loosing the clues is really valid.. i can continue with this approach..as it is working but i also need to think of some other alternative as to make it a bit fast...

Regards,
Areeba

Tags for this Thread

No tags are associated with 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