Outline the shape in the image

21 次查看(过去 30 天)
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.
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'

采纳的回答

Image Analyst
Image Analyst 2020-6-1
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 个评论
Alex Perrakis
Alex Perrakis 2021-8-31
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
Image Analyst 2021-8-31
@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;

请先登录,再进行评论。

更多回答(1 个)

Codeshadow
Codeshadow 2020-6-1
编辑:Codeshadow 2020-6-1
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 个评论
Codeshadow
Codeshadow 2020-6-1
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
You can use the Key-Value pair argument ('ShowText', 'on') to mark the levels if you'd like.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by