1

I want to use a character in for loop but I can't because I'm using ginput function and it's possible to first and end pixel in for loop changed by every mouse click I cant use an exact pixel it's my error when run my code.

Subscript indices must either be real positive integers or logicals.

A=imread('peppers.png');
C=imshow(A);
F=rgb2gray(A);
figure,imshow(F)
B=ginput(4)
a=min(B);
b=max(B);
a1=a(1)
a2=a(2)
b1=b(1)-a(1)
b2=b(2)-a(2)
for i=a1:a1+b1
    for j=a2:a2+b2
       F(j,i)=0;
    end
end
figure,imshow(F)
1
  • I recommend to read the MATLAB documentation about Indexing. It can save you a lot of work. F(a1:a1+b1,a2:a2+b2)=0 fully replaces the two loops and is faster. Commented Feb 15, 2016 at 11:46

1 Answer 1

1

The values you get from input are slightly off the full integer. Use B-round(B) and you see the error.

To fix it, use round

B=round(ginput(4))

The error message was really curious

Attempted to access F(142,162); index must be a positive integer or logical.

Sign up to request clarification or add additional context in comments.

1 Comment

@StewieGriffin: The comment you deleted was right, you can still break it simply clicking the area outside the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.