How to apply a different b/w threshold to each row of the image?
Show older comments
Hi,
I would convert an image from grayscale to black-and-white using the im2bw function, and I need to apply a different threshold to each row of the image. Which is the most efficient way to do this? Is there a way to avoid the "for" cycle?
Thank you very much.
2 Comments
Roman Boldin
on 22 Jan 2020
Hello, do you still have the code of for and loop?
Image Analyst
on 22 Jan 2020
Roman, it would be something like
[rows, columns, numberOfColorChannels] = size(theImage);
binaryImage = false(rows, columns);
for row = 1 : rows
% Get the threshold for this row - however you do it (I don't know).
thisThreshold = whatever;
% Now threshold/binarize the image for this row only.
binaryImage(row, :) = theImage(row, :) > thisThreshold;
end
Accepted Answer
More Answers (1)
Image Analyst
on 14 Jun 2016
Edited: Image Analyst
on 14 Jun 2016
0 votes
No, you'll have to use a for loop. It's not a problem though. It will be very fast. No need to worry about for loops that are only a few thousand iterations.
Why do you need a different threshold for each row anyway?
You might be able to use a different function. There are new binarization functions. See Steve's blog:
7 Comments
Nut
on 14 Jun 2016
Guillaume
on 14 Jun 2016
I don't see why you would need a for loop. You obviously start with a column vector of threshold values. Simply repmat your column vector with the number of columns of the image, and compare the image to that matrix, as per Andrew Bliss' answer.
Image Analyst
on 14 Jun 2016
Edited: Image Analyst
on 14 Jun 2016
When you assumed that you needed a new threshold at each row and said that your current approach was to do it row by row, the most logical assumption (at least to me) was that the threshold depended on the image data in that row. Doesn't that seem reasonable? You mentioned using im2bw() but that determines a global threshold based on the whole image if you just pass it the whole image, and that was unacceptable to you. Indicating that using im2bw to do the automatic threshold was an acceptable threshold determination method but that you needed it to give different thresholds based on each row was a strong indication that you needed to pass each row to im2bw() and did not know the thresholds in advance.
Note that Andrew's answer assumes that somehow you know the threshold in advance for each row. If you do, then use his answer. If you need to determine the threshold on a row-by-row basis then you'll need to use my answer since you would need to get the row of image data to examine it.
Since you accepted Andrews answer you must know the thresholds in advance, so tell me how you got thresholds in advance that were different for each row? Do the thresholds not depend on the image data?
Image Analyst
on 15 Jun 2016
OK. I have a nice manual/interactive thresholding utility in my File Exchange if you're interested: http://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image
Nut
on 16 Jun 2016
Categories
Find more on Image Preview and Device Configuration 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!