How can measure distance after threshold

4 次查看(过去 30 天)
Hello,
how can measure the disance (x , y) after threshold (x, y) between the edge of image and the white region.
  2 个评论
weam
weam 2023-7-14
编辑:weam 2023-7-14
the distance from the right side to any white region , and from bottom to the any white region.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2023-7-14
"the distance from the right side to any white region , and from botton to the any white region. "
To find the distance from the right side of the image to any white pixel, you can get that by scanning the image line by line
[rows, columns] = size(binaryImage);
rightEdges = zeros(rows, 1);
for row = 1 : rows
t = find(binaryImage(row, :), 1, 'last');
if ~isempty(t)
rightEdges(row) = t;
end
end
% Similarly to find the last row in each column, scan column-by-column:
bottomEdges = zeros(1, columns);
for col = 1 : columns
t = find(binaryImage(:, col), 1, 'last');
if ~isempty(t)
bottomEdges(col) = t;
end
end
% Find the closest pixel to the right edge of the image
[rightDistance, closestRow] = min(rightEdges)
% Find the closest pixel to the bottom edge of the image
[bottomDistance, closestCol] = min(bottomEdges)
  1 个评论
Walter Roberson
Walter Roberson 2023-7-14
By the way, the algorithm I posted in my Answer is a vectorized way of finding the last set pixel

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2023-7-13
Use bwdist on the inverse of the binary image.
help bwdist
BWDIST Distance transform of binary image. D = BWDIST(BW) computes the Euclidean distance transform of the binary image BW. For each pixel in BW, the distance transform assigns a number that is the distance between that pixel and the nearest nonzero pixel of BW. BWDIST uses the Euclidean distance metric by default. BW can have any dimension. D is the same size as BW. [D,IDX] = BWDIST(BW) also computes the closest-pixel map in the form of an index array, IDX. (The closest-pixel map is also called the feature map, feature transform, or nearest-neighbor transform.) IDX has the same size as BW and D. Each element of IDX contains the linear index of the nearest nonzero pixel of BW. [D,IDX] = BWDIST(BW,METHOD) lets you compute an alternate distance transform, depending on the value of METHOD. METHOD can be 'cityblock', 'chessboard', 'quasi-euclidean', or 'euclidean'. METHOD defaults to 'euclidean' if not specified. METHOD may be abbreviated. The different methods correspond to different distance metrics. In 2-D, the cityblock distance between (x1,y1) and (x2,y2) is abs(x1-x2) + abs(y1-y2). The chessboard distance is max(abs(x1-x2), abs(y1-y2)). The quasi-Euclidean distance is: abs(x1-x2) + (sqrt(2)-1)*abs(y1-y2), if abs(x1-x2) > abs(y1-y2) (sqrt(2)-1)*abs(x1-x2) + abs(y1-y2), otherwise The Euclidean distance is sqrt((x1-x2)^2 + (y1-y2)^2). Notes ----- BWDIST uses a fast algorithm to compute the true Euclidean distance transform. The other methods are provided primarily for pedagogical reasons. The function BWDIST changed in version 6.4 (R2009b). Previous versions of the Image Processing Toolbox used different algorithms for computing the Euclidean distance transform and the associated closest-pixel map matrix. If you need the same results produced by the previous implementation, use the function BWDIST_OLD. Class support ------------- BW can be numeric or logical, and it must be nonsparse. D is a single matrix with the same size as BW. The class of IDX depends on number of elements in the input image, and is determined using the following table. Class Range -------- -------------------------------- 'uint32' numel(BW) <= 2^32-1 'uint64' numel(BW) >= 2^32 Example 1 --------- % Here is a simple example of the Euclidean distance transform: bw = zeros(5,5); bw(2,2) = 1; bw(4,4) = 1; [D,IDX] = bwdist(bw) Example 2 --------- % This example compares 2-D distance transforms for the four methods: bw = zeros(200,200); bw(50,50) = 1; bw(50,150) = 1; bw(150,100) = 1; D1 = bwdist(bw,'euclidean'); D2 = bwdist(bw,'cityblock'); D3 = bwdist(bw,'chessboard'); D4 = bwdist(bw,'quasi-euclidean'); RGB1 = repmat(rescale(D1), [1 1 3]); RGB2 = repmat(rescale(D2), [1 1 3]); RGB3 = repmat(rescale(D3), [1 1 3]); RGB4 = repmat(rescale(D4), [1 1 3]); figure subplot(2,2,1); imshow(RGB1); title('Euclidean'); hold on; imcontour(D1); subplot(2,2,2); imshow(RGB2); title('City block'); hold on; imcontour(D2); subplot(2,2,3); imshow(RGB3); title('Chessboard'); hold on; imcontour(D3); subplot(2,2,4); imshow(RGB4); title('Quasi-Euclidean'); hold on; imcontour(D4); Example 3 --------- % This example compares isosurface plots for the distance transforms of % a 3-D image containing a single nonzero pixel in the center: bw = zeros(50,50,50); bw(25,25,25) = 1; D1 = bwdist(bw); D2 = bwdist(bw,'cityblock'); D3 = bwdist(bw,'chessboard'); D4 = bwdist(bw,'quasi-euclidean'); figure subplot(2,2,1); isosurface(D1,15); axis equal; view(3); camlight; lighting gouraud; title('Euclidean'); subplot(2,2,2); isosurface(D2,15); axis equal; view(3); camlight; lighting gouraud; title('City block'); subplot(2,2,3); isosurface(D3,15); axis equal; view(3); camlight; lighting gouraud; title('Chessboard'); subplot(2,2,4); isosurface(D4,15); axis equal; view(3); camlight; lighting gouraud; title('Quasi-Euclidean'); See also BWDIST_OLD, BWULTERODE, WATERSHED. Documentation for bwdist doc bwdist Other uses of bwdist gpuArray/bwdist

Walter Roberson
Walter Roberson 2023-7-14
First trim the white border off of the image. Then binarize and take the inverse of the image, so that the black become 1 and the white become 0. Then flipud() and fliplr() the inverse.
Now
vert_profile = fliplr(sum(cumprod(FlippedInverse,1),1));
horz_profile2 = flipud(sum(cumprod(FlippedInverse,2),2));
For any given column K of the cropped image, vert_profile(K) is the number of black rows at the bottom of the image (possibly 0). For any given row K of the cropped image, horz_profile(K) is the number of black columns at the right side of the image (possibly 0)

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by