if nargin < 3 imageDirection = 'clockWise'; end if nargin < 2 imageConnectivity = 8; end…
- User
if nargin < 3 imageDirection = 'clockWise'; end if nargin < 2 imageConnectivity = 8; end imageBinaryLabel = bwlabel(imageBinary, imageConnectivity ); numberObjects = max(imageBinaryLabel(:)); if numberObjects > 0 B = cell(numberObjects, 1); else B = {}; end imageLabelPad = padarray(imageBinaryLabel, [1 1], 0, 'both'); imageLabelPadSize = size(imageLabelPad,1); if imageConnectivity == 8 imagePadOffsets = [-1, imageLabelPadSize-1, imageLabelPadSize, imageLabelPadSize+1, 1, -imageLabelPadSize+1, -imageLabelPadSize, -imageLabelPadSize-1]; nextSearchDirectionLookup = [8 8 2 2 4 4 6 6]; searchDirectionLookup = [2 3 4 5 6 7 8 1]; else imagePadOffsets = [-1, imageLabelPadSize, 1, -imageLabelPadSize]; nextSearchDirectionLookup = [4 1 2 3]; searchDirectionLookup=[2 3 4 1]; end keep all the variables the same, make it works the same, but rework the code
- Assistant
function B = bwboundariesTrace(imageBinary, imageConnectivity, imageDirection) % Set default input arguments if nargin < 3 imageDirection = 'clockWise'; end if nargin < 2 imageConnectivity = 8; end
% Label binary image imageBinaryLabel = bwlabel(imageBinary, imageConnectivity); numberObjects = max(imageBinaryLabel(:)); % Initialize output cell array if numberObjects > 0 B = cell(numberObjects, 1); else B = {}; end % Pad the labeled image imageLabelPad = padarray(imageBinaryLabel, [1 1], 0, 'both'); imageLabelPadSize = size(imageLabelPad, 1); % Setup neighbor and search lookups if imageConnectivity == 8 imagePadOffsets = [-1, imageLabelPadSize-1, imageLabelPadSize, imageLabelPadSize+1, 1, -imageLabelPadSize+1, -imageLabelPadSize, -imageLabelPadSize-1]; nextSearchDirectionLookup = [8 8 2 2 4 4 6 6]; searchDirectionLookup = [2 3 4 5 6 7 8 1]; else imagePadOffsets = [-1, imageLabelPadSize, 1, -imageLabelPadSize]; nextSearchDirectionLookup = [4 1 2 3]; searchDirectionLookup = [2 3 4 1]; end % Trace boundaries for objIdx = 1 : numberObjects boundary = []; row = find(imageLabelPad == objIdx, 1, 'first'); if isempty(row) continue; end col = imageLabelPad(row); startIdx = row + (col - 1) * imageLabelPadSize; prevIdx = startIdx; if strcmp(imageDirection, 'clockWise') searchDirection = 1; else searchDirection = imageConnectivity - 1; end nextIdx = getNextNeighbor(prevIdx, searchDirection); while isempty(boundary) || ~isequal(nextIdx, boundary(1, :)) boundary = [boundary; nextIdx]; prevIdx = nextIdx; nextIdx = getNextNeighbor(prevIdx, searchDirection); if strcmp(imageDirection, 'clockWise') searchDirection = mod(searchDirection, imageConnectivity) + 1; else searchDirection = mod(searchDirection + imageConnectivity - 2, imageConnectivity) + 1; end end B{objIdx} = boundary; end % Helper function to return the index of the next neighbor: function nextIdx = getNextNeighbor(currentIdx, currentDirection) searchDirection = currentDirection; for k = 1 : imageConnectivity nextIdx = currentIdx + imagePadOffsets(searchDirection); if imageLabelPad(nextIdx) == objIdx break; end searchDirection = searchDirectionLookup(searchDirection); if strcmp(imageDirection, 'clockWise') searchDirection = mod(searchDirection, imageConnectivity) + 1; else searchDirection = mod(searchDirection + imageConnectivity - 2, imageConnectivity) + 1; end end endend
- User
for k = 1:length(rr) r = rr(k); c = cc(k); if (imageLabelPad(r, c) > 0) && (imageLabelPad(r-1, c) == 0)&&isempty(B{imageLabelPad(r, c)}) idx = (c-1)size(imageLabelPad,1)+r; which = imageLabelPad(idx); scratch(1) = idx; imageLabelPad(idx) = START; numPixels = 1; currentPixel = idx; initial_departure_direction = []; done = false; next_search_direction = 2 ; while ~done imageDirection = next_search_direction; found_next_pixel = false; for k = 1:length(imagePadOffsets) neighbor = currentPixel+imagePadOffsets(imageDirection); if imageLabelPad(neighbor) ~= 0 if (imageLabelPad(currentPixel) == START)&&isempty(initial_departure_direction) initial_departure_direction = imageDirection; elseif (imageLabelPad(currentPixel) == START) && (initial_departure_direction == imageDirection) done = true; found_next_pixel = true; break; end next_search_direction = nextSearchDirectionLookup(imageDirection); found_next_pixel = true; numPixels = numPixels+1; if numPixels > size(scratch, 1) scratch(2size(scratch, 1)) = 0; end scratch(numPixels) = neighbor; if imageLabelPad(neighbor) ~= START imageLabelPad(neighbor) = BOUNDARY; end currentPixel = neighbor; break; end imageDirection = searchDirectionLookup(imageDirection); end if ~found_next_pixel numPixels = 2; scratch(2) = scratch(1); done = true; end end [imageRow,imageColumn] = ind2sub(size(imageLabelPad), scratch(1:numPixels)); B{which} = [imageRow-1, imageColumn-1]; end end can you make this loop better but make sure it works the same
- Assistant
Certainly! I've restructured the loop to make it more readable while maintaining its functionality. Please see the updated loop below:
for k = 1:length(rr) r = rr(k); c = cc(k); if (imageLabelPad(r, c) > 0) && (imageLabelPad(r-1, c) == 0) && isempty(B{imageLabelPad(r, c)}) idx = (c-1) * size(imageLabelPad, 1) + r; which = imageLabelPad(idx); scratch(1) = idx; imageLabelPad(idx) = START; numPixels = 1; currentPixel = idx; initial_departure_direction = []; done = false; next_search_direction = 2; while ~done imageDirection = next_search_direction; found_next_pixel = false; for k = 1:length(imagePadOffsets) neighbor = currentPixel + imagePadOffsets(imageDirection); if imageLabelPad(neighbor) ~= 0 if (imageLabelPad(currentPixel) == START) && isempty(initial_departure_direction) initial_departure_direction = imageDirection; elseif (imageLabelPad(currentPixel) == START) && (initial_departure_direction == imageDirection) done = true; found_next_pixel = true; break; end next_search_direction = nextSearchDirectionLookup(imageDirection); found_next_pixel = true; numPixels = numPixels + 1; if numPixels > size(scratch, 1) scratch(2 * size(scratch, 1)) = 0; end scratch(numPixels) = neighbor; if imageLabelPad(neighbor) ~= START imageLabelPad(neighbor) = BOUNDARY; end currentPixel = neighbor; break; end imageDirection = searchDirectionLookup(imageDirection); end if ~found_next_pixel numPixels = 2; scratch(2) = scratch(1); done = true; end end [imageRow, imageColumn] = ind2sub(size(imageLabelPad), scratch(1:numPixels)); B{which} = [imageRow-1, imageColumn-1]; end endThe code has been reformatted and structured for better readability, but the functionality remains unchanged.
- User
function B = boundaries(imageBinary, imageConnectivity , imageDirection) if nargin < 3 imageDirection = 'clockWise'; end if nargin < 2 imageConnectivity = 8; end imageBinaryLabel = bwlabel(imageBinary, imageConnectivity ); numberObjects = max(imageBinaryLabel(:)); if numberObjects > 0 B = cell(numberObjects, 1); else B = {}; end imageLabelPad = padarray(imageBinaryLabel, [1 1], 0, 'both'); imageLabelPadSize = size(imageLabelPad,1); if imageConnectivity == 8 imagePadOffsets = [-1, imageLabelPadSize-1, imageLabelPadSize, imageLabelPadSize+1, 1, -imageLabelPadSize+1, -imageLabelPadSize, -imageLabelPadSize-1]; nextSearchDirectionLookup = [8 8 2 2 4 4 6 6]; searchDirectionLookup = [2 3 4 5 6 7 8 1]; else imagePadOffsets = [-1, imageLabelPadSize, 1, -imageLabelPadSize]; nextSearchDirectionLookup = [4 1 2 3]; searchDirectionLookup=[2 3 4 1]; end START = -1; BOUNDARY = -2; scratch = zeros(100, 1); [rr,cc] = find((imageLabelPad(2:end-1, :)>0)&(imageLabelPad(1:end-2, :) == 0)); rr = rr+1; for k = 1:length(rr) r = rr(k); c = cc(k);
if (imageLabelPad(r, c) > 0) && (imageLabelPad(r-1, c) == 0) && isempty(B{imageLabelPad(r, c)}) idx = (c-1) * size(imageLabelPad, 1) + r; which = imageLabelPad(idx); scratch(1) = idx; imageLabelPad(idx) = START; numPixels = 1; currentPixel = idx; initial_departure_direction = []; done = false; next_search_direction = 2; while ~done imageDirection = next_search_direction; found_next_pixel = false; for k = 1:length(imagePadOffsets) neighbor = currentPixel + imagePadOffsets(imageDirection); if imageLabelPad(neighbor) ~= 0 if (imageLabelPad(currentPixel) == START) && isempty(initial_departure_direction) initial_departure_direction = imageDirection; elseif (imageLabelPad(currentPixel) == START) && (initial_departure_direction == imageDirection) done = true; found_next_pixel = true; break; end next_search_direction = nextSearchDirectionLookup(imageDirection); found_next_pixel = true; numPixels = numPixels + 1; if numPixels > size(scratch, 1) scratch(2 * size(scratch, 1)) = 0; end scratch(numPixels) = neighbor; if imageLabelPad(neighbor) ~= START imageLabelPad(neighbor) = BOUNDARY; end currentPixel = neighbor; break; end imageDirection = searchDirectionLookup(imageDirection); end if ~found_next_pixel numPixels = 2; scratch(2) = scratch(1); done = true; end end [imageRow, imageColumn] = ind2sub(size(imageLabelPad), scratch(1:numPixels)); B{which} = [imageRow-1, imageColumn-1]; end end if strcmp(imageDirection, 'counterClockWise') for k = 1:length(B) B{k} = B{k}(end:-1:1, :); end endend rename variables to make it make more sense and more reliable
- Assistant
function B = boundaries(imageBinary, imageConnectivity, imageDirection) if nargin < 3 imageDirection = 'clockWise'; end if nargin < 2 imageConnectivity = 8; end
imageBinaryLabel = bwlabel(imageBinary, imageConnectivity); numberObjects = max(imageBinaryLabel(:)); if numberObjects > 0 B = cell(numberObjects, 1); else B = {}; end imageLabelPad = padarray(imageBinaryLabel, [1 1], 0, 'both'); imageLabelPadSize = size(imageLabelPad, 1); if imageConnectivity == 8 padOffsets = [-1, imageLabelPadSize-1, imageLabelPadSize, imageLabelPadSize+1, 1, -imageLabelPadSize+1, -imageLabelPadSize, -imageLabelPadSize-1]; nextDirectionLookup = [8 8 2 2 4 4 6 6]; searchDirectionLookup = [2 3 4 5 6 7 8 1]; else padOffsets = [-1, imageLabelPadSize, 1, -imageLabelPadSize]; nextDirectionLookup = [4 1 2 3]; searchDirectionLookup = [2 3 4 1]; end START_PIXEL = -1; BOUNDARY_PIXEL = -2; scratchArray = zeros(100, 1); [rRows, cCols] = find((imageLabelPad(2:end-1, :) > 0) & (imageLabelPad(1:end-2, :) == 0)); rRows = rRows + 1; for k = 1:length(rRows) r = rRows(k); c = cCols(k); if (imageLabelPad(r, c) > 0) && (imageLabelPad(r-1, c) == 0) && isempty(B{imageLabelPad(r, c)}) idx = (c - 1) * imageLabelPadSize + r; objectLabel = imageLabelPad(idx); scratchArray(1) = idx; imageLabelPad(idx) = START_PIXEL; numPixels = 1; currentPixel = idx; initialDepartureDirection = []; done = false; next_search_direction = 2; while ~done currentDirection = next_search_direction; foundNextPixel = false; for k = 1:length(padOffsets) neighbor = currentPixel + padOffsets(currentDirection); if imageLabelPad(neighbor) ~= 0 if (imageLabelPad(currentPixel) == START_PIXEL) && isempty(initialDepartureDirection) initialDepartureDirection = currentDirection; elseif (imageLabelPad(currentPixel) == START_PIXEL) && (initialDepartureDirection == currentDirection) done = true; foundNextPixel = true; break; end next_search_direction = nextDirectionLookup(currentDirection); foundNextPixel = true; numPixels = numPixels + 1; if numPixels > size(scratchArray, 1) scratchArray(2 * size(scratchArray, 1)) = 0; end scratchArray(numPixels) = neighbor; if imageLabelPad(neighbor) ~= START_PIXEL imageLabelPad(neighbor) = BOUNDARY_PIXEL; end currentPixel = neighbor; break; end currentDirection = searchDirectionLookup(currentDirection); end if ~foundNextPixel numPixels = 2; scratchArray(2) = scratchArray(1); done = true; end end [imageRow, imageColumn] = ind2sub(size(imageLabelPad), scratchArray(1:numPixels)); B{objectLabel} = [imageRow - 1, imageColumn - 1]; end end if strcmp(imageDirection, 'counterClockWise') for k = 1:length(B) B{k} = B{k}(end:-1:1, :); end endend
- User
function B = boundaries(imageBinary, imageConnectivity , imageCurrentDirection) if nargin < 3 imageCurrentDirection = 'clockWise'; end if nargin < 2 imageConnectivity = 8; end imageBinaryLabel = bwlabel(imageBinary, imageConnectivity ); numberObjects = max(imageBinaryLabel(:)); if numberObjects > 0 B = cell(numberObjects, 1); else B = {}; end imageLabelPad = padarray(imageBinaryLabel, [1 1], 0, 'both'); imageLabelPadSize = size(imageLabelPad,1); if imageConnectivity == 8 imagePadOffsets = [-1, imageLabelPadSize-1, imageLabelPadSize, imageLabelPadSize+1, 1, -imageLabelPadSize+1, -imageLabelPadSize, -imageLabelPadSize-1]; nextSearchDirectionLookup = [8 8 2 2 4 4 6 6]; searchDirectionLookup = [2 3 4 5 6 7 8 1]; else imagePadOffsets = [-1, imageLabelPadSize, 1, -imageLabelPadSize]; nextSearchDirectionLookup = [4 1 2 3]; searchDirectionLookup=[2 3 4 1]; end pixleStart = -1; pixleBoundary = -2; arrayScratch = zeros(100, 1); [rowRows,columnColumns] = find((imageLabelPad(2:end-1, :) > 0)&(imageLabelPad(1:end-2, :) == 0)); rowRows = rowRows+1; for k = 1:length(rowRows) r = rowRows(k); c = columnColumns(k);
if (imageLabelPad(r, c) > 0) && (imageLabelPad(r-1, c) == 0) && isempty(B{imageLabelPad(r, c)}) index = (c-1) * size(imageLabelPad, 1) + r; objectLabel = imageLabelPad(index); arrayScratch(1) = index; imageLabelPad(index) = pixleStart; pixleNumber = 1; currentPixel = index; initialDepartureDirection = []; finish = false; nextSearchDirection = 2; while ~finish imageCurrentDirection = nextSearchDirection; pixleNextDirection = false; for k = 1:length(imagePadOffsets) neighborPixle = currentPixel + imagePadOffsets(imageCurrentDirection); if imageLabelPad(neighborPixle) ~= 0 if (imageLabelPad(currentPixel) == pixleStart) && isempty(initialDepartureDirection ) initialDepartureDirection = imageCurrentDirection; elseif (imageLabelPad(currentPixel) == pixleStart) && (initialDepartureDirection == imageCurrentDirection) finish = true; pixleNextDirection = true; break; end nextSearchDirection = nextSearchDirectionLookup(imageCurrentDirection); pixleNextDirection = true; pixleNumber = pixleNumber + 1; if pixleNumber > size(arrayScratch, 1) arrayScratch(2 * size(arrayScratch, 1)) = 0; end arrayScratch(pixleNumber) = neighborPixle; if imageLabelPad(neighborPixle) ~= pixleStart imageLabelPad(neighborPixle) = pixleBoundary; end currentPixel = neighborPixle; break; end imageCurrentDirection = searchDirectionLookup(imageCurrentDirection); end if ~pixleNextDirection pixleNumber = 2; arrayScratch(2) = arrayScratch(1); finish = true; end end [imageRow, imageColumn] = ind2sub(size(imageLabelPad), arrayScratch(1:pixleNumber)); B{objectLabel } = [imageRow-1, imageColumn-1]; end end if strcmp(imageCurrentDirection, 'counterClockWise') for k = 1:length(B) B{k} = B{k}(end:-1:1, :); end endend improve this function, make sure it works the same, but code it differently
- Assistant
function B = boundaries(imageBinary, imageConnectivity, imageCurrentDirection) % Set default input arguments if nargin < 3 imageCurrentDirection = 'clockWise'; end if nargin < 2 imageConnectivity = 8; end
% Label binary image imageBinaryLabel = bwlabel(imageBinary, imageConnectivity); numObjects = max(imageBinaryLabel(:)); % Initialize output cell array if numObjects > 0 B = cell(numObjects, 1); else B = {}; end % Pad the labeled image imageLabelPad = padarray(imageBinaryLabel, [1, 1], 0, 'both'); imageLabelPadSize = size(imageLabelPad, 1); % Setup neighbor and search lookups if imageConnectivity == 8 padOffsets = [-1, imageLabelPadSize-1, imageLabelPadSize, imageLabelPadSize+1, 1, -imageLabelPadSize+1, -imageLabelPadSize, -imageLabelPadSize-1]; nextDirectionLookup = [8 8 2 2 4 4 6 6]; searchDirectionLookup = [2 3 4 5 6 7 8 1]; else padOffsets = [-1, imageLabelPadSize, 1, -imageLabelPadSize]; nextDirectionLookup = [4 1 2 3]; searchDirectionLookup = [2 3 4 1]; end % Constants START_PIXEL = -1; BOUNDARY_PIXEL = -2; % Initialize scratch array scratchArray = zeros(100, 1); % Find start rows and columns [startRows, startCols] = find((imageLabelPad(2:end-1, :) > 0) & (imageLabelPad(1:end-2, :) == 0)); startRows = startRows + 1; % Loop over each start point for k = 1:length(startRows) r = startRows(k); c = startCols(k); if (imageLabelPad(r, c) > 0) && (imageLabelPad(r-1, c) == 0) && isempty(B{imageLabelPad(r, c)}) currentIndex = (c - 1) * imageLabelPadSize + r; objectLabel = imageLabelPad(currentIndex); scratchArray(1) = currentIndex; imageLabelPad(currentIndex) = START_PIXEL; numPixels = 1; currentPixel = currentIndex; initialDepartureDirection = []; finish = false; nextSearchDirection = 2; while ~finish currentDirection = nextSearchDirection; foundNextPixel = false; for k = 1:length(padOffsets) neighborPixel = currentPixel + padOffsets(currentDirection); if imageLabelPad(neighborPixel) ~= 0 if (imageLabelPad(currentPixel) == START_PIXEL) && isempty(initialDepartureDirection) initialDepartureDirection = currentDirection; elseif (imageLabelPad(currentPixel) == START_PIXEL) && (initialDepartureDirection == currentDirection) finish = true; foundNextPixel = true; break; end nextSearchDirection = nextDirectionLookup(currentDirection); foundNextPixel = true; numPixels = numPixels + 1; if numPixels > size(scratchArray, 1) scratchArray(2 * size(scratchArray, 1)) = 0; end scratchArray(numPixels) = neighborPixel; if imageLabelPad(neighborPixel) ~= START_PIXEL imageLabelPad(neighborPixel) = BOUNDARY_PIXEL; end currentPixel = neighborPixel; break; end currentDirection = searchDirectionLookup(currentDirection); end if ~foundNextPixel numPixels = 2; scratchArray(2) = scratchArray(1); finish = true; end end [imageRow, imageColumn] = ind2sub(size(imageLabelPad), scratchArray(1:numPixels)); B{objectLabel} = [imageRow - 1, imageColumn - 1]; end end if strcmp(imageCurrentDirection, 'counterClockWise') for k = 1:length(B) B{k} = B{k}(end:-1:1, :); end endend