MATLAB Answers

matrix show , grain boundaries

9 views (last 30 days)
HG on 4 May 2021
Commented: HG on 14 Jun 2021
how can i show a matrix like this photo with black line boundaries ?
DGM on 4 May 2021
If you're going to ask the same question three times, why do you provide even less information now?
Now I'm reluctant to even answer it if it's just going to get deleted. If all you have is an indexed image, apply edge finding techniques, combine the images. You can keep it indexed or convert it to RGB. Look up fspecial(), imfilter(), edge(), ind2rgb(), colormap(), imshow().

Sign in to comment.

Accepted Answer

DGM on 5 May 2021
Edited: DGM on 6 May 2021
Your prior questions suggest that you already have an image to start with. A crude way to add lines to it is to just use edge finding methods. In this example, I'm just using an RGB image. You can do the same with an indexed image, but if you want it to display neatly with distinct colors, you'll have to figure out a colormap that does so (e.g. colorcube or a custom map).
inpict = imread('vdiag.png'); % this is rgb, uint8
emap = any(edgemap(inpict),3); % find edges
emap(:,[1:3 end-2:end]) = 1; % add border around image
emap([1:3 end-2:end],:) = 1;
% thin the edges and the dilate so it has uniform width
emap = imdilate(bwmorph(emap,'thin','Inf'),ones(3));
outpict = inpict.*uint8(~emap); % make edge regions zero
imshow(outpict) % show it
The function edgemap() is from MIMT:
If you don't want to use that, you can use any other method for edgefinding by gradient approximation. That can be done with fspecial+imfilter and some math, or you can use edge().
emap = zeros(size(inpict));
for c = 1:size(inpict,3)
emap(:,:,c) = edge(inpict(:,:,c),'sobel');
emap = any(emap,3);
Of course, that still requires IPT, where MIMT does not. If IPT is truly an issue, MIMT also has alternatives for imdilate() and bwmorph().
DGM on 11 Jun 2021
If there are to be lines drawn between regions, the size of small images must change, otherwise there's nowhere to put the lines. Consider the image [1 1; 2 2; 3 3]. You'd need 4 horizontal lines and 2 vertical lines. That's more pixels than there even are in the image. Even for slightly larger images, the minimal line width would still cover large portions of the regions and distort their apparent geometry.
The output matrix is RGB. That's why it's 3D. You could use a different colormap using ind2rgb() just the same:
% just incorporate the lines into the indexed image
inpict(emap) = max(inpict(:))+1;
inpict = inpict-1; % uint8 indexed images start at 0
% build a custom colormap from hot()
% adjust n as needed for other images
n = 100;
cmap = hot(double(max(inpict(:)))*n+1);
cmap = cmap(1:n:end,:)
outpict = ind2rgb(inpict,cmap);
or you could just leave inpict as an indexed image and have fun dealing with carrying around colormaps if you wanted.

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 4 May 2021
Edited: Adam Danz on 5 May 2021
H Ghari > how can i show a matrix like this photo with black line boundaries ?
This should get you started:
rng default;
n = 100;
x = rand([1 n]);
y = rand([1 n]);
h = voronoi(x,y);
DT = delaunayTriangulation(x(:),y(:));
[V,R] = voronoiDiagram(DT);
verts = cell(size(R));
for i = 1:numel(R)
verts{i} = V(R{i}([1:end,1]),:);
hold on
axlims = [xlim; ylim]; % [xlim; ylim]
patchColors = hsv(numel(verts)+1);
for j = 1:numel(verts)
patch(verts{j}(:,1), verts{j}(:,2), patchColors(j,:))
set(gca,'color', patchColors(end,:)) % <-- kind of cheating
axis equal
Also see these two blog posts by Mike Garrity.
And the vononi2mask or the voronoizone functions on the file exchange (not tested).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!