how to convert rgb pixel value into single value in matlab? this is my code
10 次查看(过去 30 天)
显示 更早的评论
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))
0 个评论
采纳的回答
Image Analyst
2016-4-1
Never do this with a jpg image:
[m n]=size(a);
Why not? See http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/ So do this instead
[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 个评论
Image Analyst
2016-4-2
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.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computer Vision with Simulink 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!