Ciprian
Ciprian

Reputation: 27

Matlab freehand ROI pixel selection

I am trying to use imfreehand(...) to replace a selected region in one image with the corresponding region in another image.

This is my code so far:

% Sample images:
I1=imread('office_1.jpg');
I2=imread('office_5.jpg');

imshow(I1)
h = imfreehand;
wait(h);
pixels = getPosition(h);

x = pixels(:,1);
y = pixels(:,2);

for i = 1:numel(x)
   I1(y(i), x(i), :) = I2(y(i), x(i), :);
end

imshow(I1)

However, I get the error that: "index must be a positive integer or logical." In this case, I am not sure why this error arises and how to correct it.

Any explanation would be greatly appreciated.

Upvotes: 0

Views: 256

Answers (1)

ibezito
ibezito

Reputation: 5822

Reason for the error

The error comes from the fact that getPosition function returns it's coordinates in double format. you need to cast it to int in order for the assignment to work.

x = int16(pixels(:,1));
y = int16(pixels(:,2));

Actual Solution

However, your code doesn't do exactly what you expect it to. The getPosition function returns a list of points along the boundary of the mask created in imfreehand. If you want to actually replace the inner part of it, you should extract the binary mask out of it, for example:

binaryImage = h.createMask();
[y,x] = find(binaryImage);

Results

enter image description here

Upvotes: 1

Related Questions