How to scan an image from a specified Row?

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

whats A in the function?
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

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

Note that your code will error if all the pixels at the top or the bottom of a row are below 26, since your first while loop can try to access pixels on row 0, -1, and the second on row height+1, height+2.
Secondly, are you sure that the condition you want for scanning up or down is | (or) and not & (and) ?
Going back to my comment about using meaningful variable names, don't you find the following a lot more self documenting and thus easier to understand:
function [blackstartingrows, blackendingrows] = get_Black_edges(img)
[height, width] = size(img);
blackstartingrows = zeros(1, width); %better preallocate as well, rather than grow
blackendingrows = zeros(1, width);
for column = 1:width
columnpixels = img(:, column);
rowsbelowthresh = find(columnpixels < 26);
meanrow = floor(mean(rowsbelowtresh));
%the whole while loop can be replaced with just one line:
blackstartrow = find([Inf columnspixel(1:meanrow)] > 26 & [Inf Inf columnspixel(1:meanrow-1) > 26], 1, 'last') + 1;
blackstartingrows(column) = blackstartrow;
%same with other loop, it's just:
blackendrow = find([columnspixel(meanrow+1:end) Inf] > 26 & [columnspixel(meanrow+2:end) Inf Inf], 1, 'first') - 1;
blackendingrows(column) = blackendrow;
end
end
Hi thanks I really appreciate the feedback, I slightly adjusted the code above due to some typos:
function [blackstartingrows, blackendingrows] = get_Black_edges3(A)
[height, width] = size(A);
blackstartingrows = zeros(1, width); %better preallocate as well, rather than grow
blackendingrows = zeros(1, width);
for column = 1:width
columnpixels = A(:, column);
rowsbelowthresh = find(columnpixels < 26);
meanrow = floor(mean(rowsbelowthresh));
%the whole while loop can be replaced with just one line:
blackstartrow = find([Inf columnpixels(1:meanrow)] > 26 & [Inf Inf columnpixels(1:meanrow-1) > 26], 1, 'last') + 1;
blackstartingrows(column) = blackstartrow;
%same with other loop, it's just:
blackendrow = find([columnpixels(meanrow+1:end) Inf] > 26 & [columnpixels(meanrow+2:end) Inf Inf], 1, 'first') - 1;
blackendingrows(column) = blackendrow;
end
end
columnspixel and columnpixels as an example, I ran the code however and the following error came up:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in get_Black_edges3 (line 12)
blackstartrow = find([Inf columnpixels(1:meanrow)] > 26 & [Inf Inf columnpixels(1:meanrow-1) > 26], 1, 'last') + 1;
Is there a problem with the adjustments I made?
Again thanks for the help! and yes I can see through the methodology of better use of variable names! much easier to follow!
Sean, please read this and fix your code.
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;
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 Convert Image Type 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!