Borrar filtros
Borrar filtros

Program for evaluating various intensity areas in an image and I get multiple errors.

3 visualizaciones (últimos 30 días)
It is a program for evaluating various intensity areas in the image. It loads a JPG image.
It calculates the histogram of the light intensity distribution in the image and displays it next to the image. A mouse click on the histogram causes a mask (say green) to cover the pixels in the image within +/- 5% of the intensity we clicked on.
If there are problems interpreting the position of the click (and only then), a horizontal slider below the histogram plus an "apply" button is acceptable. Find a solution to repeat the maneuver (click again with auto-clear, reset button, etc).
Here is my code:
% Load the image
img = imread('image.jpg');
% Display the image and get the handle to the image object
figure;
im = imshow(img);
% Calculate the histogram of the image
[h, x] = imhist(img);
% Display the histogram next to the image
subplot(1,2,2);
bar(x,h);
% Allow the user to select a histogram bin
disp('Click on a histogram bin to apply the mask.');
pixel_info = impixelinfo(im);
% Add a slider to allow the user to adjust the histogram bin
slider = uicontrol('Style', 'slider', 'Min', 1, 'Max', length(h), 'Value', 1, ...
'Position', [50 20 300 20], 'Callback', @slider_callback);
% Add a button to apply the mask
button = uicontrol('Style', 'pushbutton', 'String', 'Apply', ...
'Position', [400 20 100 20], 'Callback', @apply_callback);
% Add a button to reset the mask
reset_button = uicontrol('Style', 'pushbutton', 'String', 'Reset', ...
'Position', [550 20 100 20], 'Callback', @reset_callback);
% Define the callbacks for the slider and button
function slider_callback(source, eventdata)
% Get the value of the slider and update the pixel info display
bin = round(get(source, 'Value'));
set(pixel_info, 'String', sprintf('Intensity: %d', bin));
end
function apply_callback(source, eventdata)
% Get the value of the slider
bin = round(get(slider, 'Value'));
% Apply the mask
mask = img > 0.95*x(bin) & img < 1.05*x(bin);
masked_img = img;
masked_img(~mask) = 0;
% Display the masked image
subplot(1,2,1);
imshow(masked_img);
% Save the masked image as a JPEG file
imwrite(masked_img, 'output.jpg', 'jpg');
% Clear the mask
masked_img = img;
subplot(1,2,1);
imshow(masked_img);
end
function reset_callback(source, eventdata)
% Reset the mask to the original image
masked_img = img;
subplot(1,2,1);
imshow(masked_img);
end

Respuestas (1)

Saffan
Saffan el 27 de Jun. de 2023
Hi Alexandra,
The issue in the code is that the variables being used in callback functions are not within the function's scope, causing them to be unrecognized. There are multiple ways to resolve this, some of them are:
• Pass all the required variables as arguments.
• Use nested functions to access the variables from the enclosing function.
• Use global variables to make the variables accessible within the callback functions.
Here is an updated version of the code that incorporates nested functions, eliminating the need to pass multiple variables to the callback functions:
function evaluateIntensity(img_path)
% Load the image
img = imread(img_path);
% Create the figure to display the image and histogram
figure;
% Display the image and get the handle to the image object
subplot(1,2,1);
im = imshow(img);
% Calculate the histogram of the image
[h, x] = imhist(img);
% Display the histogram next to the image
histogram_subplot = subplot(1,2,2);
bar(x,h);
% Allow the user to select a histogram bin
disp('Click on a histogram bin to apply the mask.');
% Add a slider to allow the user to adjust the histogram bin
slider = uicontrol('Style', 'slider', 'Min', 1, 'Max', length(h), 'Value', 1, ...
'Position', [50 20 300 20], 'Callback', @slider_callback);
% Add a button to apply the mask
button = uicontrol('Style', 'pushbutton', 'String', 'Apply', ...
'Position', [400 20 100 20], 'Callback', @apply_callback);
% Add a button to reset the mask
reset_button = uicontrol('Style', 'pushbutton', 'String', 'Reset', ...
'Position', [550 20 100 20], 'Callback', @reset_callback);
% Define the callbacks for the slider and buttons
function slider_callback(source, ~)
% Get the value of the slider and update the pixel info display
bin = round(get(source, 'Value'));
disp(['Intensity: ' num2str(bin)]);
end
function apply_callback(~, ~)
% Get the value of the slider
bin = round(get(slider, 'Value'));
% Apply the mask
mask = img > 0.95*x(bin) & img < 1.05*x(bin);
masked_img = img;
masked_img(~mask) = 0;
% Display the masked image
subplot(1,2,1);
imshow(masked_img);
% Save the masked image as a JPEG file
imwrite(masked_img, 'output.jpg', 'jpg');
end
function reset_callback(~, ~)
% Reset the mask to the original image
masked_img = img;
subplot(1,2,1);
imshow(masked_img);
end
end
Refer to these links for more information:

Categorías

Más información sobre Histograms en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by