![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/223949/0000%20Screenshot.png)
How to get the positions and put a marker (rectangle or oval) on the image?
5 次查看(过去 30 天)
显示 更早的评论
How to get the positions of only white pixel values and put a marker (rectangle or oval) in the images attached. When searched I found this link https://in.mathworks.com/matlabcentral/answers/110230-drawing-a-rectangle-on-top-of-an-image. But the position I have to get automatically. Can you help me.?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/223787/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/223788/image.jpeg)
Reference images have attached. The seconf image I got using edge operator.
I = imread('im.jpg');
E1 = edge(I,'Sobel');
imshowpair(I,E1,'montage')
0 个评论
采纳的回答
Image Analyst
2019-6-11
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
%===============================================================================
% Read in gray scale demo image.
folder = pwd;
baseFileName = 'image.jpeg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the histogram of the image.
subplot(2, 3, 2);
imhist(grayImage);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Binarize the image
binaryImage = grayImage < 82;
% Extract only the two largest blobs. This will take the major ones and ignore small noise blobs.
binaryImage = bwareafilt(binaryImage, 2);
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Two main blobs', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Label the image so we can take the rightmost one.
labeledImage = bwlabel(binaryImage);
% Take the right most one. It will have the label 2.
binaryImage = labeledImage == 2;
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
% Display the image.
subplot(2, 3, 4);
imshow(binaryImage, []);
title('Rightmost Blob', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Use it to zero out the other parts of the image.
grayImage(~binaryImage) = 0;
% Display the image.
subplot(2, 3, 5);
imshow(grayImage, []);
title('Masked gray image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Get a new binary image of just the letters
lettersMask = grayImage > 86;
% Fill holes
lettersMask = imfill(lettersMask, 'holes');
% Take largest blob only.
lettersMask = bwareafilt(lettersMask, 1);
% Take convex hull
lettersMask = bwconvhull(lettersMask);
% Display the image.
subplot(2, 3, 6);
imshow(lettersMask, []);
title('Masked gray image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Measure blobs
props = regionprops(lettersMask, 'Centroid', 'BoundingBox');
xy = props.Centroid
% Put a red cross at the centroid.
hold on;
plot(xy(1), xy(2), 'r+', 'MarkerSize', 30, 'LineWidth', 2);
% Put up a bounding box.
rectangle('Position', props.BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2);
% Update title
caption = sprintf('Centroid at x (column) = %.1f, y (row) = %.1f', xy(1), xy(2));
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/223949/0000%20Screenshot.png)
更多回答(1 个)
Image Analyst
2019-6-11
Threshold, to find the background.
Call bwareafilt() to get the second biggest blob.
Mask everything else to zero, then threshold again to find the characters, and finally call regionprops() and ask for the Centroids. Attach your attempt at code if you can't get it done.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!