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.

1 view (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 21 Feb 2014
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 Comments
Image Analyst
Image Analyst on 22 Feb 2014
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.
Renuka
Renuka on 23 Feb 2014
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!