0

I need to create an image with three levels as described in the following question: How do i create a rectangular mask at known angles?

Code:

%# Create a logical image of a circle with image size specified as follows:
imageSizeX = 401;
imageSizeY = 301;

[columngrids, rowgrids] = meshgrid(1:imageSizeX, 1:imageSizeY);

%# Next create a logical mask for the circle with specified radius and center
centerY = (imageSizeY/2) + 0.5;
centerX = (imageSizeX/2) + 0.5;
radius  = 50;

Img   = double( (rowgrids - centerY).^2 + (columngrids - centerX).^2 <= radius.^2 );


%# change image labels to numeric
for ii = 1:numel(Img)

    if Img(ii) == 0
        Img(ii) = 2;  %change label from 0 to 2
    end

end

%# plot image
RI = imref2d(size(Img),[0 size(Img, 2)],[0 size(Img, 1)]);
figure, imshow(Img, RI, [], 'InitialMagnification','fit'); 

%# create the desired angle
phi = 45;       
width = 350;        % Desired width in pixels
height = 300;       % Desired height of bar in pixels

y = centerY - round(radius*cos(phi*pi/180));    % Find the nearest column
y0 = max(1, y-height); % Find where to start the bar 

Img(y0:y, 1:width)=3;

figure, imshow(Img, RI, [], 'InitialMagnification','fit'); 

I have realized that rounding the part that says (radius*cos(phi*pi/180)) to find y could in most cases create an error in the desired angle. Hence, if I remove the ‘round function’, I get the actual y value at the exact point at which the desired angle is formed in the image. Nonetheless, I get the warning as stated above. However, if I go further to apply the line: Img(y0:y, 1:width)=3;, the code still works, but I notice that Matlab approximates the y value when creating the vector y0:y (I feel this is the point where I have an issue)

My question then is: is there a way I could get around this such that I create my desired angle accurately and still end up having the bar from y to y0? without having matlab approximate the y value when it is creating the vector y0:y?

Maybe if i convert to cartesian xy coordinates i could have a chance? Any ideas how to do this conversion? Many thanks for your help! 

Community
  • 1
  • 1
User1772
  • 91
  • 8
  • `Img` is an array. You can't index an array with a non-integer. If I have `A = [1 2 3 4]` and ask you for the `2.4th` value in `A`, would you be able to give it to me? No. – sco1 Mar 24 '17 at 01:51
  • Thanks @excaza. I understand your point, but i am just wondering(and hoping) if there could be a way to work around the problem. – User1772 Mar 24 '17 at 02:11

1 Answers1

0

A pixels is the atomic element of a digital image. Digital images are discrete functions. A camera has discrete pixels, your screen has discrete pixels...

While reading sub-pixel values is always possible through interpolation, you cannot write pixels in the same way as the output is discrete.

Just accept that drawing is only possible with integer pixel coordinates but keep in mind that this does not stop you from doing more precise calculations behind the scenes.

So to answer your question: You overcome the warning by using integer indices.

Piglet
  • 27,501
  • 3
  • 20
  • 43