my work is feature extraction for character recognition.i have to split 100*100 binary image into diagonal style left and right directions of 20 regions... suggest me pls..

 Accepted Answer

A 100 x 100 matrix has (2*100-1) = 199 diagonals. 199 is prime. Therefore you will not be able to group the diagonals into any number of bands with the same number of diagonals for each band (except, of course, taking one band at a time or taking all 199 bands at a time.)
An N x N matrix has (2*N-1) diagonals. That is always going to be an odd number. Therefore you will never be able to group the diagonals into any even number of bands with the same number of diagonals in each band.
To be able to group into 19 bands of equal width, you would have to pad your 100 x 100 matrix out to 105 x 105, at which point you could construct 19 bands of 11 diagonals per band.
YourImage = rand(105,105) > 0.5;
N = size(YourImage,1);
numbands = 19;
bandwidth = (2*N - 1) / numbands;
if bandwidth ~= floor(bandwidth)
error(sprintf('Matrix size of %d x %d cannot be divided into %d equal-width bands', N, N, numbands));
end
bandoffset = N + bandwidth;
B = fliplr(YourImage);
diags = cell(numbands,1);
for J = 1 : numbands
T = cell(bandwidth,1);
for K = 1:bandwidth
T{K} = diag(B, J*bandwidth + K - bandoffset);
end
diags{J} = vertcat(T{:});
end
To get the number of white pixels per group,
diagsum = cellfun(@sum, diags);
For left-leaning bands, use B = YourImage; without the fliplr();

4 Comments

hi.. how to plot the output of the above program as shown in the image?
This code draws the diagonals such that everything on and to the left of the line is included in the section. This code ends up drawing a diagonal at the bottom right, through the last diagonal, because that is indeed the last diagonal in the grouping.
The last diagonal is a little more visible than one would expect if one is displaying the image "larger than life", because when images are drawn, the pixels are positioned so that their centers are at nice integer locations.
Figuring out exactly where to draw the diagonals to indicate which pixels are included is tricky.
imagesc(YourImage);
axis image
p = bandwidth:bandwidth:2*N-1;
q = 0 * p;
q(p>N) = p(p>N) - N;
p = p - q;
for K = 1 : length(p)
line([q(K),p(K)],[p(K),q(K)],'color','w')
end
Thanks a lot Walter Roberson. I have got the above output image. But these lines split the pixels. Whereas in the first shown image, whole pixels exists even after splitting into diagonal bands. As I have to count the no. of non-zero pixels in each band, I need whole pixels after splitting as shown in the the 1st image.. help me..
The drawn lines are presentation purpose only, and have nothing to do with the counting. The code I posted above with bandoffset and so on, that produces the cell array diags{} which has the content of the diagonal bands already split up. If the values are 0 and 1 and you want to count the 1's then you can use
cellfun(@sum, diags)

Sign in to comment.

More Answers (1)

You need to define the scan pattern to use. Diagonals in matrices most naturally divide into an odd number, not an even number. For example in an 8 x 8 matrix, the matrix divides naturally into diagonals of length 1+2+3+4+5+6+7+8+7+6+5+4+3+2+1 = 64, which is 15 diagonals that would traditionally be numbered -7 to +7
In the example diagram you show, look at the bottom row. The left square is white. If we take that square as column 1, and let the columns be evenly spaced apart by distance D, then because the right column is also white, we arrive at the equation 1 + 10*D = 100 for some D. Clearly D cannot be integral.

7 Comments

thanks a lot sir.. can u please give the code for splitting 100*100 binary image into 19 diagonal parts..
What form do you want these 20 bands (10 for each slant direction I presume) in? Each band will have a different number of pixels in it. Do you want each band to be a cell array containing a 1-D list of all the pixel values in that band?
Here are 20 diagonals that cover the image, but the second half are not symmetric length with the first. I am investigating
B = fliplr(YourImage);
diags = cell(20,1);
for J = 1 : 20
T = cell(10,1);
for K = 1:10
T{K} = diag(B, (J-11)*10+K);
end
diags{J} = vertcat(T{:});
end
@Image Analyst: the 20 bands should be as in the above image.each band will have different number of pixels.Initially image should be split into left aligned diagonal bands, no. of white pixels in each band should be counted and stored. similarly image should be split into right aligned diagonal bands,no. of white pixels should be counted and stored.
@Walter Roberson: Thanks for your code. But my binary image is converted to grayscale image by this. Can you help me..
The image you showed at the very top is not binary - there are several gray levels there. Which variable is your binary image? Which line converts it into gray levels other than 0 and 1 (or 0 and 255)?
My testing shows that the output is a cell array of whatever class YourImage is.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!