Outline the shape in the image
21 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hassan Strong
le 31 Mai 2020
Commenté : Image Analyst
le 31 Août 2021
Hi,
I need to outline a shape with circle in the image. Here is the image which i created using colormap.
The condition is if the 0.6<pixel<1 , then outline it with a circle. where pixel is the pixel value which is ranged from 0.1 to 1.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307235/image.png)
Here is the image info
Filename: XXXXX
FileModDate: '31-May-2020 23:43:42'
FileSize: 1588399
Format: 'tif'
FormatVersion: []
Width: 840
Height: 630
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: [8 8 8]
Compression: 'Uncompressed'
PhotometricInterpretation: 'RGB'
StripOffsets: [1×70 double]
SamplesPerPixel: 3
RowsPerStrip: 9
StripByteCounts: [1×70 double]
XResolution: 96
YResolution: 96
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: [255 255 255]
MinSampleValue: [0 0 0]
Thresholding: 1
Offset: 1587608
ImageDescription: 'MATLAB Handle Graphics'
0 commentaires
Réponse acceptée
Image Analyst
le 1 Juin 2020
You can use bwboundaries() and plot():
binaryImage = 0.6 < grayImage & grayImage < 1;
boundaries = bwboundaries(binaryImage);
hold on;
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
x = thisBoundary(:, 2); % Column
y = thisBoundary(:, 1); % Row
plot(x, y, 'r-', 'LineWidth', 2);
end
Attach your array or image file if you need more help.
5 commentaires
Alex Perrakis
le 31 Août 2021
Hey, is there a way to plot those lines in normal plot with x-y-coordinates? i have your solution and i have found very good success thanks
Image Analyst
le 31 Août 2021
@Alex Perrakis, yes, simply bring up a new axes - one that does not have an image it in already - and call the same code
binaryImage = 0.6 < grayImage & grayImage < 1;
boundaries = bwboundaries(binaryImage);
figure; % Create a new figure.
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
x = thisBoundary(:, 2); % Column
y = thisBoundary(:, 1); % Row
plot(x, y, 'r-', 'LineWidth', 2);
end
grid on;
Plus de réponses (1)
Codeshadow
le 1 Juin 2020
Modifié(e) : Codeshadow
le 1 Juin 2020
You could try using contourf and specify the contour levels as an input argument.
For example:
% Create an example mesh
x = linspace(-1, 1, 100);
y = x;
[X, Y] = meshgrid(x, y);
% Compute the radii
R = sqrt(X.^2 + Y.^2);
% Generate contour map, with levels in increments of 0.1 or at a fixed value
% level = [0:0.1:1];
level = [0.6 0.6];
contourf(X, Y, R, level)
colorbar();
xlabel('X')
ylabel('Y');
Replace R used above with your data.
Note: You might not be placing a circle around your data, but will instead be tracing the isolines at the levels you desire.
Hope this helps!
3 commentaires
Codeshadow
le 1 Juin 2020
Apologies, I'd made a typo in my original answer, and have fixed it now.
When specifying a single level in the contourf function, you will need to pass it in as a vector of two identical values, such as [0.6 0.6] in the corrected code above. If you want to plot several levels, you can specify all levels in a vector (see commented code).
For the example of the circle, if you specify a level as [0.6 0.6], you will get something like
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307946/image.jpeg)
You can use the Key-Value pair argument ('ShowText', 'on') to mark the levels if you'd like.
Voir également
Catégories
En savoir plus sur Contour Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!