finding submatrix and index

4 views (last 30 days)
Mohammad Golam Kibria
Mohammad Golam Kibria on 20 May 2013
Hi I have the following matrix as two small sample matrix. I want to fill the matrix value zero with 1 between two diagonal 1.
I1=
0 0 0
0 0 1
0 0 0
1 0 0
I2=
1 0 0
0 0 0
0 0 1
0 0 0
I want to have my output as follows:
I1=
0 0 0
0 0 1
0 1 0
1 0 0
I2=
1 0 0
0 1 0
0 0 1
0 0 0
Thanks in advanced.

Answers (2)

Andrei Bobrov
Andrei Bobrov on 20 May 2013
Edited: Andrei Bobrov on 20 May 2013
I1out = imdilate(I1,flipud(eye(min(size(I1)))));
I2out = imdilate(I2,eye(min(size(I1))));
ADD after Mohammad Golam Kibria's comment
s = size(I1);
d = 1-size(I1):size(I1,2)-1;
x = spdiags(I1(end:-1:1,:),d);
x1 = cumsum(x)&cumsum(x(end:-1:1,:));
I1out = full(spdiags(flipud(x1),d,s(1),s(2)));
  2 Comments
Mohammad Golam Kibria
Mohammad Golam Kibria on 20 May 2013
Edited: Mohammad Golam Kibria on 20 May 2013
This does not work well. for example, if I1 =
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
then I1out = imdilate(I1,flipud(eye(min(size(I1))))); creates the following output:
-Inf 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
but I need the output as follows:
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
could u please help me any more.
Image Analyst
Image Analyst on 20 May 2013
You would have to tell it which pairs to connect. Once you specify that, you can use my answer.

Sign in to comment.


Image Analyst
Image Analyst on 20 May 2013
You need to use Bresenham's Line Algorithm: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm. If you have the Image Processing Toolbox, you can do that with imline(). Let me know if you want a demo of imline.
  2 Comments
Mohammad Golam Kibria
Mohammad Golam Kibria on 21 May 2013
yes, I need a demo. then it will be clear to me.
Image Analyst
Image Analyst on 21 May 2013
Here's the demo:
% Demo to write an ellipse and a line into the overlay of an image,
% and then to burn those overlays into the image.
%----- Initializing steps -----
% Clean up
clc;
clear all;
close all;
workspace; % Display the workspace panel.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image 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
% Display images to prepare for the demo.
monochromeImage = imread('pout.tif');
subplot(2, 4, 1);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 2);
imshow(monochromeImage);
title('Original Image with ellipse in overlay');
subplot(2, 4, 5);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 6);
imshow(monochromeImage);
title('Original Image with line in overlay');
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
%----- Burn ellipse into image -----
% Create elliptical mask, h, as an ROI object over the second image.
subplot(2, 4, 2);
hEllipse = imellipse(gca,[10 10 50 150]); % Second argument defines ellipse shape and position.
% Create a binary image ("mask") from the ROI object.
binaryImage = hEllipse.createMask();
% Display the ellipse mask.
subplot(2, 4, 3);
imshow(binaryImage);
title('Binary mask of the ellipse');
% Let's try to add some text. (Doesn't work)
% hText = text(50, 100, 'Line of Text');
% textMask = hText.createMask();
% binaryImage = binaryImage & textMask;
% imshow(binaryImage);
% Burn ellipse into image by setting it to 255 wherever the mask is true.
monochromeImage(binaryImage) = 255;
% Display the image with the "burned in" ellipse.
subplot(2, 4, 4);
imshow(monochromeImage);
title('New image with ellipse burned into image');
%----- Burn line into image -----
burnedImage = imread('pout.tif');
% Create line mask, h, as an ROI object over the second image in the bottom row.
subplot(2, 4, 6);
hLine = imline(gca,[10 100],[10 100]); % Second argument defines line endpoints.
% Create a binary image ("mask") from the ROI object.
binaryImage2 = hLine.createMask();
% Display the line mask.
subplot(2, 4, 7);
imshow(binaryImage2);
title('Binary mask of the line');
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage(binaryImage2) = 255;
% Display the image with the "burned in" line.
subplot(2, 4, 8);
imshow(burnedImage);
title('New image with line burned into image');

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!