필터 지우기
필터 지우기

Counting black and white pixels

조회 수: 3 (최근 30일)
Matthew Alston
Matthew Alston 2014년 7월 16일
댓글: Matthew Alston 2014년 7월 16일
After converting from RGB image to binary image I'm looking to count total number of white and black pixels in an image. I found a demo posted a while back, but am getting a little error at the very of the script when it gets to the message and msgbox. I'm not sure I'm reading the code correctly because the line breaks are a little strange in the demo, could you take a look at the demo (attached) to see if I have it correct?
The error I get is: >Error: File: pixelDemo.m Line: 34 Column: 16 >Unbalanced or unexpected parenthesis or bracket.
Line 34 is the command: msgbox (message);

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 7월 16일
편집: Geoff Hayes 2014년 7월 16일
It is line 33 of the attached file that is causing the problem
message = sprintf('Done!\nTotal number of pixels = %d\nBlack pixels = %d = %.1f%%\nWhite pixels = %d = %.1f%%', ...totalNumberOfPixels, numberOfBlackPixels, percentBlackPixels, ...numberOfWhitePixels, percentWhitePixels);
Note the ellipsis (...) which is used in MATLAB to indicate that code on the subsequent line follows code from the previous line. For example
m=42;
fprintf('this is a test of printing of the number %d\n',...
m);
is equivalent to
m=42;
fprintf('this is a test of printing of the number %d\n', m);
In your code, there are two instances of the ellipsis and so either they should be removed, or line breaks should be added. If doing the latter, then the above line of code becomes three lines of code as
message = sprintf('Done!\nTotal number of pixels = %d\nBlack pixels = %d = %.1f%%\nWhite pixels = %d = %.1f%%', ...
totalNumberOfPixels, numberOfBlackPixels, percentBlackPixels, ...
numberOfWhitePixels, percentWhitePixels);
Just replace line 33 with the above (or simply remove the ellipses) and see what happens!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by