I am new in MATLAB, I am trying to implement flood filling using this algorithm in matlab, I dont know what I did wrong may be I didnt used recursive function right, but still I dont whats going wrong and this code make my matlab close I am using the following code I am trying to debug it since morning but failed to find the problem
function [ colored_Image ] = floodFill( image, target_Loc_x, target_Loc_y, targetColor, replacementColor )
colored_Image = image;
if (target_Loc_x >= 1) && (target_Loc_x <= size(image,1)) && (target_Loc_y >= 1) && (target_Loc_y <= size(image,2))
if image(target_Loc_x,target_Loc_y) == targetColor
colored_Image(target_Loc_x,target_Loc_y) = replacementColor;
colored_Image = floodFill(colored_Image,target_Loc_x ,target_Loc_y + 1, targetColor, replacementColor);
colored_Image = floodFill(colored_Image,target_Loc_x + 1,target_Loc_y, targetColor, replacementColor);
colored_Image = floodFill(colored_Image,target_Loc_x,target_Loc_y - 1, targetColor, replacementColor);
colored_Image = floodFill(colored_Image,target_Loc_x - 1,target_Loc_y, targetColor, replacementColor);
end
end
end
calling this function using
image = floodFill(im,1,1,0,127);
imshow(image);
im is my matrix image of 200 by 200 I want my black color(0) to grey color(127), any help will be thankful
You are probably hitting Matlab's recursion limit. My computer doesn't crash, but generates this error:
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer.
The way around this is to rewrite floodFill so it does not use recursion. There are some alternative algorithms on Wikipedia.
Also: aardvarkk's answer makes an important point about Matlab's column-major indexing. You can fix your function by swapping all x and y variables.