How to apply a different b/w threshold to each row of the image?

조회 수: 1 (최근 30일)
Nut
Nut 2016년 6월 13일
댓글: Image Analyst 2020년 1월 22일
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
Roman Boldin
Roman Boldin 2020년 1월 22일
Hello, do you still have the code of for and loop?
Image Analyst
Image Analyst 2020년 1월 22일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Andrew Bliss
Andrew Bliss 2016년 6월 13일
Depending on the input type of your image, you may be able to just do a simple thresholding operation (as below), otherwise you'll have to delve a little deeper into image processing.
asdf=imread('example.tif');
figure,imshow(asdf)
zxcv=rgb2gray(asdf);
figure,imshow(zxcv)
thresh=[50*ones(325,1);150*ones(325,1)]; %here you set the threshold for each row
threshMatrix=repmat(thresh,1,600);
BW=zxcv>threshMatrix;
figure,imshow(BW)
  댓글 수: 2
Nut
Nut 2016년 6월 14일
Thank you very much for your answer, it may be a good way. I'll compare this with the for loop.
Nut
Nut 2016년 6월 14일
You're right. The execution time with your solution is about 1.6 seconds, instead using im2bw and the for loop it is about 5.1 seconds. Very good idea, thank you again.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 6월 14일
편집: Image Analyst 2016년 6월 14일
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
Image Analyst
Image Analyst 2016년 6월 15일
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
Nut 2016년 6월 16일
I'll try it, thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by