image to binary issues
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi,
i want to run a single image through multiple thresholds. Initially, i had separate lines of code for each threshold (im2bw(img,.1), im2bw(img,.2) and so on). Now i want to run the image between thresholds ranging from 0:.001:.5. thats a total of 500 hundred values. How do I write that in few lines rather than write 500 lines?
I have tried for loop
thld = 0 : .001 : .5
for i=1:500
a(i)=im2bw(img,thld)
end
but this doesn't work.
Thank you for the help
채택된 답변
Guillaume
2014년 10월 23일
Nearly there. Use cell arrays for storing your images or a 3d matrix:
thld = 0 : .001 : .5;
for idx = 1:numel(thld)
a{idx} = im2bw(img, thld(idx));
end
or
thld = 0 : .001 : .5;
a = zeros([size(img), numel(thld)]); %better preallocate huge array
for idx = 1:numel(thld)
a(:,:, idx) = im2bw(img, thld(idx));
end
댓글 수: 8
jchris14
2014년 10월 23일
This works well. But now need to find the difference in the number of black pixels to white pixels.
I used to do,
wpix=sum(a(:)); %a is the im2bw function
bpix=numel(a)-wpix;
this wont work with cell arrays unfortunately. Any ideas? I tried using cell2mat with no luck unless im using it wrong.
thanks again
Image Analyst
2014년 10월 23일
편집: Image Analyst
2014년 10월 23일
First of all, img better be a double image. If it's a uint8 image, or even a double image that was created from a uint8 image with something like imdouble(img) or img/max(img(:)) then you're not going to get more than 256 threshold, not 501. You can do this:
thld = 0 : .001 : .5;
numWhitePixels = zeros(1, length(thld));
numBlackPixels = zeros(1, length(thld));
for idx = 1:numel(thld)
thisImage = im2bw(img, thld(idx));
numWhitePixels(idx) = sum(thisImage(:));
numBlackPixels(idx) = numel(thisImage) - numWhitePixels(idx);
% Save to a cell array only if
% you need all images after the loop has exited.
a{idx} = thisImage;
end
I am trying to quantify a TIF image. But this is the error i get when i run the code.
Error using im2bw Expected input number 1, I, X or RGB, to be one of these types:
single, uint8, uint16, int16, logical, double
Instead its type was char.
Error in im2bw>parse_inputs (line 79)
validateattributes(varargin{1},...
Error in im2bw (line 38)
[A,map,level] = parse_inputs(varargin{:});
Error in R1 (line 20) thisImage = im2bw(img
Your img is a character string. Did you pass the filename to im2bw() instead of the image array? It should look like this:
img = imread(fullFileName);
binaryImage = im2bw(img,..............
jchris14
2014년 10월 23일
My code says
a= uigetfile('*.tif');
thld = 0 : .001 : .5;
numWhitePixels = zeros(1, length(thld));
numBlackPixels = zeros(1, length(thld));
for idx = 1:numel(thld)
thisImage = im2bw(a, thld(idx));
numWhitePixels(idx) = sum(thisImage)
numBlackPixels(idx) = numel(thisImage) - numWhitePixels(idx)
% Save to a cell array only if
% you need all images after the loop has exited.
%a{idx} = thisImage;
end
Guillaume
2014년 10월 23일
Use more descriptive name than a for your variable and you'll see where you go wrong.
You ask for a filename with uigetfile but you never load the image. In addition even if you'd loaded the image in a, you would have overwritten it in the loop since you're also using a for storing the binary image.
Your code should be something like:
imgfilename = uigetfile('*.tif');
sourceimage = imread(imgfilename);
...
binaryimage = im2bw(sourceimage, thld(idx));
binaryimages{idx} = binaryimage;
Image Analyst
2014년 10월 23일
Yep, like I said, the badly-named "a" is the culprit. It's a string just like I said. Did you use imread() like I suggested in my prior comment? No, not in that code you just posted. Why don't you use my suggestion? It will be alright then if you do.
jchris14
2014년 10월 23일
Thank you for the help. I will use them.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
