MATLAB: How to get the positions and put a marker (rectangle or oval) on the image

dectectorImage Processing Toolbox

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.?
Reference images have attached. The seconf image I got using edge operator.
I = imread('im.jpg');
E1 = edge(I,'Sobel');
imshowpair(I,E1,'montage')

Best Answer

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