How to binarize an image using spinner in app designer?

3 views (last 30 days)

I'm writing a multiwindow app to segment image based on the rgb profile.
App1, App2 and an example of the image used is attached.
App1 is composed of two push buttons: "Load image" and "Threshold". When the "Load image" button is pressed, it executes some code to load image from a folder. When the "Threshold" button is pressed, it opens a new window, called app2, that contains multiple parts:
5 UIAxes ==> Original image, Mask image, Red channel, Green channel and Blue channel
3 push buttons ==> Import original image and histogram, close App2 and Apply RGB values
6 spinners used to adjust low and high red, green and blue levels used to binarize the image

When the "import original image and histogram" is pushed, it loads the image from App1 and displays it in "Original image" and "Mask image" UIAxes. It also load and display in "red channel", "green channel" and "blue channel" the histogram distribution of red, green and blue layers.
Then, and it is where I have the problem, when i select a specific min or max value with the spinners and hit the "Apply RGB values" button, I got the following error message in the command window:

Error using >=
Arrays have incompatible sizes for this operation.

Error in app2/ApplyRGBvaluesButtonPushed (line 114)
spinnerBW = (I(:,:,1) >= app.minRedValue) & (I(:,:,1) <= app.maxRedValue) ...

Error in matlab.apps.AppBase>@(source,event)executeCallback(appdesigner.internal.service.AppManagementService.instance(),app,callback,requiresEventData,event) (line 63)
newCallback = @(source, event)executeCallback(appdesigner.internal.service.AppManagementService.instance(), ...

Related documentation

Error while evaluating Button PrivateButtonPushedFcn.

I have already this bit of code in a script that works like a charm.
Any help on solving this error would be greatly appreciated.


  1 Comment
GMabilleau on 29 Jul 2022
after adding a debug button to assign to the workspace the different variable, I managed to find the problem: as I did not modifiy the spinner value as compared to its initial value and used the "ValueChangedFcn" callback, the spinner.Value is empty.
this brings me another question, How can I get the spinner value in the event that I do not change it from initial value?

Sign in to comment.

Accepted Answer

Kevin Holly
Kevin Holly on 29 Jul 2022
You do not need to define the spinner values like you did here:
app.minRedValue = app.MinRed.Value;
You can just access the values of the spinners directly throughout your other callback functions. If you want to access the min red value from the spinner even if you don't change it, you can just access it with the following:
So in your calculation, I would write:
spinnerBW = (I(:,:,1) >= app.MinRed.Value) & (I(:,:,1) <= app.MaxRed.Value) ...
& (I(:,:,1) >= app.MinGreen.Value) & (I(:,:,1) <= app.MaxGreen.Value) ...
& (I(:,:,1) >= app.MinBlue.Value) & (I(:,:,1) <= app.MaxBlue.Value);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by