Search code examples
imagematlabbitmapcropmask

Trying to cut an image via Matlab


Dear friends I'm trying binarise the image, it’ll look best if I cut the black parts out. To avoid any funny edge effects I’ll crop the pattern to a circle, and so our ‘mask’ is drawn below, along with the relevant snippet of Matlab

enter image description here

P = imread('Pattern.png');
P = 1-im2bw(P); % Keep black parts
Nmin = min(size(P));
% Crop into square, then circle
P = P(1:Nmin, 1:Nmin);
[xg, yg] = meshgrid(1:Nmin, 1:Nmin);
P((xg - Nmin/2).^2 + (yg - Nmin/2).^2 > 0.99*0.25*Nmin^2) = 0;
% Create a small border
P = padarray(P, [1 1], 0);

But for some reason I'm having issues with line 7,

Error: File: Try1.m Line: 7 Column: 42
Unbalanced or unexpected parenthesis or bracket.

please help me...

edit:

here is explanation

enter image description here


Solution

  • I assume that you have copied this code from a website. The reason is the following line:

    P((xg - Nmin/2).^2 + (yg - Nmin/2).^2 > 0.99*0.25*Nmin^2) = 0;
    

    Notice the >? (which is what @beaker mentioned in their comment) If you check the original code on the website where you found it, there is probably a > instead of > right?

    The reason is that in HTML, the > sign is displayed by writing > in the HTML source code. When the website is shown in your browser, it will convert > to the > sign, but apparently when copy-pasting the code, it apparently copied the HTML source code and not the rendered sign.

    MATLAB of course can't handle HTML source code, so you have to replace > by > and your code works fine:

    P((xg - Nmin/2).^2 + (yg - Nmin/2).^2 > 0.99*0.25*Nmin^2) = 0;
    

    resulting image