I exactly want to do same thing explained in "what should I do so that only gray image converts into binary , not the black backgroung while moving the threshold." question. But when I count white and black pixels in ROI I am getting 0.

I exactly want to do same thing explained in "what should I do so that only gray image converts into binary , not the black background while moving the threshold." question. But when I count white and black pixels in ROI I am getting 0.
Following is my code.
if true
close all;
clear all;
%Read image in Matlab
ReadImg = imread('Sanjivjadhv25rckd.jpg');
figure(1); imshow(ReadImg);title('Original Image');
%Convert image into Gray Image
GrayImg = rgb2gray(ReadImg);
figure(2); imshow(GrayImg);title('Gray Image');
%ROI selection manually
height = 254 %189;
width = 254 %229;
[col1, row1] = ginput(1);
row2 = row1 + height;
col2 = col1 + width;
CropedImg = ReadImg(row1:row2, col1:col2);
figure(3); imshow(CropedImg); title('Region of Intrest');
%Removal of spekel noise using Median filter
medianFilteredIm2 = medfilt2(CropedImg,[4,4]);%using [4,4] window size getting better results
%figure(3); imshow(medianFilteredIm1);title('output of median filter');
figure(4); imshow(medianFilteredIm2);title('output of median filter');
%Segmentation of Kidney part using imfreehand
h = imfreehand();
mask = createMask(h);
medianFilteredIm2(~mask) = 0;
figure(5); imshow(medianFilteredIm2);
GrayThresh = 0.4; %graythresh(medianFilteredIm2);
BinaryImg = im2bw(medianFilteredIm2,GrayThresh);
figure(10); imshow(BinaryImg);title('Binary Image');
filledBinaryImage = imfill(BinaryImg, 'holes');
pixelsInROI = medianFilteredIm2(filledBinaryImage);
numBlackPixels = sum(pixelsInROI == 0);
numWhitePixels = sum(pixelsInROI == 255);
end
Following are images.

 Akzeptierte Antwort

I don't understand the question you quoted (due to bad grammar), and don't understand yours either. But I think you want this:
numWhitePixels = sum(filledBinaryImage(:));
numBlackPixels = numel(filledBinaryImage) - numWhitePixels ;

6 Kommentare

I referred following question "what should I do so that only gray image converts into binary , not the black background while moving the threshold" on Mathworks . I have same requirement. I also want to find ratio of black and white pixels of ROI. When I apply above given solution it is giving me all black pixels in image and white pixels of ROI.
I referred above code but it is giving number of white pixels in ROI and black pixels in whole image, but I want pixels value from ROI only.
Define exactly what is the ROI. Is it the mask area of the gray scale image or the white area in the binary image.
ROI is Kidney Image.In which both black and white pixels are there. But after binarization Black pixels are get added with background pixel,so I cannot able to count exact number of black pixel. OR is there any soltion like changing backgrond color to red or any other color and only ROI will change into black and white
Well somehow you have a mask image (what you call ROI) - whether by hand drawing or some other method. This is one binary image. Then you have the other binary image where you threshold or something and it eats into the kidney.
numImagePixels = numel(mask);
numWhiteMaskPixels = sum(mask(:));
numBlackMaskPixels = numImagePixels - numWhiteMaskPixels;
numWhiteKidneyPixels = sum(binaryImage(:));
numBlackKidneyPixels = numImagePixels - numWhiteKidneyPixels;
So that's everything. You can get the black pixels just inside the kidney by subtracting.
blackKidneyPixels = numBlackKidneyPixels - numBlackMaskPixels;
Anything else you need can be gotten also from subtractions.
Thank you.I got pixel value. But in next step of image processing, I want to do classification for chronic kidney disease. Stages of CKD will be decided on black and white ratio .For that I have to separate kidney in 10 equidistant part by using 3x3 squre morphological operator.but wihout proper kidney image how will i do that? As shown in fig 1. I got output up to that level. but as shown in fig 2 and 3 I am not getting output.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Images finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 21 Feb. 2014

Kommentiert:

am 23 Feb. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by