MATLAB Answers

# How to detect first 50 white pixels from right side of a binary image?

2 ビュー (過去 30 日間)
anshumala rakesh 2019 年 2 月 24 日
コメント済み: Image Analyst 2019 年 2 月 24 日
i have a binary image and for extracting the REGIIN OF INTEREST we need to detect the first 50 white pixels from right side of the image. can anyone suggest me how to do it?

サインインしてコメントする。

### 採用された回答

Rik 2019 年 2 月 24 日

You can use find to get the indices from the left, so the only thing you need to do is flipping the image and convert the col indices:
A=[1 0 0;0 1 0;1 1 1];
N_pixels=3;
A=fliplr(A);%flip since find looks from the upper left corner
[r,c]=find(A,N_pixels);
c=size(A,2)-c+1;%flip indices back
%prove this is indeed correct:
B=accumarray([r,c],ones(size(r)),size(A))
%this may be faster depending on the size of your matrix and number of points:
%B=zeros(size(A));ind=sub2ind(size(B),r,c);B(ind)=1

#### 4 件のコメント

Image Analyst 2019 年 2 月 24 日
"first 50 white pixels from right side of the image" is very ambigous, so you'd better post your image. In fact the first 50 pixels in a column major fashion may not even be in the same blob! They may be in two different blobs. And why 50? What if those 50 are in a blob that is much bigger and crosses many rows and columns in an irregular shape? Do you want the whole shape? Probably, but I don't know since I haven't seen your image. Or are you trying to identify blobs that touch the left edge specifically because you want to retain them after doing an imclearborder?
Again we need to see your image and have a better explanation so we can decide on the best algorithm.
anshumala rakesh 2019 年 2 月 24 日
Actually I have this binary image as my input and I wanted to extract only the region marked in red as output.
So I thought of detecting the first few white pixels from right side which fits that region. here 50 is just a random guess . l just wanted to know about the function.
Image Analyst 2019 年 2 月 24 日
The code Rik gave you won't work to extract that red region.
This is a classic case of someone saying "How do I do X" and then someone tells them how to do X. Then they say "That doesn't work". Then you ask what they REALLY want to do (the larger picture) and they say "What I really want to do is Y". Then the answerer says "Well if you really want to do Y then you don't want to do X at all, you want to do Z."
So now we need to know exactly what pixels should be in the red region. It appears to be a circle fitted to the right edge of a blob that is required to touch the left edge (or maybe not touch - I don't know). But the question is where is the left edge of the red circle? If you fit a circle to the pixels on the right of the blob, or a convex hull of it, then where the right part of the red circle will be depends on how far to the left you take pixels to do the circule fit.
Perhaps if you saw your gray scale, pre-segmentation image, it might give us another clue. Otherwise give us some idea of how we can determine what boundary pixels to fit to a circle.

サインインしてコメントする。

Translated by