How to do and plot histogram equalization of rgb image?
조회 수: 17 (최근 30일)
이전 댓글 표시
I am have read rgb image and separated r,g,b values
%Split into RGB Channels
Red = image(:,:,1);
Green = image(:,:,2);
Blue = image(:,:,3);
%Get histValues for each channel
[yRed, x] = imhist(Red);
[yGreen, x] = imhist(Green);
[yBlue, x] = imhist(Blue);
%Plot them together in one plot
plot(x, yRed, 'Red', x, yGreen, 'Green', x, yBlue, 'Blue')
After that I applied histeq() on individual rgb component for histogram equalization. How to plot a graph of it?
댓글 수: 1
Huda Zulfiqar
2020년 5월 11일
Index exceeds matrix dimensions.
Error in Green (line 10)
a_g = a( :, :, 2 );
When i run the below code it shows the error "index exceeds matrix dimensions"
can anyone plzz help me to correct my code??
clc;
clear;
close;
% Read the image
a=imread('mouse.png');
% Convert to grayscale incase it is color
a = rgb2gray(a);
a_g = a( :, :, 2 );
b=size(a_g);
a=double(a_g);
% Loop for Getting the Histogram of the image
hist1 = zeros(1,256);
for i=1:b(1)
for j=1:b(2)
for k=0:255
if a(i,j)==k
hist1(k+1)=hist1(k+1)+1;
end
end
end
end
%Generating PDF out of histogram by diving by total no. of pixels
pdf=(1/(b(1)*b(2)))*hist1;
%Generating CDF out of PDF
cdf = zeros(1,256);
cdf(1)=pdf(1);
for i=2:256
cdf(i)=cdf(i-1)+pdf(i);
end
cdf = round(255*cdf);
ep = zeros(b);
for i=1:b(1) %loop tracing the rows of image
for j=1:b(2) %loop tracing thes columns of image
t=(a(i,j)+1); %pixel values in image
ep(i,j)=cdf(t); %Making the ouput image using cdf as the transformation function
end
end
% Loop for Getting the Histogram of the image
hist2 = zeros(1,256);
for i=1:b(1)
for j=1:b(2)
for k=0:255
if ep(i,j)==k
hist2(k+1)=hist2(k+1)+1;
end
end
end
end
subplot(2,2,1);
imshow(uint8(a_g));
title('Original Image');
subplot(2,2,2);
imshow(uint8(ep));
title('Image after Histogram Equalization');
subplot(2,2,3);
stem(hist1);
title('Histogram of the Original Image');
subplot(2,2,4);
stem(hist2);
title('Histogram after Histogram Equalization of the Original Image');
suptitle(' The original green channel image along with image after histogram equalization with their respective histograms')
답변 (1개)
Image Analyst
2016년 12월 26일
In general histogram equalization tends to give crummy, harsh looking, unnatural images compared to linear stretches. Doing it in RGB color space like you're trying will introduce color artifacts. Much better would be to convert to lab or hsv color space and equalize ONLY the L or V channel, then convert back to rgb color space.
I don't know what you want to plot a graph of. If you want you can do just what you did for the original image on the equalized image.
For what it's worth, I've attached my demo of taking RGB histograms of iamges in a folder.
댓글 수: 3
Image Analyst
2020년 5월 11일
편집: Image Analyst
2020년 5월 11일
That's not all my code. You've altered it. After you convert it to gray scale, there is no third dimension so you can't get the second color channel. Gray scale images have no color channels -- no third dimension that you can pass 2 to.
% Convert to grayscale in case it is color
a = rgb2gray(a);
% a_g = a( :, :, 2 ); No need for this. a is no longer RGB, it's now gray scale.
[rows, columns, numberOfColorChannels] = size(a)
If you want the separate color channels, use the imsplit() function if you have r2018b or later:
% Extract the individual red, green, and blue color channels using imsplit() (introduced in R2018b).
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
or, with older MATLAB versions (r2018a or earlier), do it like this:
% Extract the individual red, green, and blue color channels. Use if you're using release r2018a or older.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!