Find the median row of a binary column and replace the column with just the median row

2 次查看(过去 30 天)
I have some binary columns with ones and zeros. I would like to replace the places where there are multiple rows of ones with just one one in the median row index. I would like to do this for every column. For example:
1 0 1 1
1 0 1 1
1 1 1 0
0 1 1 0
0 1 0 0
becomes
0 0 0 1
1 0 1 0
0 0 0 0
0 1 0 0
0 0 0 0
The ones represent a curved y plot graph, that I would like to reduce down to a one pixel width (i.e. 1 y pixel per x value).
line.PNG
Thanks for any advice!
Chees

采纳的回答

mackhina
mackhina 2019-12-27
Got it. Not the most elegant solution, but it works. Thanks for the tips!
size_image = size(image)
filt_image = zeros(size_image)
for j = 1:size_image(2)
[rows, columns] = find((image(:,j)) > 0);
row_index = round(mean(rows));
filt_image(:,j) = image(:,j);
filt_image(:,j) = 0;
filt_image(row_index,j) = 1;
end

更多回答(3 个)

Image Analyst
Image Analyst 2019-12-26
Try bwmorph():
skeletonImage = bwmorph(binaryImage, 'skel', inf);
  1 个评论
mackhina
mackhina 2019-12-27
This reduces the line down nearly all the way, but there are lots of instances where I will have multiple y values for a single value of x.
errors.png

请先登录,再进行评论。


Image Analyst
Image Analyst 2019-12-27
Yes of course. Not every stretch will be a horizontal or diagonal stretch.

Andrei Bobrov
Andrei Bobrov 2019-12-27
I = imread('line.png');
bw = im2double(rgb2gray(I));
[i,j] = find(bw);
[n,g] = findgroups(j);
idx = floor(splitapply(@median,i,n));
[k,l] = size(bw);
out = zeros([k,l]);
out(sub2ind([k,l],idx,g)) = 1;

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by