How to scan an image from a specified Row?

4 views (last 30 days)
So below is a function Im using to locate the beginning and end of dark regions on a greyscale image:
function [Ssb Seb]=get_Black_edges(A)
[r c]=size(A);
Ssb=[]; %start Black
Seb=[]; %end Black
for k=1:1:c
x=A(:,k);
a=find(x<26);
mid=floor(mean(a));
i=mid;
while(x(i-1) <26 | x(i-2) <26)
i=i-1;
end
sb=i;
Ssb=[Ssb ; sb];
i=mid;
while(x(i+1) <26 | x(i+2) <26)
i=i+1;
end
eb=i;
Seb=[Seb ; eb];
end
return
Say my image is 886x1024, how to I adjust the code so that the scanning ignores the first 40 rows and only scans the remaining 846?
  2 Comments
Stelios Fanourakis
Stelios Fanourakis on 25 Jan 2019
I get the error
Subscript indices must either be real positive integers or logicals.
Error in efr>get_Black_edges (line 16)
while (x(i-1) <26 | x(i-2) <26)
Error in efr (line 3)
get_Black_edges(A);
>>

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 10 Feb 2015
Your code would be so much easier to understand if you'd use meaningful variable names. It's particularly confusing that you're using x to represent pixel intensities when it's usually used to represent pixel coordinates.
In any case, why can't you just pass the portion of the image of interest to your function? That is instead of:
[blackstartingrows, blackendingrows] = get_Black_edges(someimage);
do
[blackstartingrows, blackendingrows] = get_Black_edges(someimage(41:end, :));
blackstartingrows = blackstartingrows + 40; %readjust to original image coordinates
blackendingrows = blackendingrows + 40;
  6 Comments
Guillaume
Guillaume on 10 Feb 2015
Sean, yes, I didn't test the code, just typed it in my answer, so expect some typos and minor bugs.
I forgot that columnpixels was a column (!). Therefore you need a vertical concatenation (a.k.a semicolons), that is:
blackstartrow = find([Inf; columnpixels(1:meanrow)] > 26 & [Inf; Inf; columnpixels(1:meanrow-1) > 26], 1, 'last') + 1;
and
blackendrow = find([columnpixels(meanrow+1:end); Inf] > 26 & [columnpixels(meanrow+2:end); Inf; Inf], 1, 'first') - 1;
Sean
Sean on 10 Feb 2015
Hi, I got the code working! Thanks a lot again for the help. Much appreciated!

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!