Prev: Help with function and syms
Next: Bar graph in Matlab
From: JordanPainter Painter on 13 Apr 2010 09:18 Hiya, I'm trying to set a threshold to reduce my values to 1's and 0's then get a total of these 0's and 1's so i can work out a ratio of black to white? My image is a a large jpeg file. I've tried this so far: im = 'image.jpg'; im = uint8(im); level = graythresh(im); BW = im2bw(im,level); x = imread(BW); blackcount = sum(BW(1,:)) whitecount = sum(BW(:,1)) I used the sum code at the bottom for just the standard image but it only pulled up the completely white and completely black pixels i think. So i added the threshold but i can't get it to work. Any help is welcome, thanks a lot!
From: ImageAnalyst on 13 Apr 2010 12:14 blackCount = sum(sum(BW == 0)); whiteCount = sum(sum(BW)); That x=imread(BW) line is unneeded and wrong, as well as your indices in your count lines. Also, make sure im is 2D monochrome, not 3D RGB image.
From: JordanPainter Painter on 13 Apr 2010 12:44 ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <db7d7856-358d-4d47-aaf1-500e413542dc(a)u22g2000yqf.googlegroups.com>... > blackCount = sum(sum(BW == 0)); > whiteCount = sum(sum(BW)); > > That x=imread(BW) line is unneeded and wrong, as well as your indices > in your count lines. Also, make sure im is 2D monochrome, not 3D RGB > image. Ah right, i tried your values instead. It seems to only view one line of 9 pixels instead of the entire matrix. Other than that i think this works. Any ideas on how to get it analyse the entire image. Thanks :)
From: JordanPainter Painter on 13 Apr 2010 13:15 This is my latest code: im = 'crack.jpg'; im = uint8(im); % imshow(im); level = graythresh(im); BW = im2bw(im,level); imwrite(BW,'black.jpg'); img = 'black.jpg'; imread(img); blackcount = sum(sum(img == 0)) whitecount = sum(sum(img)) It gives me the answer: blackcount = 0 whitecount = 876 I'm not sure if saving the output black and white file and then using imread on that is the right idea?
From: Steven Lord on 13 Apr 2010 13:33
"JordanPainter Painter" <jordanpainter(a)hotmail.co.uk> wrote in message news:hq28rf$h9t$1(a)fred.mathworks.com... > This is my latest code: > > im = 'crack.jpg'; > im = uint8(im); > % imshow(im); > level = graythresh(im); > BW = im2bw(im,level); > imwrite(BW,'black.jpg'); > img = 'black.jpg'; > imread(img); > > blackcount = sum(sum(img == 0)) > whitecount = sum(sum(img)) > > It gives me the answer: > > blackcount = 0 > whitecount = 876 > > I'm not sure if saving the output black and white file and then using > imread on that is the right idea? Those answers are correct for the code as written. Hint 1: When you execute the lines when you compute blackcount and whitecount, what does the variable img contain? Hint 2: Call IMREAD with an output argument. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ |