Translate pseudocode to MATLAB

1 view (last 30 days)
med-sweng
med-sweng on 23 Feb 2014
Commented: Walter Roberson on 19 Jan 2020
I'm going through some pseudocode, and having some issues with a part that is similar to the following:
among all pixels that belong to `s`
being 4-neighbours of `m`, but bot included in 'm'
select the pixel that minimizes some function (assume any function)
How can I translate the above psuedocode to `MATLAB`?
Thanks.
  2 Comments
Walter Roberson
Walter Roberson on 23 Feb 2014
Is "m" a region (set of pixels) or a single pixel ?
med-sweng
med-sweng on 23 Feb 2014
@Walter Roberson. "m" is a set of pixels

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 23 Feb 2014
This is done with nlfilter() of the Image Processing Toolbox. It scans the image and takes a small window of a size you choose and does whatever you want to it. You can take a 3by3 window and take the 4-connected pixels and do whatever function you want to the 5 four-connected pixels. It can also be done (with a bit more complication) using blockproc().
  3 Comments
Image Analyst
Image Analyst on 23 Feb 2014
Attached is an example of how to do a locally adaptive threshold with nlfilter. See how it's used and adapt your program to do it like that. If you can't figure it out, post your image, say what functions or operations you want to do on the 5 pixels and let me know. But try first . Don't just immediately say you can't do it.
Image Analyst
Image Analyst on 23 Feb 2014
Alright. Here you go. Here is the demo using 4 corrected pixels to do a local Otsu thresholding.
% Demo to threshold an image with a locally adaptirve Otsu threshold using nlfilter.
function nlfilter_demo()
clc;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in gray scale demo image.
folder = pwd; % Change this if the image is not in the same folder as this m-file.
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo to Threshold Document', 'NumberTitle', 'Off')
% Let's compute and display the histogram, just for fun.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Original Image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
%--------------------------------------------------------------------------
% Do a local Otsu of the gray level image using the nlfilter function.
% We'll tell nlfilter to use our LocalOtsu() function to do its operations.
fun = @(x) LocalOtsu(x);
doubleImage = im2double(grayImage); % Cast to double.
% Here comes the actual filtering.
localThresh = nlfilter(doubleImage, [3, 3], fun);
%--------------------------------------------------------------------------
% All done!
% Display the image.
subplot(2, 2, 3);
imshow(localThresh, []);
title('Local Otsu', 'FontSize', fontSize);
% Function to take the Otsu threshold of the small patch of gray levels passed in by nlfilter().
function oneThresholdedPixel = LocalOtsu(grayImagePatch)
oneThresholdedPixel = false;
try
[rows, columns] = size(grayImagePatch); % Just FYI
fivePixels = grayImagePatch([2,4,5,6,8]); % 4 connected pixels
% Compute a thershold level.
level = graythresh(grayImagePatch);
% Threshold the center pixel only, the third element
oneThresholdedPixel = im2bw(fivePixels(3), level);
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from LocalOtsu()

Sign in to comment.


Karla Soto
Karla Soto on 19 Jan 2020
1. Start
2. Input the Augmented Coefficients Matrix (A):
For i = 1 to n
For j = 1 to n+1
Read Ai,j
Next j
Next i
3. Apply Gauss Elimination on Matrix A:
For i = 1 to n-1
If Ai,i = 0
Print "Mathematical Error!"
Stop
End If
For j = i+1 to n
Ratio = Aj,i/Ai,i
For k = 1 to n+1
Aj,k = Aj,k - Ratio * Ai,k
Next k
Next j
Next i
4. Obtaining Solution by Back Substitution:
Xn = An,n+1/An,n
For i = n-1 to 1 (Step: -1)
Xi = Ai,n+1
For j = i+1 to n
Xi = Xi - Ai,j * Xj
Next j
Xi = Xi/Ai,i
Next i
5. Display Solution:
For i = 1 to n
Print Xi
Next i
6. Stop
  1 Comment
Walter Roberson
Walter Roberson on 19 Jan 2020
? This does not appear to match the original question?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!