# MATLAB: Attempted to access img_edge(513,118); index out of bounds because size(img_e​dge)=[512,​512].

index out of bounds because size

I get this message when i try to run my code. I am new in the program.I need starting point of contour I don't know what to do my code is following :
I = imread('1.jpg'); f = imadjust(I,[0.23 0.61],[]); BW = im2bw(f, graythresh(f)); h=fspecial('gaussian',16,100); BW=imfilter(BW,h); BW=imrotate(BW,-180); im = BW(50:end-40,50:end-40); im1 = bwareaopen(BW,100000); im2 = bwareaopen(im1,100000); lines = bwperim(im2,8); [B,f,N,A] = bwboundaries(im2,'hole'); [labeled,numObjects] = bwlabel(BW,4);
figure ,imshow(im2); %Show binary image
im2= imresize(im2, [512, 512]); img_edge=edge(im2);
figure,imshow(img_edge); % Show edge of image
[a b]=find(img_edge); counter=size(find(img_edge),1); point_x(1)=a(1); point_y(1)=b(1);% find out the starting point neighbor_offset=[1 1 0 -1 -1 -1 0 1;0 1 1 1 0 -1 -1 -1]; new_neighbor=[8 8 2 2 4 4 6 6]; neighbor=1;
    for i=1:counter %try to find out all those pixels on the contour      while img_edge(point_x(i)+neighbor_offset(1,neighbor),point_y(i)+neighbor_offset(2,neighbor))==0            neighbor=mod(neighbor,8)+1;        end        point_x(i+1)=point_x(i)+neighbor_offset(1,neighbor);        point_y(i+1)=point_y(i)+neighbor_offset(2,neighbor);        neighbor=new_neighbor(neighbor);         if and(point_x(1)==point_x(i+1),point_y(1)==point_y(i+1))            break        end    end
[rows, columns, numberOfColorChannels] = size(I);% Don't let row or column go outside the image edges.thisRow = max([rows, point_x(i)+neighbor_offset(1,neighbor)]);thisCol = max([columns, point_y(i)+neighbor_offset(2,neighbor)]);while img_edge(thisRow, thisCol)==0