Effacer les filtres
Effacer les filtres

how to convert rgb pixel value into single value in matlab? this is my code

13 vues (au cours des 30 derniers jours)
clear all
close all
a=imread('C:\Users\shankar\Documents\MATLAB\filtering\image.jpg');
[m n]=size(a);
t=155;
for i=1:m
for j=1:n
if a(i,j)<t
b(i,j)=0;
else
b(i,j)=1;
end
end
end
subplot(2,2,1),imshow(a),title('original image'),
subplot(2,2,2),imshow(b),title('thresholded image'),
xlabel(sprintf('threshold value is %g',t))

Réponse acceptée

Image Analyst
Image Analyst le 1 Avr 2016
Never do this with a jpg image:
[m n]=size(a);
[rows, columns, numberOfColorChannels] = size(a);
Then you can do skip the loops and threshold like this:
binaryImage = a < t;
Here's a more full blown demo using a demo image. Just copy and paste:
clc; % Clear the command window.
% clearvars;
% close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
%===============================================================================
% Read in a gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'onion.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold the image
t = 155;
binaryImage = grayImage < t;
% Display the image.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
  3 commentaires
Nivetha m
Nivetha m le 2 Avr 2016
how will you calculate MAE,SNR for above code?
Image Analyst
Image Analyst le 2 Avr 2016
For the mean absolute difference, you'd do
mad = mean2(abs(double(image1)-double(image2)));
For SNR, you can use the psnr() function. From the help:
peaksnr = psnr(A,ref) calculates the peak signal-to-noise ratio for the image A, with the image ref as the reference. A and ref must be of the same size and class.

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 1 Avr 2016
b=rgb2gray(a)
imshow(b)
  2 commentaires
Nivetha m
Nivetha m le 2 Avr 2016
thank you, very useful answer...
Nivetha m
Nivetha m le 2 Avr 2016
how will you calculate MAE,SNR for above code?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by