Wednesday, May 8, 2024
HomeMatlabAn Instance of Operate Argument Validation for an Picture Utility Operate »...

An Instance of Operate Argument Validation for an Picture Utility Operate » Steve on Picture Processing with MATLAB


Whereas I used to be engaged on a prototype yesterday, I discovered myself writing a small little bit of code that turned out to be so helpful, I questioned why I had by no means carried out it earlier than. I might like to point out it to you. It will be a chance to discover some current advances in MATLAB that make helpful operate syntaxes straightforward to put in writing.

I wrote a easy operate referred to as imageSizeInXY. This operate takes a MATLAB Graphics Picture object and returns its complete width and top within the spatial x-y items of the axes.

operate out = imageSizeInXY(im)

 

 

finish

(I will present the total implementation under.) I used to be utilizing the operate like this.

A = imread(“peppers.png”);

im = imshow(A, XData = [-1 1], YData = [-1 1]);

axis on

However this operate solely works if I’ve captured the picture object in a variable. Often, I name imshow with no output argument, and so I haven’t got the picture object:

imshow(A, XData = [-1 1], YData = [-1 1])

I assumed to myself, it will positive be good if I may simply name imageSizeInXY with no enter arguments, and it will simply robotically inform me in regards to the picture that is displayed within the present determine. After fascinated about this briefly, I wrote the next:

operate out = imageSizeInXY(im)

arguments

im (1,1) matlab.graphics.primitive.Picture = findimage

finish

 

 

finish

 

operate im = findimage

image_objects = imhandles(imgca);

if isempty(image_objects)

error(“No picture object discovered.”);

finish

im = image_objects(1);

finish

The primary attention-grabbing piece right here is using the arguments block, a MATLAB language syntax that was launched in R2019b. The arguments block, and extra typically the operate argument validation options, helps to implement operate conduct with useful error messages and helpful defaults whereas writing little or no code.

Let’s look extra carefully initially of the operate.

operate out = imageSizeInXY(im)

arguments

im (1,1) matlab.graphics.primitive.Picture = findimage

finish

The arguments block lets us specify constraints and default conduct for the operate’s enter arguments. The “(1,1)” half says that the enter argument, im, should have measurement $1 occasions 1$. In different phrases, it have to be a scalar. The subsequent half, “matlab.graphics.primitive.Picture,” signifies that im is required to be that class (or convertible to it).

MATLAB will implement these measurement and sort constraints robotically, issuing particular error messages if they aren’t met.

image-size-in-xy-errors.png

Word that you could get the total class title of MATLAB graphics objects by calling class:

class(im)

The “= findimage” portion is used to robotically present a default worth for the enter im. This could be a fixed worth or an expression. On this case, findimage is an expression that calls the native operate that I’ve written:

operate im = findimage

image_objects = imhandles(imgca);

if isempty(image_objects)

error(“No picture object discovered.”);

finish

im = image_objects(1);

finish

The capabilities imgca and imhandles are Picture Processing Toolbox utilities. The operate imgca returns essentially the most lately present axes object containing a number of photos, and imhandles returns the picture objects inside it.

The previous few traces are simply me being cautious to deal with the circumstances the place there aren’t any picture objects to be discovered, or the place there are a number of picture objects within the axes.

The total implementation, together with the straightforward measurement calculation, is under.

operate out = imageSizeInXY(im)

arguments

im (1,1) matlab.graphics.primitive.Picture = findimage

finish

 

pixel_width = extent(im.XData,measurement(im.CData,2));

pixel_height = extent(im.YData,measurement(im.CData,1));

 

x1 = im.XData(1) – (pixel_width / 2);

x2 = im.XData(finish) + (pixel_width / 2);

 

y1 = im.YData(1) – (pixel_height / 2);

y2 = im.YData(finish) + (pixel_height / 2);

 

out = [(x2 – x1) (y2 – y1)];

finish

 

operate im = findimage

image_objects = imhandles(imgca);

if isempty(image_objects)

error(“No picture object discovered.”);

finish

im = image_objects(1);

finish

 

operate e = extent(information,num_pixels)

if (num_pixels <= 1)

e = 1;

else

e = (information(2) – information(1)) / (num_pixels – 1);

finish

finish

Now I haven’t got to recollect to name imshow with an output argument in an effort to use it.

If you have not already explored utilizing operate argument validation and arguments blocks for writing your individual MATLAB capabilities, I encourage you to present them a attempt. It is actually value it!



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments