views:

108

answers:

1

There is an image A of fixed size 256*256*3 (RGB). The mathematical formula for covariance between two adjacent pixels values x,y in an image of is popularly known to be

cov(x,y) =1/n summation _i =1 to N [ E(x_i-E(x))(y_i-E(y))]

r_xy= cov(x,y) / ( sqrt(D(x)* D(y) ))

where r_xy is the correlation coefficients between two horizontally,vertically and diagonally adjacent pixels of these two images. D(x) = 1/n summation _i=1 to n square[(x_i - E(x))]

E(x) = 1/n summation i=1 to n (x_i)

Q1 :- How to do the above computation in matlab

Q2 :- How to randomly select say 5000 pairs of two horizontally adjacent pixels from the images and then plot the distribution of these two horizontally adjacent pixels.

Please help.

+1  A: 

As is typical with image processing for truecolor RGB images, there are a couple of key issues to first address. I mentioned these in my answer to your other question involving a different image processing algorithm, but they bear repeating here:

  • Figuring out how to deal with the third dimension: An RGB image is actually a set of three 2-D matrices (one each for the red, green, and blue color components of the pixels) concatenated along a third dimension. When performing pixel-wise operations, you have to decide whether you are going to perform the operations three times (i.e. once for each color plane) or whether you are going to somehow collapse the values along the third dimension (i.e. converting to a grayscale intensity image with functions like RGB2GRAY) to give you a set of 2-D image data to operate on.
  • Be mindful of data types: Image data loaded into MATLAB is typically in the form of an unsigned 8-bit integer, but sometimes it can be an unsigned 16-bit integer or a double precision type. When dealing with integer types, conversion to double precision is usually desired before performing certain operations in order to avoid certain aspects of integer arithmetic such as round-off and saturation.

Alright, now that those formalities are out of the way, I see your problem above as comprising two steps. First, you have to select subsets of paired pixels from the image, such as all horizontally-paired pixels. Second, you have to apply the statistical formulae you have above. In the examples below, I'll assume the operations are being performed on the red (i.e. first) color plane of the matrix A:

  1. Selecting subsets of paired pixels: Let's start with the set of unique horizontal pairings of pixels. If I select the pixels in the first column of A and place them in the subset x, then the horizontally adjacent pixels will be those in the second column of A, and these will be placed in the subset y. I can also add the pixels in the second column to the subset x, and the horizontally adjacent pixels in the third column would then be placed in the subset y. Repeating this for all columns in A, we can see that the pixels in columns 1 through 255 will be in subset x, and the pixels in columns 2 through 256 will be in the subset y. The matrix indexing would therefore look like this:

    x = A(:,1:end-1,1);  %# All rows and columns 1 through 255 from red plane
    y = A(:,2:end,1);    %# All rows and columns 2 through 256 from red plane
    

    Following similar logic as above, you can construct the entire set of unique vertical pairings of pixels in this fashion:

    x = A(1:end-1,:,1);  %# Rows 1 through 255 and all columns from red plane
    y = A(2:end,:,1);    %# Rows 2 through 256 and all columns from red plane
    

    And likewise for the set of unique diagonal pairings of pixels, where "diagonal" runs from top left to bottom right in the matrix:

    x = A(1:end-1,1:end-1,1);  %# All but the last row and column
    y = A(2:end,2:end,1);      %# All but the first row and column
    

    Or for "anti-diagonals", where "diagonal" runs from bottom left to top right in the matrix:

    x = A(2:end,1:end-1,1);  %# All but the first row and last column
    y = A(1:end-1,2:end,1);  %# All but the last row and first column
    

    Now, you can choose any one of these sets of x and y data to perform the statistical calculations you want for the red color plane. You can repeat the above substituting 2 or 3 for the last index in each line to get the calculation for the green and blue color planes, respectively.

  2. Performing the statistical tests: This part is simple. There is already a built-in function CORRCOEF for computing the correlation coefficient in MATLAB. You may have to reshape the subsets of pixel values x and y into column vectors first using single-colon indexing:

    r_xy = corrcoef(x(:),y(:));
    

    Functions also exist for the other formulae as well: MEAN for E(x), VAR for D(x), and COV for cov(x,y).

