How to convert stance image from index to grayscale
3 views (last 30 days)
Show older comments
I am looking to replace the 'ind2gray' function as we no longer have access to the image processing toolbox. Is there a script to replace it?
% Convert stance image for each phase from 'index' to 'grayscale' and
% insert into Phases field: ManipulatedStances
for k = 1 : numPhases;
thisPhase = (Phases(k).Stance);
I = ind2gray(thisPhase,map1);
Phases(k).ManipulatedStances = I;
end
0 Comments
Answers (3)
DGM
on 12 Sep 2023
Edited: DGM
on 12 Sep 2023
No, don't use BT709 luma.
No, don't discard columns from the colormap.
If you're using IPT ind2gray(), just use ind2rgb(). It's not part of IPT, nor was it in IPT in 2016.
% get an indexed color image
[indpict CT] = imread('canoe.tif');
% convert to RGB
rgbpict = ind2rgb(indpict,CT);
% calculate luma
% since we don't have IPT, and we're living in early 2016, do it manually
factors = permute([0.299 0.587 0.114],[1 3 2]); % Rec 470/601 (this is what ind2gray would use)
ypict = sum(bsxfun(@times,im2double(rgbpict),factors),3);
ypict = im2uint8(ypict); % assuming you want uint8 output
% is there a difference? no, not really.
ypict0 = ind2gray(indpict,CT); % for comparison
immse(ypict0,ypict) % nothing but rounding errors (1LSB in a few specks)
imshow(ypict)
0 Comments
John BG
on 17 Mar 2016
Hi Denni
You want to go from RGB to Y. Y is called luminance.
There is a good presentation of TV colour calibration charts in
Basically, there are 2 industry accepted standards to calculate Y from RGB:
A=imread('PAL TV colour calibration chart 1.jpg')
imshow(A)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173705/image.jpeg)
1.-
A1=A(:,:,1);A2=A(:,:,2);A3=A(:,:,3)
Y_CCIR601=.299*A1+.587*A2+.114*A3
imwrite(Y_CCIR601,'Y_CCIR601.png') % save BW image in graphics file format
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173706/image.jpeg)
2.-
Y_ITUR=.2126*A1+.7152*A2+.0722*A3
imwrite(Y_ITUR,'Y_ITUR.png') % save BW image in graphics file format
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John
0 Comments
Image Analyst
on 17 Mar 2016
If you have a map, that converts indexed into gray, you can do it with a for loop. If the map is a 256 by 3, then extract just the first column so it's a 1-D vector of 256 elements.
if size(map, 2) > 1
map = map(:, 1);
end
Since you don't have intlut() either (because you no longer have the toolbox), you'll have to use a for loop
grayImage = zeros(size(thisPhase)); % Initialize
for k = 1 : numel(grayImage)
inputIndex = thisPhase(k);
grayImage(k) = uint8(255 * map(inputIndex));
end
Now grayImage will be a uint8 2D array the same size as thisPhase with gray levels in the range 0-255 that were assigned according to the map that you appear to have already.
6 Comments
Image Analyst
on 21 Mar 2016
I have no idea where they got map1. It should have thrown an error since it's not defined before they started using it.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!