Filter löschen
Filter löschen

Calculating the range of pixel differences between two images

7 Ansichten (letzte 30 Tage)
Hello all. As a beginner in writing a program in matlab related to imageprocessing, I have stuck in between and have no idea in moving forward. I wrote a program in matlab which reads two gray images and calculate the intensity difference between certain specified range of user input.
Now, I have to calculate the total sum of difference when the user gives multiple input ranges. That means the all the difference values must be stored and summed to get the final total difference. Additionally, I have used inputdlg to create the dialogue box where the user gives the input range. Please help me regarding this.
I have taken the x value from 1 to 15 which means the box appears 15 times for the input. When there are less input ranges to choose (may be 8 or 10), the program stops running if the cancel button is pressed in the input box. Please also suggest the necessary change to this in order to avoid the program to stop running in case of choosing the cancel button.
I have used the following code:
for x = 1:15
x = inputdlg('range:', 'pixel range', [1 255]);
data = str2num(x{:});
count1 = sum(im(:)>= min(str2num(x{:})) & im(:)<=max(str2num(x{:}));
count2 = sum(im2(:)>= min(str2num(x{:})) & im2(:)<=max(str2num(x{:}));
difference = abs(count1 - count2);
disp(difference)
end
Thanks in advance.

Akzeptierte Antwort

Jinsong Han
Jinsong Han am 8 Aug. 2017
Bearbeitet: Jinsong Han am 8 Aug. 2017
According to the documentation for inputdlg in R2017a, an empty cell array is returned when the cancel button is pressed. If you want your code to continue running past the for loop when the cancel button is pressed, you can check for this case by adding the following lines after you obtain the data from the input dialogue box:
if isempty(x)
break;
end
This will check for the empty cell array case, and will exit the for loop and continue with the rest of the program. You can also consider using a while loop with two exit conditions to avoid using a break statement, as those tend to make code harder to follow.
To sum all the differences calculated in each loop, you can keep track of a variable containing the current sum to this point, and continue adding the difference value to the variable with each iteration. If the values need to be stored and accessed later, an empty array can be created before the loop, and each iteration of the loop can append the difference value to the end of the array like so:
differences = [];
for x = 1:15
% do stuff
differences = [differences difference]
end

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by