In regard to your second question, you can first create x and y as I did above for all unique pairs of horizontally adjacent pixels, then create a vector with a random permutation of the integer indices into x and y using the function RANDPERM. Selecting the first 5000 entries of those randomly permuted indices will give you 5000 random indices into x and y:

randIndex = randperm(numel(x));  %# A random permutation of the integers
                                 %#   from 1 to numel(x)
randIndex = randIndex(1:5000);   %# Pick the first 5000 indices
xRand = x(randIndex);            %# 5000 random values from x
yRand = y(randIndex);            %# The corresponding 5000 values from y

This will give you your 5000 pairs of horizontally adjacent pixel values in x and y. However, it is unclear what you mean by "plot the distribution". I'm guessing you will either end up using the function HIST or perhaps the function SCATTER for this purpose.

gnovice
@sumona: I've updated my answer with a detailed explanation. I'm not exactly sure what you're asking about with the additional question in your comment, though.
gnovice
@sumona: If you want to visualize the correlation (if there is any) between the sets of `x` and `y` values, `scatter(x,y)` is what you want.
gnovice
@gnovice. What about numel(x) in randIndex = randperm(numel(x)); numel should take integers/ pixel positions (or values, which one?) randomly from the image itself. I have followed how to select 5000 random values from vector x and y. Doubts : 1. Would the correlation for these 5000 random values be r_xy = corrcoef(xRand(:),yRand(:)) instead? 2. For scatter graph plot, should it not be scatter(xRand,yRand) in order to plot 5000 values of x and y? Kindly correct me if i am wrong with what i understood.
sumona
@sumona: In your question you say you want to randomly select 5000 pairs of horizontally adjacent pixels. One option is to select 5000 image indices at random, then select the index to the left or right of these indices to complete the pair. The other option I give in my answer is to generate `x` and `y` such that they contain all unique horizontal pairings (as I describe further up), then select 5000 of those pairs at random. It's up to you which way you want to do it. Regarding points 1 and 2, yes, you will want to use `xRand` and `yRand` for your correlation and scatter plot.
gnovice
@gnovice,there is something wrong with the way diagonals are fetched. Actually, all the elements are fetched and there is no difference visually (imshow(A(:,1:end-1,1:3))) between diagonal pixels and pixels which are adjacent horizontally and vertically. What can be the problem?
sumona
@sumona: The sample code I have above will create sets `x` and `y` that contain *all* unique pairs of diagonally-adjacent pixel values in the image. Is this not what you want?
gnovice
ofcourse,this is what i want but on viewing the subsets by imshow, the fig turns out to be the same for all different subsets. If, pixels chosen are different then the image resulting out of them(image of adjacent pixels horizontally chosen,image of pixels vertically chosen and image of pixels diagonally chosen) be also different? Further, if you could kindly follow this link http://stackoverflow.com/questions/3701480/logical-error-in-matlab. In this, the answer replied by Amro gives an entire different way of fetching the diagonal elements. I am not contradicting anyone, but im confused.
sumona
@gnovice, i really apologize for all the trouble but your code works fine and the other version also works. I think there are several ways to work upon an approach but visually all the subsets appear to give the same figure. So how do i actually verify that the subsets contain correct pixels chosen in a particular orientation.
sumona
@sumona: When you view `x` and `y` with IMSHOW, they will look very much the same. This is because they are simply versions of the original image with one row or column removed from an edge. However, when calling CORRCOEF the elements of `x` and `y` will be reshaped into single columns, and the corresponding elements in `x` and `y` (i.e. the "pairings") will end up being completely different due to the removal of those individual rows or columns from the edges of the original image. ...
gnovice
...(continued) In contrast, the answer given by Amro on your other question does something completely different. It selects all pixels that lie along the *main diagonal* of the image matrix, which will be pixels `A(1,1,:), A(2,2,:), ... A(256,256,:)`.
gnovice
@gnovice. Thank you very much for all the help and tutorial. Now its cleared.
sumona