I am trying to get pixel intensity values from regions of interest in RGB images.
I segmented the image and saved the regions of interest (ROI) using regionprops 'PixelList' in MATLAB, as shown below:
In this example I am using "onion.png" image built in MATLAB. (But in reality I have hundreds of images, and each of them have several ROIs hence why I'm saving the ROIs separately.)
%SEGMENTATION PROGRAM:
a=imread('C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\images\imdata\onion.png');warning('off', 'Images:initSize:adjustingMag');
figure; imshow(a,[]);
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
bgd=(rr+bb)./(2*gg); %calculation RGB ratio of background
zr1=bgd>1.15; %matrix containing background as 1 and ROI as 0
% remake binary image for holes command which requires white object to fill % (this step is not relevant for this example, but it is for my data)
zr2=zr1<0.5;
zr3=imfill(zr2, 'holes');
figure;imshow(zr3); pause;
roi=regionprops(zr3,'Centroid','PixelList','Area');
ar=[roi.Area];
% find sort order , largest first
[as, ia]=sort(ar(1,:),'descend');
for w=1:length(roi); xy(w,:)=roi(w).Centroid;end
% use sort index to put cenrtoid list in same order
xy1=xy(ia,:);
%and pixel id list
for w=1:length(roi)
roi2(w).PixelList=roi(ia(w)).PixelList;
end
%extract centriod positions as two colums
%SAVE PIXEL LIST FOR EACH ROI IN A SEPARATE FILE
for ww=1:w;
k=roi2(ww).PixelList;
save('onion_PL','k');
end
How do I use this pixel list to get the intensity values in the original image? More specifically, I need to get the ratio of pixels in Green channel over Red ("grr=rdivide(gg,rr);"), but only in the region of interest labeled with PixelList. Here's my code so far:
%PL is the PixelList output we got above.
a=imread('C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\images\imdata\onion.png');warning('off', 'Images:initSize:adjustingMag');
PL=dir(['*PL.mat']); %load file PixelList files. "dir" is a variable with directory path containing the pixelist files. In this example, we saved "onion_PL.mat"
for m=1:length(PL);
load(PL(m).name);
ex=[]; %empty matrix to hold the extracted values
for mm=1:length(k);
%INSERT ANSWER HERE
end
This next bit is wrong because it's based on the entire image ("a"), but it contains the calculations that I would like to perform in the ROIs
figure; imshow(a,[]);
pause;
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
grr=rdivide(gg,rr);
I am brand new to MATLAB, so my code is not the greatest... Any suggestions will be greatly appreciated. Thank you in advance!
The loop you are looking for seems simple:
grr = zeros(nrows, ncols); % Initialize grr with zeros.
for mm = 1:length(k)
x = k(mm, 1); % Get the X (column) coordinate.
y = k(mm, 2); % Get the Y (row) coordinate.
grr(y, x) = gg(y, x) / rr(y, x);
end
A more efficient solution is using sub2ind for converting the x,y coordinates to linear indices:
% Convert k to linear indices.
kInd = sub2ind([nrows, ncols], k(:,2), k(:,1));
% Update only the indices in the PixelList.
grr(kInd) = rdivide(gg(kInd), rr(kInd));
In your given code sample there are 5 PixelLists.
I don't know how do you want to "arrange" the result.
In my code sample, I am saving the 5 results to 5 mat files.
Here is an executable code sample:
close all
%SEGMENTATION PROGRAM:
a=imread('onion.png');warning('off', 'Images:initSize:adjustingMag');
figure; imshow(a,[]);
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
bgd=(rr+bb)./(2*gg); %calculation RGB ratio of background
zr1=bgd>1.15; %matrix containing background as 1 and ROI as 0
% remake binary image for holes command which requires white object to fill % (this step is not relevant for this example, but it is for my data)
zr2=zr1<0.5;
zr3=imfill(zr2, 'holes');
figure;imshow(zr3); %pause;
roi=regionprops(zr3,'Centroid','PixelList','Area');
ar=[roi.Area];
% find sort order , largest first
[as, ia]=sort(ar(1,:),'descend');
for w=1:length(roi); xy(w,:)=roi(w).Centroid;end
% use sort index to put cenrtoid list in same order
xy1=xy(ia,:);
%and pixel id list
for w=1:length(roi)
roi2(w).PixelList=roi(ia(w)).PixelList;
end
%extract centroid positions as two columns
%SAVE PIXEL LIST FOR EACH ROI IN A SEPARATE FILE
for ww=1:w
k=roi2(ww).PixelList;
%save('onion_PL', 'k');
save(['onion', num2str(ww), '_PL'], 'k'); % Store in multiple files - onion1_PL.mat, onion2_PL.mat, ... onion5_PL.mat
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear % Use clear for testing - the variables are going to be read from the mat file.
%PL is the PixelList output we got above.
a=imread('onion.png');warning('off', 'Images:initSize:adjustingMag');
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
grr=rdivide(gg,rr);
PL=dir('*PL.mat'); %load file PixelList files. "dir" is a variable with directory path containing the pixelist files. In this example, we saved "onion_PL.mat"
for m = 1:length(PL)
load(PL(m).name);
ex=[]; %empty matrix to hold the extracted values
%for mm=1:length(k)
%INSERT ANSWER HERE
grr = zeros(nrows, ncols); % Initialize grr with zeros.
for mm = 1:length(k)
x = k(mm, 1); % Get the X (column) coordinate.
y = k(mm, 2); % Get the Y (row) coordinate.
grr(y, x) = gg(y, x) / rr(y, x);
end
% Instead of using a loop, it's more efficient to use sub2ind
if false
% Convert k to linear indices.
kInd = sub2ind([nrows, ncols], k(:,2), k(:,1));
% Update only the indices in the PixelList.
grr(kInd) = rdivide(gg(kInd), rr(kInd));
end
figure;imshow(grr);title(['grr of m=', num2str(m)]) % Show grr for testing.
save(['grr', num2str(m)], 'grr'); % Save grr for testing.
imwrite(imadjust(grr, stretchlim(grr)), ['grr', num2str(m), '.png']); % Store grr as image for testing
end
First two grr matrices as images (used for testing):
grr1.png:
grr2.png:
I just want to threshold it before applying algorithm.i tried it in one algorithm and got perfect result.but here i considered binary image and then masking.i just want the intervertebral disc(ellipse portion) which is in between vertebrae .can you suggest any other method?
close all;
clear all;
%%
Img = imread('Sub.png');
Img=rgb2gray(Img);%convert to single channel as data do not contain color image
Img=im2bw(Img,.16);
fig1=figure;
subplot(221)
imshow(Img); title('Original Image')
m1 = uint8(Img>35);
subplot(222), imshow(m1,[]); title('Mask m1');
m2=uint8(Img<300);
subplot(223), imshow(m2,[]); title('Mask m2');
sd = stdfilt( double(Img), ones(3,3));
m3= uint8(sd<18);
subplot(224), imshow(m3,[]); title('Mask m3');
fig2=figure;
I = uint8(Img.* m1);
I = uint8(I.* m2);
I = uint8(I.* m3);
subplot(221), imshow(I);
title('Masked Img')
i'm getting error as
I cannot view mask1 , mask 2, mask3 and also I'm getting error
Error using .*
Integers can only be combined with integers of the same class, or scalar doubles.
Error in test1 (line 22)
corrected code is
close all;
clear all;
%%
Img = imread('Sub.png');
Img=rgb2gray(Img);%convert to single channel as data do not contain color image
Img=im2bw(Img,.16);
fig1=figure;
subplot(221)
imshow(Img); title('Original Image')
m1 = uint8(Img>35);
subplot(222), imshow(m1,[]); title('Mask m1');
m2=uint8(Img<300);
subplot(223), imshow(m2,[]); title('Mask m2');
sd = stdfilt( double(Img), ones(3,3));
m3= uint8(sd<18);
subplot(224), imshow(m3,[]); title('Mask m3');
fig2=figure;
Img(~m1)=0;
Img(~m2)=0;
Img(~m3)=0;
subplot(221), imshow(Img);
title('Masked Img')
There are probably multiple issues with the code. While applying a mask using multiplication is technically possible, I recommend to use indexing instead:
Img(~mask)=0;
This code sets every pixel which is not in mask to zero.
There is probably another issue with your code. You are using uint8 to get a uint8 image, but the function does not rescale. If you input a normalized double image (values between 0 and 1) you will end up with a black image because it contains only 0 and 1 values. Use im2uint8 to convert images, but I think what you are trying to do here could be done on the double image as well.
I am trying to read GIF file and display in below following format -
Grey Scale
Resized
Double Image
Thinning
Inverted
Below is my code to do the same (incomplete):
clear all;
close all;
clc;
%Various preprocessing of Images
checkimage=imread('CheckSign/sign.gif');
checkimage_resize=imresize(checkimage,[512, 512]);
checkimage_grey=rgb2gray(checkimage_resize);
[m n p] = size(checkimage_grey)
for i=1:n
for j=1:m
if(checkimage_grey(i,j) ~= 0)
bimage(i,j) = 1;
else
bimage(i,j) = 0;
end
end
end
subplot (2,3,1),imshow(checkimage),title('Original Image');
subplot (2,3,2),imshow(checkimage_resize),title('Resized Image');
subplot (2,3,3),imshow(checkimage_grey),title('Grey Scale Image');
subplot (2,3,4),imshow(bimage),title('Binary Image');
But I get the below error:
Error using rgb2gray>parse_inputs (line 81)
MAP must be a m x 3 array.
Error in rgb2gray (line 35)
X = parse_inputs(varargin{:});
Error in preprocessing (line 8)
checkimage_grey=rgb2gray(checkimage_resize);
On viewing the image in imtool I see a pixel info as:
Pixel (X,Y) index [R,G,B]
<213>
R 0.80
G 0.80
B 1.00
Now I am not sure how to read this kind of index file and convert to grey scale one?
Also how do I change the image in concern to double, thinning and Inverted Imagesformat?
If I not wrong is inverted image as 1 - bimage?
For indexed images you need to read the index as well as the colorap
[ind map] = imread( 'CheckSign/sign.gif' );
Once you have all the information you need you can use ind2rgb to convert the index map into RGB image
checkimage_rgb = ind2rgb( ind, map );
You do not need a nested for-loop to "invert" the colors, it's enough
bimage = checkimage_grey ~= 0;
inverted = 1 - bimage;
I am trying to create a histogram data by reading an Image file:
>> img = imread('Flowers.jpg');
>> g = img(:,:,2);
>> bins = 0:1:255;
>> H = hist(g(:), bins);
?? Error using ==> full
Function 'full' is not defined for values of class 'uint8'.
Error in ==> C:\MATLAB\toolbox\matlab\datafun\hist.m
On line 66 ==> xx = full(real(xx)); y = full(real(y)); % For compatibility
>> version
ans =
6.5.0.180913a (R13)
>>
I am not sure why I am receiving this error?
If you have the Image Processing toolbox, I recommend you use imhist instead. This will handle uint8 images natively, without having to do any conversion, and works on binary and greyscale images.
Similarly to hist, you can either call it without outputs to just get the image directly, or get the outputs and plot the result yourself. Unlike hist, you can only give it a number of bins (for greyscale, this defaults to 256), not a vector.
img = imread('Flowers.jpg');
g = img(:,:,2);
[counts, x] = imhist(g);
stem(x,counts); % or bar, or whatever you prefer
The output x here will be the same as your bins.
I am trying to implement a simple function that its the same with the default hist() by MatLab.
We have two same images with different brightness and we have to convert them to grayscale and then use the default function of MatLab hist() to get the histograms (so far so good!).
Then we have to implement the function hist my_hist() , and when i am trying to count the frequency of the intensity the results are not the same.
It seems that it sums-up the frequency of 254 & 255 to 254 and 255 is zero!I dont know what the problem is, any help would be appreciated.
here is the code for the command line:
%Read the images and convert them from rgb to grayscale
i=imread('pic1.jpg');
j=rgb2gray(i);
x=imread('pic2.jpg');
y=rgb2gray(x);
%Display the two images
figure
imshow(j)
figure
imshow(y)
%Display the histogram of the two images
[a,b] = hist(j(:),0:1:255);
figure
plot(b,a)
[c,d]=hist(y(:),0:1:255);
figure
plot(d,c)
%Call of the built-in function
my_hist('pic1.jpg','pic2.jpg')
And here is the code of the self built function:
function []= my_hist( x,y)
%Read the images and convert them from rgb to grayscale
pic1=imread(x);
i=rgb2gray(pic1);
pic2=imread(y);
j=rgb2gray(pic2);
%Initialize two vectors to be the axis for histogram
plotx=0:255;
ploty=zeros(1,256);
%Take the dimensions of the first image pic1
[m,n] = size(i);
%With 2 loops we go through the matrix of the image and count how many
%pixels have the same intensity
for k=1:m
for l=1:n
num=i(k,l)+1;
ploty(num)=ploty(num)+1;
end
end
%Display the histogram for the first image pic1
figure
plot(plotx,ploty);
%Initialize two vectors to be the axis for histogram
plotx2=0:255;
ploty2=zeros(1,256);
%Take the dimensions of the second image pic2
[m2,n2] = size(j);
%With 2 loops we go through the matrix of the image and count how many
%pixels have the same intensity
for o=1:m2
for p=1:n2
num2=j(o,p)+1;
ploty2(num2)=ploty2(num2)+1;
end
end
%Display the histogram for the second image pic2
figure
plot(plotx2,ploty2);
end
And here are the images pic1 and pic2.
This is a problem due to your image being of integer type uint8 which can only range from 0-255:
>> a= uint8(255)
a =
255
>> a=a+1
a =
255
Convert your data to say type uint16 with
j = uint16(j);
y = uint16(y);
and your problem should be gone:
>> a=uint16(a)
a =
255
>> a=a+1
a =
256