Logical error regarding mouse input

1 view (last 30 days)
Ria3242
Ria3242 on 5 Oct 2016
Commented: Ria3242 on 2 Nov 2016
Hello. I have an image of different sub images from which I crop different sub images at different times during processing. My image consists of 9 animals as 9 sub images. I want to crop them one by one by randomly getting mouse input and then crop the sub image which is clicked. I compare the values of x and y and then crop using imcrop function. the problem i'm facing is, when I want to crop sub image 7, the command line compares the input values and displays sub image 4. and so on with 5 instead of 8 and 6 instead of 9. Just these three sub images are not displayed or over-written.
My code is:
% x and y values for sub image 4
if(x>1 && x<175)
if(y>116 && y<235)
%code for displaying sub image 4
%and so on with 5 and 6
% x and y values for sub image 7
if(x>1 && x<175)
if(y>225 && y<340)
when I run these statements separately in the command line, the results are correct. but when working in my GUI, the results are as described above. During debugging, when I click on sub image 7, the exact values I get for x and y are x=135.2441 , y= 264.1670
I can't get why still the control gets in the if structure of sub image 4 having these values, as they take us for image 7, and then displays image 4 instead of 7. Please help me correct my calculations. Thanks !
  2 Comments
Adam
Adam on 5 Oct 2016
Edited: Adam on 5 Oct 2016
I assume it is just a typo that your y ranges overlap between 225 and 235?
Your code will land in the first if statement though not the second. It depends what, if anything, else happens within the first if statement, but not the second. Either way, if there are no else statements then it should still fall into the nested if statements at the bottom even if it does something else first.
Logically there is obviously something going wrong in code that you have not shown us.
Ria3242
Ria3242 on 9 Oct 2016
yes Sir I checked again and again before I read your comment. It was my mistake of entering incorrect values of the maximum range of y. in the case of sub images 4 and 7, the maximum value of y was same, this was my mistake. Thank you so much for responding.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 9 Oct 2016
As Adam says, there must be something wrong some logic that you haven't shown us.
In any case, seeing so many if makes me cringe. Assuming the subimages touch each other, a much better way (and less error prone) of finding which image the user has clicked would be:
startx = [1, 175, 350]; %took a guess at the third column
starty = [1, 116, 235];
subimagenumber = sub2ind([3 3], find(y >= starty, 1), find(x >= startx, 1));
That's it, much simpler, no chance of error in one of the if.
If the subimages do not touch, or they're not all the same size, it gets a bit more complicated, but the same idea can be applied.
  1 Comment
Ria3242
Ria3242 on 2 Nov 2016
yes Sir, I got the error myself, it was the mistake of entering the value of y and the coordinates of image 4 were clashing with the coordinates of image 7. Thank you so much for responding.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!