How to add a point on an image?

13 ビュー (過去 30 日間)
ALEXANDROS
ALEXANDROS 2024 年 8 月 27 日
コメント済み: Ronit 2024 年 8 月 28 日
Hello,
So i have the following code:
clear all
clc
% Creating Tabs and properties
fig = uifigure("Name", "BISC");
fig.Resize = 'off'; % Disable resizing
fig.WindowState = 'normal'; % Ensure the window is in normal state (not maximized or minimized)
set(fig, 'Units', 'normalized', 'Position', [0.1, 0.1, 0.8, 0.8]);
tg = uitabgroup(fig, 'Units', 'normalized', "Position", [0, 0, 1, 1]);
t1 = uitab(tg, "Title", "To be added");
t1.Scrollable = "on";
% Adding a button to load the floor plan image
b_loadImage = uibutton(t1, 'Position', [650 550 140 22], 'Text', 'Load Floor Plan');
ax = uiaxes(t1, 'Position', [30 130 600 450]); % Axes to display the floor plan
% Adding a button to detect walls
b_detectWalls = uibutton(t1, 'Position', [650 525 140 22], 'Text', 'Detect Walls');
b_detectWalls.Enable = 'off'; % Disable until an image is loaded
% Adding a button to add points
b_addPoint = uibutton(t1, 'Position', [650 500 140 22], 'Text', 'Add Point');
b_addPoint.Enable = 'off'; % Disable until an image is loaded
% Callback to load image
b_loadImage.ButtonPushedFcn = @(btn, event) loadImage(ax, b_detectWalls, b_addPoint);
% Callback to detect walls
b_detectWalls.ButtonPushedFcn = @(btn, event) detectWalls(ax);
% Callback to add points
b_addPoint.ButtonPushedFcn = @(btn, event) enablePointAdding(ax);
% Functions
% Function to load an image
function loadImage(ax, b_detectWalls, b_addPoint)
% Let user select an image file
[file, path] = uigetfile({'*.jpg;*.jpeg;*.png;*.bmp;*.tif', 'All Image Files'}, 'Select Floor Plan');
if isequal(file, 0)
return;
end
% Display the image on the axes
img = imread(fullfile(path, file));
imshow(img, 'Parent', ax);
% Enable the detect walls and add point buttons
b_detectWalls.Enable = 'on';
b_addPoint.Enable = 'on';
end
% Function to detect walls in the image
function detectWalls(ax)
% Retrieve the image from the axes
imgHandle = findobj(ax, 'Type', 'image');
if isempty(imgHandle)
return;
end
img = imgHandle.CData; % Extract image data
% Convert to grayscale if it's a color image
if size(img, 3) == 3
grayImg = rgb2gray(img);
else
grayImg = img;
end
% Threshold the image to create a binary image
bw = imbinarize(grayImg, 'adaptive', 'ForegroundPolarity', 'dark', 'Sensitivity', 0.4);
% Apply morphological operations
se = strel('rectangle', [5, 5]);
bw = imclose(bw, se); % Close small gaps
bw = imopen(bw, se); % Remove small noise
% Display the processed binary image
imshow(bw, 'Parent', ax);
end
% Function to enable adding points on the image
function enablePointAdding(ax)
% Reset any previous button-down functions
ax.ButtonDownFcn = [];
% Set the ButtonDownFcn of the axes to allow point addition
ax.ButtonDownFcn = @(src, event) addPoint(src, event);
end
% Function to add a point on the image
function addPoint(src, event)
% Get the current point coordinates
cp = event.IntersectionPoint(1:2);
% Plot the point on the axes
hold(src, 'on'); % Keep existing image
plot(src, cp(1), cp(2), 'ro', 'MarkerSize', 10, 'LineWidth', 2);
hold(src, 'off');
% Display the coordinates in the command window (or use for further processing)
disp(['Point added at: X=', num2str(cp(1)), ', Y=', num2str(cp(2))]);
end
But it seems that the point button does not work. Does anyone have a solution to this, i have been trying for quite a while now and can not manage adding a point on my floor plan.
Thank you in advance.

回答 (1 件)

Ronit
Ronit 2024 年 8 月 27 日
編集済み: Ronit 2024 年 8 月 27 日
The issue you're experiencing with the point addition feature in MATLAB GUI might be due to how the ButtonDownFcn is being set. The ButtonDownFcn for an axes object can sometimes be overridden by child objects like images. To ensure that clicking on the image triggers the ButtonDownFcn, you need to set the HitTest property of the image to 'off' and ensure the PickableParts property of the axes is set to 'all'.
Modify the loadImage function as follows:
% Set HitTest of the image to 'off' to ensure clicks are registered by the axes
imgHandle = findobj(ax, 'Type', 'image');
set(imgHandle, 'HitTest', 'off');
Additionally, ensure your axes are set to be pickable:
ax.PickableParts = 'all';
Please go through the documentation of Capturing Mouse Clicks for better understanding: https://www.mathworks.com/help/matlab/creating_plots/capturing-mouse-clicks.html
I hope this works for you!
  2 件のコメント
ALEXANDROS
ALEXANDROS 2024 年 8 月 27 日
Hello @Ronit
Thank you very much for your help!
I followed what you said but as i wanted to be able to add the point after the wall detection, i added
% Set HitTest of the image to 'off' to ensure clicks are registered by the axes
imgHandle = findobj(ax, 'Type', 'image');
set(imgHandle, 'HitTest', 'off');
ax.PickableParts = 'all';
inside the detectWalls(ax) function.
Ronit
Ronit 2024 年 8 月 28 日
Yes that will help in selecting points after the wall detection.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by