You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to plot a subset of triangles after DelaunayTri
1 view (last 30 days)
Show older comments
I have computed a Delaunay triangulation using DelaunayTri(). Then I extracted big triangles based on a threshold, how can I reconstruct these triangles and plot it in a figure ?
thanks
Accepted Answer
Simon
on 9 Dec 2013
Hi!
If you have a triangulation you can of course plot specific triangles (if you know them) with
TRI = dt.Triangulation;
X = dt.X;
ind = [1,2,3,4,5];
triplot(TRI(ind, :), X(:, 1), X(:, 2));
5 Comments
Amani
on 11 Dec 2013
what about if I have the IDs of Bounding Vertices, how can I plot the triangles ? in other words, how can I get the ind of the IDs of bounding vertices ?
Simon
on 11 Dec 2013
Just find all triangles in TRI with the specified vertex IDs:
[ind, ~] = ind2sub(size(TRI), find(TRI==ID))
Simon
on 12 Dec 2013
I used the code:
x = rand(20,1);
y = rand(20,1);
dt = DelaunayTri(x,y);
figure(1)
triplot(dt);
ID = 1;
TRI = dt.Triangulation;
X = dt.X;
[ind, ~] = ind2sub(size(TRI), find(TRI==ID));
figure(2)
triplot(TRI(ind, :), X(:, 1), X(:, 2));
Works without problems.
More Answers (1)
Amani
on 9 Dec 2013
how can I calculate the distance between two connected vertices (only Y coordinate) TRI = dt.Trangulation for q=1:size(dt,1) for u=1:size(dt,1) j = isConnected(k,q,u); if (j == true) % retrive q and u (y coordinate) and calculate the distance between them end
end
thanks
14 Comments
Simon
on 11 Dec 2013
The above code can be used as well:
% find connected triangles to ID
[ind, ~] = ind2sub(size(TRI), find(TRI==ID));
% connected vertices
vert = setdiff(unique(TRI(ind,:)), 1)
% y-distance
X(ID, 2) - X(vert, 2)
Amani
on 16 Dec 2013
thank you very much
what about if the y-distance < Threshold and I need to find only the connected vertices (not triangles) with ydistance below the threshold ...how can I retrieve them ?
thanks
Amani
on 16 Dec 2013
I'm trying to do the reverse of this code 1- find vertices (edge) with distance less than a threshold 2- find their triangles
Simon
on 16 Dec 2013
Almost the same:
- compute the y-distance of all vertices to the given one
- finding all distances below threshold gives you vertex IDs
- find those vertex IDs in triangulation
Amani
on 17 Dec 2013
thank you very much actually what I'm trying to do is to find the text lines in a handwritten documents so after creating triangles using triangulation I want to divide the text into title or subtitles and paragraphs based on tracing the edges (or triangles) horizontally to identify those regions you can find the generated triangles based on the centroid of connected components here https://lh6.googleusercontent.com/-Y-N-05Y5hx4/Uq_xp9w98hI/AAAAAAAAAAc/2GNXwURh5pY/w560-h420-no/Triangles.jpg what do you thing the best way to do so? really appreciate you help
Simon
on 17 Dec 2013
Sorry, but I don't understand what you're doing. Where do the triangles come from? What is the source for triangulation? How does the "original text" look like?
Amani
on 18 Dec 2013
the source of triangulation is the centers of connected component of the handwritten text, and I want to divide the text into regions: title, subtitle and paragraphs you can find the original documents here https://lh6.googleusercontent.com/-RjnIpj7Qn9Y/UrE0o_8B24I/AAAAAAAAABw/4boaEyJ1FMY/w426-h554/ARA_D01_W0001.tif
thank you
Simon
on 18 Dec 2013
Hi!
Now I see what you are doing ;-)
What is the reason that you are using triangulations for this? I did something similar a while ago (not for text, but other images). I found the script and adapted it for your image. Try it and see if it is useful for you.
% file with handwritten text
filename = 'ARA_D01_W0001.jpg';
% read in
A = imread(filename);
% and show
figure(1); cla;
image(A)
% make combined color value
Amode = double(A(:,:,1)) * 2^16 + double(A(:,:,2)) * 2^8 + double(A(:,:,3));
% color most often used -> background
BackColor = mode(Amode(:));
% find RGBs of background
ind = find(Amode == BackColor, 1);
[x,y] = ind2sub(size(Amode), ind);
% background RGB
BackRGB = squeeze(A(x, y, :));
% threshold for background detection
BackThreshold = 20;
% image mask, everything that is background is set to true
ImageMask = ...
(A(:, :, 1) > (BackRGB(1) - BackThreshold)) & ...
(A(:, :, 1) < (BackRGB(1) + BackThreshold)) & ...
(A(:, :, 2) > (BackRGB(2) - BackThreshold)) & ...
(A(:, :, 2) < (BackRGB(2) + BackThreshold)) & ...
(A(:, :, 3) > (BackRGB(3) - BackThreshold)) & ...
(A(:, :, 3) < (BackRGB(3) + BackThreshold));
% threshold of line/paragraph detection (number of background pixel in y-direction)
BackYThreshold = 1;
% image mask for background lines in image
LineMask = false(size(ImageMask));
for y = (1+BackYThreshold):(size(A, 1)-BackYThreshold)
for x = 1:size(A, 2)
%R check y-range
if ImageMask(y, x)
if all(ImageMask((y-BackYThreshold):(y+BackYThreshold), x))
LineMask(y, x) = true;
end
end
end
end
% find lines with only background
LineMask = all(LineMask, 2);
figure(2); cla;
image(LineMask*255);
Amani
on 18 Dec 2013
thank you gain and again :) actually I'm modifying someone else code and he is using triangulation, he got a very good result with long text line but not with short ones I got this error when I run your code
Index exceeds matrix dimensions.
Error in simon (line 9)
Amode = double(A(:,:,1)) * 2^16 + double(A(:,:,2)) * 2^8 + double(A(:,:,3));
Simon
on 18 Dec 2013
Then you have a grayscale image, that only has 1 as
size(A, 3)
in contrast to a RGB image with 3. So check "size(A, 3)" and do
if size(A, 3) == 1
Amode = double(A);
elseif size(A, 3) == 3
Amode = double(A(:,:,1)) * 2^16 + double(A(:,:,2)) * 2^8 + double(A(:,:,3));
endif
Amani
on 18 Dec 2013
ok , then should I change the ImageMask ?
ImageMask = ...
(A(:, :, 1) > (BackRGB(1) - BackThreshold)) & ...
(A(:, :, 1) < (BackRGB(1) + BackThreshold)) & ...
(A(:, :, 2) > (BackRGB(2) - BackThreshold)) & ...
(A(:, :, 2) < (BackRGB(2) + BackThreshold)) & ...
(A(:, :, 3) > (BackRGB(3) - BackThreshold)) & ...
(A(:, :, 3) < (BackRGB(3) + BackThreshold));
Amani
on 19 Dec 2013
when I modified the code to
if size(A, 3) == 1
Amode = double(A);
elseif size(A, 3) == 3
Amode = double(A(:,:,1)) * 2^16 + double(A(:,:,2)) * 2^8 + double(A(:,:,3));
end
and run it I got this error
Index exceeds matrix dimensions.
Error in simon (line 84) ImageMask = ...
I converted the grayscale image into RGB using this code
[m n]=size(A);
rgb=zeros(m,n,3);
rgb(:,:,1)=A;
rgb(:,:,2)=rgb(:,:,1);
rgb(:,:,3)=rgb(:,:,1);
A=rgb/255;
it works without error but image (A) was all zeros and (ImageMask) was all ones
Simon
on 19 Dec 2013
I used gimp for conversion. But I think you should not divide by 255. Images in matlab are uint8 with values between 0 and 255, in contrast to color values for e.g plotting with values between 0 and 1.
Amani
on 23 Dec 2013
I remove \255 but still did not work .. is there any other way for conversion to rgb?
thanks
See Also
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)