Steve Eddins retired from MathWorks in 2024 after 30 years of service. He can now be found at MATLAB Central and at Matrix Values, where he continues to write about MATLAB and related topics. His MathWorks career included image processing, toolbox development, MATLAB development and design, development team management, and MATLAB design standards. He wrote the Steve on Image Processing blog for 18 years and is a co-author of Digital Image Processing Using MATLAB.
An amateur musician and French horn enthusiast, Steve is a member of Concord Orchestra and Melrose Symphony Orchestra, as well as a member of the board of directors for Cormont Music and the Kendall Betts Horn Camp. He blogs about music and French horn at Horn Journey.
Several people have asked me recently how to plot some kind of shape on top of an image, so I thought I'd show the basic technique
here. Essentially, you display the image, then call hold on, and then use plot or some other graphics command to superimpose the desired shape.
For my first example, I'll superimpose the boundaries found by the bwboundaries function on top of the original binary image.
bw = imread('circles.png');
b = bwboundaries(bw);
imshow(bw)
Now call hold on. This causes subsequent plotting commands to add to what's already in the figure, instead of replacing it.
hold on
Finally, call plot to superimpose the boundary locations.
for k = 1:numel(b)
plot(b{k}(:,2), b{k}(:,1), 'r', 'Linewidth', 3)
end
It's good to get in the habit of calling hold off when you're done adding plot elements. That way, the next high-level plotting command will start over, which is usually
what is expected.
My second example shows how to superimpose a 25-pixel-by-25-pixel grid on an image. (See Natasha's blog comment.) Display the image first, and then call hold on.
clf
rgb = imread('peppers.png');
imshow(rgb)
hold on
Now superimpose the grid. To make sure the grid is visible over all pixel colors, I'll use the trick of superimposing two
line objects with contrasting colors and line styles. The Pixel Region Tool in the Image Processing Toolbox uses this same trick.
M = size(rgb,1);
N = size(rgb,2);
for k = 1:25:M
x = [1 N];
y = [k k];
plot(x,y,'Color','w','LineStyle','-');
plot(x,y,'Color','k','LineStyle',':');
endfor k = 1:25:N
x = [k k];
y = [1 M];
plot(x,y,'Color','w','LineStyle','-');
plot(x,y,'Color','k','LineStyle',':');
end
hold off
コメント
コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。