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
Related
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'm working in matlab and i wanted to apply the Contrast Stretching for grey scale image and also RGB image ,
so for the grey scale i've tried this one and it worked
clear all;
clc;
itemp = imread('cameraman.tif'); %read the image
i = itemp(:,:,1);
rtemp = min(i); % find the min. value of pixels in all the
columns (row vector)
rmin = min(rtemp); % find the min. value of pixel in the image
rtemp = max(i); % find the max. value of pixels in all the
columns (row vector)
rmax = max(rtemp); % find the max. value of pixel in the image
m = 255/(rmax - rmin); % find the slope of line joining point
(0,255) to (rmin,rmax)
c = 255 - m*rmax; % find the intercept of the straight line
with the axis
i_new = m*i + c; % transform the image according to new slope
figure,imshow(i); % display original image
figure,imshow(i_new); % display transformed image
this is for greyscale image ,
the problem is that that i don't know how to do for the RGB image
any idea? how to implement that?
thank you :)
Could the function stretchlim (reference) be useful for your purpose?
Find limits to contrast stretch image.
Low_High = stretchlim(RGB,Tol) returns Low_High, a two-element vector
of pixel values that specify lower and upper limits that can be used
for contrast stretching truecolor image RGB.
img = imread('myimg.png');
lohi = stretchlim(img,[0.2 0.8]);
If you write
rmin = min(i(:));
Then it computes the minimum over all values in i. This will work for RGB images also, which simply are 3D matrices with 3 values along the 3rd dimension.
The rest of your code also applies directly to such images.
% Image Arithmetic Operations
% Image addition is done between two similar size of image, so image resize
% function is used to make size of both image same.
% I=I1+I2
clc
close all
I1=imread('test.jpg');
I2=imread('test_1.jpg');
subplot(2,2,1);imshow(I1);title('Original image I1');
subplot(2,2,2);imshow(I2);title('Original image I2');
I=I1+I2; % Addition of two images
subplot(2,2,3);imshow(I);title('Addition of image I1+I2');
I=I1-I2; % Subtraction of two images
subplot(2,2,4);imshow(I);title('Subtraction of image I1-I2');
figure;
subplot(2,2,1);imshow(I1);title('Original image I1');
I=I1+50;
subplot(2,2,2);imshow(I);title('Bright image I');
I=I1-100;
subplot(2,2,3);imshow(I);title('Dark image I');
M=imread('key.png');
M=im2bw(M); % Converts into binary image having 0s and 1s
I=uint8(I1).*uint8(M); % Type casting before multiplication
subplot(2,2,4);imshow(I);title('Masked Image I');
%clear all;
[filename,pathname]=uigetfile({'*.bmp;*.jpg;*.gif','Choose Image File'});
myimage=imread(filename);
if(size(myimage,3)==3)
myimage=rgb2gray(myimage);
end
[Rows,Cols]=size(myimage);
newimage=zeros(Rows,Cols);
k=1;
while k<5
for i=1:Rows
for j=1:Cols
if k==1
newimage(i,j)=myimage(i,j)-100;
end
if k==2
newimage(i,j)=myimage(i,j)-50;
end
if k==3
newimage(i,j)=myimage(i,j)+50;
end
if k==4
newimage(i,j)=myimage(i,j)+50;
end
end
end
subplot(2,2,k);imshow(newimage,[]);
k=k+1;
end
% calculate mean value
[Rows,Cols]=size(myimage);
newimage=zeros(Rows,Cols);
total=0;
for i=1:Rows
for j=1:Cols
total=total+myimage(i,j);
end
end
average=total/(Rows*Cols);
I am in big trouble because this error is not resolved and i am new to matlab coding so please help me and your solution is appreciated to me in future.
I faced an error mentioned below :
??? Error using ==> times Matrix dimensions must agree.
Error in ==> DIP_3 at 23 I=uint8(I1).*uint8(M); % Type casting before
multiplication
Assuming that the three images you are working with (test.jpg, test_1.jpg and test.png) are of the same size, the problem arises when you call the im2bw function
M=im2bw(M);
The input matrix M is a 3D matrix (eg. 100x100x3) while the output matrix is only a 2D matrix (100x100).
The error is generated because you multiply matrix I1 (which is also a 3D matrix) by a 2D matrix.
I=uint8(I1).*uint8(M);
You have therefore to make the matrix M a 3D matrix.
If you want to apply the same scaling factor on matrix I1 you can do something like:
M=imread('key.png');
M0=im2bw(M); % Converts into binary image having 0s and 1s
M(:,:,2)=M0;
M(:,:,3)=M0;
Otherwise, you have to define, somehow M(:,:,2) and M(:,:,3)
Hope this helps.
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'm currently using code:
i = imread('/usr/share/icons/matlab.png');
for k=1:1:m
for l=1:1:n
%a(k,l)=m*n;
a(k,l) = (.299*i(k,l,1))+(.587*i(k,l,2))+(.114*i(k,l,3));
end
end
imshow(a);
It shows only a white screen. Also the newly generated dimensions are n x m x 3 whereas it should be only m x n x 1.
If I use mat2gray it display the image like this
Since the image is a PNG, imread() is returning an integer image, with intensity values in the range [0 255] or equivalent, depending on the original bit depth. The conversion formula makes a a double image, which is expected to have intensities in the range [0 1]. Since all the pixel values in a are probably much greater than 1, they get clipped to 1 (white) by imshow().
The best option is to explicitly convert the image format before you start - this will take care of scaling things correctly:
i = imread('/usr/share/icons/matlab.png');
i = im2double(i);
a = .299*i(:,:,1) + .587*i(:,:,2) + .114*i(:,:,3); % no need for loops
imshow(a);
input=imread('test.jpg');
subplot(1,2,1), imshow(input), title('RGB Scale image');
[x,y,~] = size(input);
for i = 1:1:x
for j = 1:1:y
output(i,j) = 0.40*input(i,j,1) + 0.50*input(i,j,2) + 0.30*input(i,j,3);
end
end
subplot(1,2,2), imshow(output), title('Gray Scale image');